简体   繁体   中英

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. etc. or more subfolders.

How would I go about renaming all and any files in this manner:

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. Do inverse grep on that list, leaving just the file names. Loop through each FILE and echo out the original folder+file, followed by the folder and file with the suffix removed by patten matching.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM