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---------------------------------------
-
\$\begingroup\$ If you don’t know of a problem, try Code Review instead. \$\endgroup\$Ry-– Ry-2013年06月30日 23:18:36 +00:00Commented Jun 30, 2013 at 23:18
1 Answer 1
errors
- typo: function named
movieMovie
, invoked asmoveMovie
potential errors
- use of
$dest_Dir"$line"
- you expect dest_Dir to end with a slash, but that's not validated anywhere
- use
"$dest_Dir/$line"
instead (it's OK to have multiple slashes)
don't use
ls
to verify the presence/absence of files:removeEmptyDirectory() { dir="1ドル" shopt -s globstar nullglob dotglob files=("$dir"/*) # an array holding all the filenames if (( ${#files[@]} == 0 )); then # the directory is empty rmdir "$dir" else echo "warning: directory not empty: $dir" >&2 fi }
Even better, use
find
:removeEmptyDirectories() { while read -r line; do find "$dest_Dir/$line" -depth 0 -empty -exec rmdir '{}' \; done < "$DirList" }
Double quote all your variables, everywhere, unless you have a specific reason not to.
style
- clean up your indentation. It is idiomatic to put
then
anddo
at the end of the same line asif
andwhile
(seeremoveEmptyDirectories()
example above). - use a consistent naming convention
- variables
DirList
,src_Dir
-- use camelcase or underscores, but both?
- variables
- don't need the
else :
branch - "line" is not a descriptive variable name.
- use
find -exec
instead of piping toxargs
.