Reputation: 532
I want to change extension of large number of files,
Example: "ABC.dat.length.doc.fasta"
Desired: ABC.mat
Upvotes: 2
Views: 254
Reputation: 67221
ls -1 *.dat.length.doc.fasta | nawk '{p=$0;gsub("dat.length.doc.fasta","mat");system("mv "p" "$0); }'
Upvotes: 0
Reputation: 2342
prename will do this easily for you. Uses regular expressions to locate and rename files.
prename 's/(.*)\.dat\.length\.doc\.fasta/$1.mat/' *.dat.length.doc.fasta
Upvotes: 1
Reputation: 4384
You can use bash
parameter expansion in a for
loop:
for file in *.length.doc.fasta
do
mv "$file" "${file%dat.length.doc.fasta}"mat
done
Another way to do it:
for file in *.length.doc.fasta
do
mv "$file" "${file%%.*}".mat
done
Upvotes: 5
Reputation: 2357
Alternatively to using bash
or awk
it is possible to use the rarely used command line tool rename
:
rename .dat.length.doc.fasta .mat *.dat.length.doc.fasta
This replaces the first argument with the second argument for the files provided is subsequent arguments (here represented by the shell expansion of *.dat.length.doc.fasta
).
Upvotes: 3
Reputation: 6087
You can do it in a single line using AWK:
ls -1 *.dat.length.doc.fasta | awk -F"." '{system("mv "$0" "$1".mat")}' -
Input:
$ ls
ABC.dat.length.doc.fasta
ABCD.dat.length.doc.fasta
ABCDE.dat.length.doc.fasta
do_not_touch.txt
Output:
$ ls
ABCDE.mat
ABCD.mat
ABC.mat
do_not_touch.txt
Upvotes: 0