Ask Ubuntu is a question and answer site for Ubuntu users and developers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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

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


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
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
@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
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
why still error -bash: syntax error near unexpected token 'do' – empugandring Jun 17 at 3:43

Your Answer


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.