Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 606
HattrickNZ Avatar asked Feb 11 '14 02:02

HattrickNZ


People also ask

How do you rename part of a file in Linux?

Rename Files in Linux using the rename Command. The mv command is a handy tool to rename single files, but if you want to rename multiple files, then you have to use a tool that is specifically made for this. Rename command is used to rename multiple files. This command requires basic knowledge of regular expressions.

How to Remove a file with hyphen in Linux?

You can use standard UNIX or Linux rm command to delete a file name starting with - or -- . All you have to do is instruct the rm command not to follow end of command line flags by passing double dash -- option before -foo file name.

How do I change a filename in bash?

You can also rename a file by using a command in bash script. Many commands exist in Linux to rename a filename. The command 'mv' is the most popular command for renaming a file. There is another command called 'rename' that can also be used for the same task.


1 Answers

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.

like image 166
John1024 Avatar answered Oct 20 '22 10:10

John1024