Reputation: 97
I need to rename all filenames (of varying lengths) in directory that end in ".dat.txt" to just ".txt"
INPUT:
FOO.dat.txt, FOO2.dat.txt, SPAM.dat.txt, SPAM_AND_EGGS.dat.txt
DESIRED OUTPUT:
FOO.txt, FOO2.txt, SPAM.txt, SPAM_AND_EGGS.txt
Have been trying to use "rename" but I've never used for this situation before.
for f in DIRECTORY'/'*.dat.txt
do
rename 's/*.dat.txt/*.txt' *
done
Thanks for your help!!!
Upvotes: 3
Views: 3658
Reputation: 18331
Assuming you have the rename program from the util-linux package installed:
rename .dat.txt .txt *.dat.txt
But I think you might have the perl version instead:
rename 's/\.dat\.txt/\.txt/' *.dat.txt
See this Linux Questions wiki page for a brief summary of the two versions.
Upvotes: 3
Reputation: 6418
for i in FOO*dat.txt; do mv "$i" "${i%%dat.txt}txt"; done
Using bash parameter expansion: http://wiki.bash-hackers.org/syntax/pe#substring_removal
Or perhaps more elegantly:
for i in *dat.txt; do mv "$i" "${i/dat.txt/txt}"; done
Upvotes: 1
Reputation: 1262
This should work:
for old in FOO*.dat.txt
do
new=$(echo $old | sed 's/.dat.txt/.txt/g')
mv "$old" "$new"
done
Upvotes: 2