简体   繁体   中英

How to replace recursively part of file name in bash

I have a directory having subdirectories containing images files with a wrong name such like filename.jpg.jpg i want to replace this recursiveley with filename.jpg i have tried:

find ./ -type f -exec sed -i 's/string1/string2/g' {} \;

or

sed -i 's/foo/bar/g' *

but none works

thank you

find ./ -iname "*.jpg.jpg" -exec rename .jpg.jpg .jpg '{}' \;

如果您有使用perl正则表达式的rename实用程序,请使用以下命令:

find ./ -iname "*.jpg.jpg" -exec rename 's/\.jpg\.jpg$/.jpg/' '{}' \;

May be you could use the inbuilt rename command.

rename 's/\.jpg\.jpg$/.jpg/' *.jpg.jpg

The first argument is a regular expression that would match filenames ending in .jpg.jpg and replace with .jpg . The second argument will look for files with .jpg.jpg format in the current directory.

For more info, read the man page.

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