How to rename all files in folder with name ending with "_backup"
Solution 1
try rename -v 's/\.jpg_backup$/\.jpg/' *.jpg_backup
the -v gives you detail output.
see here How to rename
Solution 2
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.
Related videos on Youtube
bigpotato
Updated on September 18, 2022Comments
-
bigpotato over 1 year
I have a bunch of images that were somehow renamed from
myimage.jpg
tomyimage.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
-
bigpotato almost 11 yearsi need it to be recursive though
-
l0b0 almost 11 years@Edmund Just use that command in the
find
. -
Radu Rădeanu almost 11 years@geirha Where the OP specified that he has whitespaces in the filenames?
-
Eliah Kagan almost 11 years@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.
-
evilsoup over 10 yearsYou can replace the
mv -- "$file" "$(echo $file | sed 's/_backup//g')"
withmv -- "$file" "${file%_backup}"
, which would give identical results and be pure bash (and therefore faster). -
empugandring almost 8 yearswhy still error
-bash: syntax error near unexpected token 'do'