Reputation: 203
I have files like these in a directory called current_dir:
my data-file 2014-10.txt
data file201409.txt
and I want
my data-file.txt
data file.txt
Here is what I tried
rename 's/ *[0-9]{4}-?[0-9]{2}.*$//' current_dir/*.txt
but it removed the .txt extension as well.
I'm quite new to linux, could anyone help me?
Upvotes: 2
Views: 3963
Reputation: 342373
you can do it in bash
. only if you are sure that the date part is right beside .txt extension on the left
cd $current_dir
for file in *.txt
do
mv "$file" "${file%%[0-9]*.txt}
done
Upvotes: 0
Reputation: 31429
rename only replaces the matched part so removing .*$
from the regular expression keeps the .txt
rename 's/ *[0-9]{4}-?[0-9]{2}//' current_dir/*.txt
Examples
$ rename -n 's/ *[0-9]{4}-?[0-9]{2}//' *.txt
rename(data file201409.txt, data file.txt)
rename(my data-file 2014-10.txt, my data-file.txt)
Upvotes: 3