Dan J’s Website

A variety of interesting things

Useful Terminal Trick - Putting a Bunch of Files in Eponymous Folders

I just downloaded the caption files to a bunch of video clips, and wanted each one to go in a folder with its same name (minus the file extension), cos that’s where the work on each clip is going to happen. I found a script to do that all at once from the Terminal. From here:

> for file in *.srt; do
>   dir=${file%%.*}
>   mkdir -p -- "$dir"
>   mv "$file" "$dir" 
> done

Note the semicolon that appears after the file extension, but before the do1. It’s nice to be able to do this at the command line, without having to write a script, as a one-off thing.


  1. I put the semicolon after do at first, and had to figure out why the following command was invalid... 😜