A simple mv command in a BASH script

2019-08-26 02:40发布

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.

标签: bash file mv
3条回答
成全新的幸福
2楼-- · 2019-08-26 02:57

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.

查看更多
够拽才男人
3楼-- · 2019-08-26 03:02

Put this command before mv command should fix your problem.

mkdir -p ~/Music/Output
查看更多
Viruses.
4楼-- · 2019-08-26 03:16

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.:)

查看更多
登录 后发表回答