#!/bin/sh # Takes files downloaded through youtube-dl and cleans up their filenames stdin() { while read -r infile do ext=".$(echo $infile | rev | cut -d '.' -f 1 | rev)" ext_length=${#ext} remove=$((15+$ext_length)) filename="$(echo $infile | rev | cut -c $remove- | rev)" mv "$infile" "$filename$ext" done } cli() { for infile in "$@" do ext=".$(echo $infile | rev | cut -d '.' -f 1 | rev)" ext_length=${#ext} remove=$((15+$ext_length)) filename="$(echo $infile | rev | cut -c $remove- | rev)" mv "$infile" "$filename$ext" done } options=$(getopt -o 'hsx' -- "$@") eval set -- "$options" read_stdin=0 noext=0 while true do case "$1" in '-h') echo "usage: yt-fix [options] [files]\n\th\tprint this help and exit\n\tx\tdo not include extension in output\n\ts\tread from stdin"; exit 0;; '-s') read_stdin=1; shift; continue;; '-x') noext=1; shift; continue;; '--') shift; break;; esac done [ $read_stdin -eq 0 ] && cli "$@" || stdin