How to Automate Creating Directories Based on File Names and Moving Files Into Them in Linux

When I first started unlocking new features in Kodi, I noticed that all the artwork was making my library directories a bit unruly, so to help clean things up, I decided to move all the video files into their own folders. Unfortunately, I have quite a lot of video files. Fortunately, I uncovered a little bit of code to manage this task for me. Drop these two lines into a shell script and execute the script in your favorite flavor of Linux to create a subdirectory based on the name of each file in a given folder, then move that file into its respective directory.

find . -maxdepth 1 -type f -exec mkdir {}-DIR \; -exec mv {} {}-DIR \; ;

find . -maxdepth 1 -type d -exec rename "s:\..{2,3}-DIR::" {} \;

One thought on “How to Automate Creating Directories Based on File Names and Moving Files Into Them in Linux”

Leave a Reply