简体   繁体   中英

bash scripting - trouble renaming files in subdirectories using find / rename

I have a large series of directories and subdirectories with many jpgs in them. I need to do two things:

  1. Create a copy of each jpg found within it's own subdirectory
  2. Rename this copied jpg such that apple.jpg becomes apple_m.jpg

I have tried to run the following commands in order:

//this creates a copy of every jpg found and appends '_m' to the end of it

find . -name '*.jpg' -execdir cp {} {}_m \;

//now again find everything with 'jpg_m' at the end and rename it as filename_m.jpg

find . -name '*.jpg_m' -execdir rename -v 's/\.jpg_m/_m\.jpg/' {} \;

However the 2nd command does'nt seem to work, can someone please explain what I'm doing wrong?

Thanks, Adi

To rename files, you can simply use it this way rename ".jpg_m" "_m.jpg" files_to_rename

so, if you want to copy and rename, you can rename it this way:

find . -name "*.jpg_m" -execdir rename ".jpg_m" "_m.jpg" {} \;

您为什么不一口气做到呢?

find . -name '*.jpg' -exec cp {} m_{} \; -exec rename "m_" "" {} \;

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