I have been working on a bash script for a while now to organize my movie files. I can do it by hand, but at the time I was taking a bash scripting class and thought I would like to use it to create this project. I don't know if there is a better way of doing it or not, but I would like you to take a look at my script and give me feedback on it. I believe it works, but I haven't put it through the paces yet.
#!/bin/bash
MovieList="points to .txt file with name of files"
DirList="points to a .txt file with the name to use to create the directory"
dest_Dir="where to put the files "
src_Dir="where to look for the files"
CreateLogFile="log file of directory created"
MoveLogFile="log file of move files"
# Will read in a list of movies and make the directories for them
createDirectory()
{
while read -r line
do
if [ ! -d $dest_Dir"$line" ]
then
mkdir -v $dest_Dir"$line" >> $CreateLogFile
else
:
fi
done < "$DirList"
}
# end createDirectory
# After directories have been created will go and find those movies and move
# them to their proper directory
movieMovies()
{
while read -r line
do
find $src_Dir -type f \( -iname "$line" ! -iname ".*" \) | xargs -I '{}' mv -v {} $dest_Dir"$line" >> $MoveLogFile
done < "$MovieList"
}
# end of moveMovies
# Time to tidy up by going back and seeing if there is any empty directory
# and if so removing said directory
DirectoryEmpty()
{
while read -r line
do
if [ $(ls -A $dest_Dir"$line" | wc -l) -eq 0 ]
then
rmdir $dest_Dir"$line"
else
:
fi
done < "$DirList"
}
# end of DirectoryEmpty
# ---------------------------------Main---------------------------------------
createDirectory
moveMovies
DirectoryEmpty
# ----------------------------------End---------------------------------------