user59036
user59036

Reputation: 203

remove date from filename but keep the file extension

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

Answers (2)

ghostdog74
ghostdog74

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

FDinoff
FDinoff

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

Related Questions