Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

I have a bunch of images that were somehow renamed from myimage.jpg to myimage.jpg_backup, so the images on my website don't load anymore. How would I recursively find all images ending with _backup and remove just the _backup part while preserving the rest of the filename?

I tried something like this:

sudo find . -name "*.jpg_backup" -exec rename -n 's/_backup$//' *.jpg_backup ';'

but it gives me an error:

Can't rename *.jpg_backup *.jpg: No such file or directory
share|improve this question

2 Answers 2

try rename -v 's/\.jpg_backup$/\.jpg/' *.jpg_backup the -v gives you detail output. see here How to rename

share|improve this answer
    
i need it to be recursive though –  Edmund Jul 19 '13 at 17:21
    
@Edmund Just use that command in the find. –  l0b0 Jul 19 '13 at 18:39

In a terminal, go to the directory where those files are (with cd /path/to/folder). If the files don't contain any whitespace or special characters \[*?, run the following command:

for file in `find . -name *.jpg_backup` ; do mv "$file" "${file%_backup}"; done

If you think that you have filenames containing whitespaces or globbing characters, use:

find . -type f -name '*.jpg_backup' -print0 \
| while IFS= read -r -d '' file ; do mv -- "$file" "${file%_backup}"; done

or:

shopt -s globstar; 
for file in /path/to/folder/**/*.jpg_backup ; do mv -- "$file" "${file%_backup}"; done

The above commands recursively will find all *.jpg_backup files from the current folder and subfolders and will rename them to *.jpg. The last one looks inside symbolic links to directories as well.

share|improve this answer
2  
This will fail for filenames containing whitespace and may also fail for filenames containing glob characters. See BashFAQ 20 on how to handle filenames safely, and BashFAQ 30 for safe ways to rename. –  geirha Jul 24 '13 at 21:21
    
@geirha Where the OP specified that he has whitespaces in the filenames? –  Radu Rădeanu Jul 25 '13 at 4:39
2  
@RaduRădeanu The issue is that the OP did not specify he doesn't have any filenames with whitespace (and even if he has none now, he may have some later). Similarly, he didn't say any of his filenames have more than 18 characters or capital letters (or the letter 'q', or ...), but any solution incompatible with such filenames would be very limited. So geirha's comment is totally on-target. With that said, your recent edit should alleviate the problem. –  Eliah Kagan Jul 25 '13 at 4:50
1  
You can replace the mv -- "$file" "$(echo $file | sed 's/_backup//g')" with mv -- "$file" "${file%_backup}", which would give identical results and be pure bash (and therefore faster). –  evilsoup Sep 21 '13 at 18:22

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.