Remove a word from file names on Linux
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?
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.