Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I Remove Specific Characters From File Names Using BASH

Tags:

bash

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".

like image 834
resolveaswontfix Avatar asked Sep 30 '09 19:09

resolveaswontfix


People also ask

How do I delete a specific character in bash?

The tr command (short for translate) is used to translate, squeeze, and delete characters from a string. You can also use tr to remove characters from a string.

How do you remove the first character in bash?

To remove the first and last character of a string, we can use the parameter expansion syntax ${str:1:-1} in the bash shell. 1 represents the second character index (included). -1 represents the last character index (excluded). It means slicing starts from index 1 and ends before index -1 .


1 Answers

Bash can do sed-like substitutions:

for file in *; do mv "${file}" "${file/000/}"; done 
like image 114
Dennis Williamson Avatar answered Sep 20 '22 15:09

Dennis Williamson