BASH 脚本中的简单 mv 命令

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20963477/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 09:11:01  来源:igfitidea点击:

A simple mv command in a BASH script

bashfilemv

提问by user3167628

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 genreeach file belongs to
  3. if the genre is Heavy, then move the file to another directory ($Home/Music/Output)
  1. 查看目录 ($Home/Music/TEST) 及其子目录中的所有文件(它们是音乐文件)
  2. 找出每个文件属于什么音乐流派
  3. 如果类型为Heavy,则将文件移动到另一个目录 ($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 }'|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 命令。我尝试过的一切都失败了。我收到这样的错误:

mv: cannot move ‘3rd Eye Landslide.mp3' to ‘/Music/Output/3rd Eye Landslide.mp3': No such file or directory

mv:无法将“3rd Eye Landslide.mp3”移动到“/Music/Output/3rd Eye Landslide.mp3”:没有这样的文件或目录

Please don't think I wrote that mminfo line - that's just copied from good old Google search. It's way beyond me.

请不要以为我写了那行 mminfo - 那是从旧的 Google 搜索中复制的。它远远超出了我。

回答by David T. Pierson

Your second argument to mvappears to be "~/Music/Output/$files"

你的第二个论点mv似乎是"~/Music/Output/$files"

If the ~is meant to signify your home directory, you should use $HOMEinstead, like:

如果~用于表示您的主目录,则应$HOME改用,例如:

mv "$files" "$HOME/Music/Output/$files"

~does not expand to $HOMEwhen quoted.

~$HOME引用时不会扩展到。

回答by kta

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

从外观上看,当您将文件移动到目标位置时会出现问题。请检查 /Music/Output/ 是否从当前目录中退出。或者使用绝对路径以确保安全。另外,最好不要在文件名中使用空格。希望这会有所帮助。:)

回答by BMW

Put this command before mv command should fix your problem.

将此命令放在 mv 命令之前应该可以解决您的问题。

mkdir -p ~/Music/Output