bash removing part of a file name
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
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
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.