简体   繁体   English

Mac OS - 批量重命名文件夹中的所有文件但忽略所有子文件夹

[英]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.

相关问题 在Mac上打印出文件夹及其子文件夹中的所有文件 - Print out all files in a folder and its subfolders on a Mac 小写或重命名Mac OS X上目录中的所有文件 - lowercase or rename all files in a directory on Mac OS X Mac OS X Terminal批处理重命名…但具有文件夹路径 - Mac OS X Terminal batch rename…but with folder paths 从 Mac 上的命令行删除当前文件夹和所有子文件夹中的 .DS_STORE 文件 - Delete .DS_STORE files in current folder and all subfolders from command line on Mac 使用regexp在Mac OS X Bash中批量重命名文件 - Batch rename files in mac os x bash with regexp Mac OS X命令或脚本以特定名称压缩所有子文件夹 - Mac OS X command or script to zip all subfolders with a particular name 如何获取 Mac OS 或 Windows 10 文件夹下所有 PDF 文件的字数 - How can I getting word count for all PDF Files under a folder in Mac OS or Windows 10 在Mac中等效于批处理文件,可以将所有扩展名为.dat的文件移动到USB驱动器中的另一个文件夹 - What is batch file equivalent in Mac to move all files with .dat extension to another folder in a usb drive 在OSX中批量重命名,将@ 2x添加到以.png结尾的所有文件 - Batch rename in OSX, add @2x to all files ending with .png Mac OS X -- 丢失了应用程序文件夹中的所有应用程序 - Mac OS X -- lost all app in Application folder
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM