BASH脚本中的简单mv命令 [英] A simple mv command in a BASH script

查看:470
本文介绍了BASH脚本中的简单mv命令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的脚本的目标:

  1. 查看目录($ Home/Music/TEST)及其子目录(它们是音乐文件)中的所有文件
  2. 找出每个文件属于哪种音乐流派
  3. 如果类型为 Heavy ,则将文件移动到另一个目录($ Home/Music/Output)
  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)

这就是我所拥有的:

#!/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

请告诉我如何编写mv命令.我尝试过的一切都失败了.我收到这样的错误:

Please tell me how to write the mv command. Everything I have tried has failed. I get errors like this:

mv:无法将"3rd Eye Landslide.mp3"移动到"/Music/Output/3rd Eye Landslide.mp3":没有此类文件或目录

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

请不要以为我写的是mminfo行-只是从旧的Google搜索中复制而来的.超越我了.

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

推荐答案

您对mv的第二个参数似乎是"~/Music/Output/$files"

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

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

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

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

~引用时不会扩展为$HOME.

这篇关于BASH脚本中的简单mv命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆