用于重命名和重新定位文件的 Shell 脚本
我正在做某事,需要解决以下问题。我给出了我的问题的类似版本。
假设我们有一个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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
该脚本接收两个参数:源文件夹和目标文件夹。它将把源目录下任何目录下的每个文件移动到具有新文件名的新目录:
以下是示例:
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:
Here is a sample:
重击:
Bash:
假设有以下目录树:
以下一行将创建您可以使用的“mv”命令:
编辑:
如果您的目录结构仅包含相关目录,则可以使用以下 grep 进行扩展:
注意它看起来至少包含一个目录和一个文件的任何文件。如果你有多个内部目录,那还不够好。
Assuming the following directory tree:
The following one-liner will create 'mv' commands you can use:
EDIT:
If your directory structure contains only the relevant directories, you can expand use the following grep instead:
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.