User Tools

Site Tools


BASH: Renaming files in a directory by removing a part of their name


Let's suppose we have a bunch of files with filenames following the pattern:

noturgent.file1.txt noturgent.file2.txt noturgent.file3.txt

And we want to remove the first part (noturgent) or replace it with another word.

for filename in *; do cp $filename $(echo $filename | sed s/noturgent.// ); done

This will iterate all the files in the folder (we can use *.txt to make a distinction here), and then copy them with the new name. This is obtained using the old one and sed with s for substitution. If you just want to replace noturgent with urgent, for example, just fill up the second argument for sed (as in sed s/noturgent./urgent./)

linux/bash_rename_files.txt · Last modified: 2013/03/16 17:40 (external edit)