问题
The aim of my script:
- look at all the files in a directory ($Home/Music/TEST) and its sub-directories (they are music files)
- find out what music genre each file belongs to
- if the genre is Heavy, then move the file to another directory ($Home/Music/Output)
This is what I have:
#!/bin/bash
cd Music/TEST
for files in *
do
if [ -f "$files" ];then
# use mminfo to get the track info
genre=`mminfo "$files"|grep genre|awk -F: '{print $2}'|sed 's/^ *//g'|sed 's/[^a-zA-Z0-9\ \-\_]//g'`
if [ $genre = Heavy ] ;then
mv "$files" "~/Music/Output/$files"
fi
fi
done
Please tell me how to write the mv command. Everything I have tried has failed. I get errors like this:
mv: cannot move ‘3rd Eye Landslide.mp3’ to ‘/Music/Output/3rd Eye Landslide.mp3’: No such file or directory
Please don't think I wrote that mminfo line - that's just copied from good old Google search. It's way beyond me.
回答1:
Your second argument to mv
appears to be "~/Music/Output/$files"
If the ~
is meant to signify your home directory, you should use $HOME
instead, like:
mv "$files" "$HOME/Music/Output/$files"
~
does not expand to $HOME
when quoted.
回答2:
By the look of it the problem occurs when you move the file to its destination.Please check that /Music/Output/ exits from your current directory.Alternatively use the absolute path to make it safe. Also it's a good idea not use space in the file-name.Hope this will helps.:)
回答3:
Put this command before mv command should fix your problem.
mkdir -p ~/Music/Output
来源:https://stackoverflow.com/questions/20963477/a-simple-mv-command-in-a-bash-script