问题
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?
回答1:
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)
回答2:
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
来源:https://stackoverflow.com/questions/26793400/remove-date-from-filename-but-keep-the-file-extension