A simple mv command in a BASH script

核能气质少年 提交于 2019-12-11 17:45:26

问题


The aim of my script:

  1. look at all the files in a directory ($Home/Music/TEST) and its sub-directories (they are music files)
  2. find out what music genre each file belongs to
  3. 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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!