How do I Remove Specific Characters From File Names Using BASH


Question

I have a lot of files that have a shared pattern in their name that I would like to remove. For example I have the files, "a_file000.tga" and "another_file000.tga". I would like to do an operation on those files that would remove the pattern "000" from their names resulting in the new names, "a_file.tga" and "another_file.tga".

1
28
9/30/2009 7:48:13 PM

Accepted Answer

Try this (this works in plain old Bourne sh as well):

for i in *000.tga
do
    mv "$i" "`echo $i | sed 's/000//'`"
done

Both arguments are wrapped in quotes to support spaces in the filenames.

48
6/6/2012 8:49:07 AM

Bash can do sed-like substitutions:

for file in *; do mv "${file}" "${file/000/}"; done

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon