I accidentally ran exiftool -all= * on my Linux system in my Downloads folder (the command removes the EXIF metadata from all files in the current directory). Luckily, exiftool creates backup files by adding the extension _original (so untitled.jpg becomes untitled.jpg_original. How can I remove this suffix from the original files (which I've moved into a folder) so I can make the original files have their original file names?
- 145
2 Answers
rename
Open the terminal, change directories with cd to the directory containing the files that have _original at the end of their names and run this command:
find . -type f -name '*_original' -print0 | xargs -0 rename 's/_original//'
To find files that have an _original suffix in their file names run the first part of the above command.
find . -type f -name '*_original'
find files in subdirectories too and rename renames files wherever they are located, so the files don't need to be moved somewhere else before they are renamed.
rename 's/_original//' deletes _original from the names of the files that find found by replacing _original with a zero-length empty string.
sed and mv
If the rename program in your Linux distro is different from the rename that was used to test this code (Larry Wall's "Perl rename", rename on Debian based, prename on Red Hat based distributions), you can use mv instead of rename to rename files.
find . -type f -name '*_original' | sed -e 'p;s/_original//' | xargs -n2 mv
find . -type f -name '*_original' finds all files that have an _original suffix and sed -e 'p;s/_original//' | xargs -n2 mv replaces _original with a zero-length empty string.
- 13,488
-
Warning: In general in *nix there are many similar programs and
renamein one distro may be different thanrenamein another distro. – Kamil Maciorowski Jan 03 '20 at 06:11 -
The
renameabove appears to be Larry Wall's "Perl rename" (renameon Debian,prenameon RedHat). A very useful tool. – xenoid Jan 03 '20 at 07:40
If they all are in one level of directories, it can be as simple as this with bash:
$ for i in *.jpg_original; do mv -v "$i" "${i/.jpg_original/.jpg}"; done
renamed 'test.jpg_original' -> 'test.jpg'
$
- 9,643
vidir. – Kamil Maciorowski Jan 03 '20 at 06:02