Home > Mobile >  bash doing different actions for different filetypes, for all files in directory
bash doing different actions for different filetypes, for all files in directory

Time:11-26

So I want to change the meta title of all my movies in a directory and its subdirectories

for file in *; do 
if [[ $file == *.mkv ]]
then
    mkvpropedit --set "title=$file" "$file"; 
elif [[ $file == *.mp4 ]]
then
    exiftool "-Title<Filename" *.mp4 -overwrite_original -r
else
    echo "$file wrong filename"
fi
done

That's my general idea so far, but it doesn't find the files. The commands should work, but the if conditions aren't even enterd. Also just using * doesn't search subdirectories.

CodePudding user response:

You can use two find commands:

find . -type f -name '*.mkv' -exec mkvpropedit --set "title={}" "{}" \;
find . -type f -name '*.mp4' -exec exiftool "-Title<Filename" "{}" -overwrite_original -r \;

The first find recursively searches the working directory for any files with names that end in .mkv, and then runs the following command (i.e., the command is everything between the -exec and the \;), replacing all {}s with the name of the file.

The second find does essentially the same thing, but for files with names that end in .mp4.

For more info on find, see its Linux man page or the GNU manual.


If you want to get just the filename to set the title, you can use the following:

find . -type f -name '*.mkv' -exec mkvpropedit --set "title=$(basename "{}")" "{}" \;

basename takes a path as an argument and returns just the last part of the path (i.e., the directory name or filename).

CodePudding user response:

With bash >= 4.0:

shopt -s globstar          # enable globstar
for file in **; do
  case "${file##*.}" in    # extract suffix
    mkv) echo "do something with $file"
         ;;
    mp4) echo "do something with $file"
         ;;
    *)   echo "unknown suffix at $file"
         ;;
  esac
done
  • Related