用于重命名和重新定位文件的 Shell 脚本

发布于 2024-09-26 10:34:57 字数 240 浏览 2 评论 0原文

我正在做某事,需要解决以下问题。我给出了我的问题的类似版本。

假设我们有一个music目录,其中有200个目录对应不同的电影。每个电影目录中都有一些音乐文件。

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

有什么解决方法吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

奶气 2024-10-03 10:34:57

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

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

以下是示例:

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

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
哀由 2024-10-03 10:34:57

重击:

newdir=path/to/new_directory;
find . -type d |while read d; do
  find "$d" -type f -maxdepth 1 |while read f; do
    movie="$(basename "$d" |sed 's/\(\..*\)\?//')"
    mv "$f" "$newdir/$movie_$(basename $f)";
  done;
done

Bash:

newdir=path/to/new_directory;
find . -type d |while read d; do
  find "$d" -type f -maxdepth 1 |while read f; do
    movie="$(basename "$d" |sed 's/\(\..*\)\?//')"
    mv "$f" "$newdir/$movie_$(basename $f)";
  done;
done
对岸观火 2024-10-03 10:34:57

假设有以下目录树:

./movie1:
movie1.mp3

./movie2:
movie2.mp3

以下一行将创建您可以使用的“mv”命令:

find ./ | grep "movie.*/" | awk '{print "mv "$1" "$1}' | sed 's/\(.*\)\//\1_/'

编辑:

如果您的目录结构仅包含相关目录,则可以使用以下 grep 进行扩展:

grep "\/.*\/.*"

注意它看起来至少包含一个目录和一个文件的任何文件。如果你有多个内部目录,那还不够好。

Assuming the following directory tree:

./movie1:
movie1.mp3

./movie2:
movie2.mp3

The following one-liner will create 'mv' commands you can use:

find ./ | grep "movie.*/" | awk '{print "mv "$1" "$1}' | sed 's/\(.*\)\//\1_/'

EDIT:

If your directory structure contains only the relevant directories, you can expand use the following grep instead:

grep "\/.*\/.*"

Notice it looks file anything with at least one directory and one file. If you have multiple inner directories, it won't be good enough.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文