remove date from filename but keep the file extension

对着背影说爱祢 提交于 2019-12-11 03:15:43

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!