HattrickNZ
HattrickNZ

Reputation: 4643

bash removing part of a file name

I have the following files in the following format:

$ ls CombinedReports_LLL-*'('*.csv
CombinedReports_LLL-20140211144020(Untitled_1).csv
CombinedReports_LLL-20140211144020(Untitled_11).csv
CombinedReports_LLL-20140211144020(Untitled_110).csv
CombinedReports_LLL-20140211144020(Untitled_111).csv
CombinedReports_LLL-20140211144020(Untitled_12).csv
CombinedReports_LLL-20140211144020(Untitled_13).csv
CombinedReports_LLL-20140211144020(Untitled_14).csv
CombinedReports_LLL-20140211144020(Untitled_15).csv
CombinedReports_LLL-20140211144020(Untitled_16).csv
CombinedReports_LLL-20140211144020(Untitled_17).csv
CombinedReports_LLL-20140211144020(Untitled_18).csv
CombinedReports_LLL-20140211144020(Untitled_19).csv

I would like this part removed:
20140211144020 (this is the timestamp the reports were run so this will vary)

and end up with something like:

CombinedReports_LLL-(Untitled_1).csv
CombinedReports_LLL-(Untitled_11).csv
CombinedReports_LLL-(Untitled_110).csv
CombinedReports_LLL-(Untitled_111).csv
CombinedReports_LLL-(Untitled_12).csv
CombinedReports_LLL-(Untitled_13).csv
CombinedReports_LLL-(Untitled_14).csv
CombinedReports_LLL-(Untitled_15).csv
CombinedReports_LLL-(Untitled_16).csv
CombinedReports_LLL-(Untitled_17).csv
CombinedReports_LLL-(Untitled_18).csv
CombinedReports_LLL-(Untitled_19).csv

I was thinking simply along the lines of the mv command, maybe something like this:

$ ls CombinedReports_LLL-*'('*.csv

but maybe a sed command or other would be better

Upvotes: 15

Views: 39034

Answers (5)

John1024
John1024

Reputation: 113814

rename is part of the perl package. It renames files according to perl-style regular expressions. To remove the dates from your file names:

rename 's/[0-9]{14}//' CombinedReports_LLL-*.csv

If rename is not available, sed+shell can be used:

for fname in Combined*.csv ; do mv "$fname" "$(echo "$fname" | sed -r 's/[0-9]{14}//')" ; done

The above loops over each of your files. For each file, it performs a mv command: mv "$fname" "$(echo "$fname" | sed -r 's/[0-9]{14}//')" where, in this case, sed is able to use the same regular expression as the rename command above. s/[0-9]{14}// tells sed to look for 14 digits in a row and replace them with an empty string.

Upvotes: 20

user5318429
user5318429

Reputation: 1

I'm using the advice given in the top response and have put the following line into a shell script:

ls *.nii | xargs rename 's/[f_]{2}//' f_0*.nii

In terminal, this line works perfectly, but in my script it will not execute and reads * as a literal part of the file name.

Upvotes: 0

Pedro Nunes
Pedro Nunes

Reputation: 420

for f in CombinedReports_LLL-* ; do
    b=${f:0:20}${f:34:500}
    mv "$f" "$b"
done

You can try line by line on shell:

f="CombinedReports_LLL-20140211144020(Untitled_11).csv"
b=${f:0:20}${f:34:500}
echo $b

Upvotes: 2

Alfe
Alfe

Reputation: 59416

Without using an other tools like rename or sed and sticking strictly to bash alone:

for f in CombinedReports_LLL-*.csv
do
  newName=${f/LLL-*\(/LLL-(}
  mv -i "$f" "$newName"
done

Upvotes: 10

Kevin
Kevin

Reputation: 2182

You can use the rename utility for this. It uses syntax much like sed to change filenames. The following example (from the rename man-page) shows how to remove the trailing '.bak' extension from a list of backup files in the local directory:

rename 's/\.bak$//' *.bak

Upvotes: 0

Related Questions