[英]Mac OS - Batch Rename All Files in Folder but Disregard All SubFolders
I have a bunch of folders that I would like to rename all the files contained within minus any subdfolders.我有一堆文件夹,我想重命名其中包含的所有文件减去任何子文件夹。
For example lets say I have two parent folders:例如,假设我有两个父文件夹:
ParentFolder1 - [PF1]
ParentFolder2 - [PF2]
Each parent folder has various amounts of subfolders:每个父文件夹都有不同数量的子文件夹:
SubParentFolder1_1
SubParentFolder1_2
SubParentFolder2_1
Inside the ParentFolder and each SubParentFolder there can be files such as.mp3, .txt.在 ParentFolder 和每个 SubParentFolder 中可以有 .mp3、.txt 等文件。 etc. or more subfolders.等或更多子文件夹。
How would I go about renaming all and any files in this manner:我将如何 go 以这种方式重命名所有和任何文件:
example.mp3 -> example - [PF1]
example.txt -> example - [PF2]
example.docx -> example - [PF2]
Appreciate any input!感谢任何输入!
This is a way to list files (not folders) in a range of directories and then do something with them... Specifics of renaming are up to you.这是一种在一系列目录中列出文件(而不是文件夹)然后对它们执行某些操作的方法……重命名的细节取决于您。
for FOLD in Parent*;
do for FILE in $(ls -p $FOLD | grep -v "/");
do echo "$FOLD/$FILE" "$FOLD/${FILE%.*}";
done; done;
Explanation:解释:
For each folder ( FOLD
) in directories matching the wildcard Parent*
, list the contents, adding /
to the end of directory names.对于与通配符Parent*
匹配的目录中的每个文件夹 ( FOLD
),列出内容,将/
添加到目录名称的末尾。 Do inverse grep
on that list, leaving just the file names.在该列表上执行反向grep
,只留下文件名。 Loop through each FILE
and echo out the original folder+file, followed by the folder and file with the suffix removed by patten matching.循环遍历每个FILE
并回显原始文件夹+文件,然后是通过模式匹配删除后缀的文件夹和文件。
Once you test this, you can replace echo
with mv
to do the actual renaming... (I've put these on separate lines to make them more readable, but would usually run this as one long command.一旦你测试了这个,你可以用mv
替换echo
来进行实际的重命名......(我把它们放在单独的行上以使它们更具可读性,但通常会将其作为一个长命令运行。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.