用于重命名和重定位文件的Shell脚本 [英] Shell Script for renaming and relocating the files

查看:166
本文介绍了用于重命名和重定位文件的Shell脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在努力解决以下问题。我给了一个类似的我的问题版本。



说我们有一个音乐目录,其中有200个目录对应于不同的电影。在每个电影目录中有一些音乐文件。



现在,说一个文件music.mp3在文件夹movie.mp3中。我想制作一个shell脚本,以便将文件重命名为movie_music.mp3,并将其放在我提到的某个文件夹中。基本上,子目录中的所有文件都将被重命名,并被放入一个新的目录。



这个的任何解决方法?

解决方案

此脚本接收两个参数:源文件夹和目标文件夹。它会将源目录下的任何目录下的每个文件移动到具有新文件名的新目录中:

 #!/ bin。 sh 
echo$ 1$ 2
$ 1/ *中的目录; do
如果[-d$ dir];然后
用于$ dir/ *中的文件;如果[-f$ file];然后
echo$ {file} - > $ 2 /`basename$ dir`_`basename$ {file}`
mv$ {file}$ 2/`basename $ dir`_`basename$ {file}`
fi
done
fi
done

以下是一个示例:

 
bash move.sh dir dir2
从dir2转到dir2
dir / d1 / f1 - > dir2 / d1_f1
dir / d1 / f2 - > dir2 / d1_f2
dir / d2 / f1 - > dir2 / d2_f1
dir / d2 / f2 - > dir2 / d2_f2


I am working on something and need to solve the following. I am giving a analogous version of mine problem.

Say we have a music directory, in which there are 200 directories corresponding to different movies. In each movie directory there are some music files.

Now, say a file music.mp3 is in folder movie.mp3 . I want to make a shell script such that it renames the file to movie_music.mp3 and put it in some folder that I mention to it. Basically, all the files in the subdirectories are to be renamed and to be put in a new directory.

Any workaround for this?

解决方案

This script receives two arguments: the source folder and the destination folder. It will move every file under any directory under the source directory to the new directory with the new filename:

#!/bin.sh
echo "Moving from $1 to $2"
for dir in "$1"/*; do
  if [ -d "$dir" ]; then
    for file in "$dir"/*; do
      if [ -f "$file" ]; then
        echo "${file} -> $2/`basename "$dir"`_`basename "${file}"`"
        mv "${file}" "$2"/`basename "$dir"`_`basename "${file}"`
      fi
    done
  fi
done

Here is a sample:

bash move.sh dir dir2
Moving from dir to dir2
dir/d1/f1 -> dir2/d1_f1
dir/d1/f2 -> dir2/d1_f2
dir/d2/f1 -> dir2/d2_f1
dir/d2/f2 -> dir2/d2_f2

这篇关于用于重命名和重定位文件的Shell脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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