I want to quickly change the filename extensions for a directory with hundreds of *.dta files. The extension will be changed from *.dta to *.dat. I will use a simple inline script to accomplish the task. Also, I want to set the file to read all. Here's an example.
Change filename extension
# cd /directory_with_datafiles
# csh
# foreach filename ( *.dta )
?set base=`basename $filename .dta`
?mv $filename $base.dat
?end
# chmod 444 *.dat
Here's an actual run
# csh
# touch t1.data t2.data t3.data t4.data t5.data
# foreach filename (*.data)
? set base=`basename $filename .data`
? mv $filename $base.dat
? end
# ls
t1.dat t2.dat t3.dat t4.dat t5.dat
This blog covers Unix system administration HOWTO tips for using inline for loops, find command, Unix scripting, configuration, SQL, various Unix-based tools, and command line interface syntax. The Unix OS supports tasks such as running hardware, device drivers, peripherals and third party applications. Share tips/comments. Read the comments. But most importantly: Read Disclaimer - Read Disclaimer.
Subscribe to:
Post Comments (Atom)
5 comments:
For ksh:
for i in *.dta
do
mv $i ${i%%\.dta}.dat
done
Thank you for your input anonymous.
Another:
find . -name "*.dta" | pax -rw -s '/\.dta$/\.dat/g' . ; rm -f *.dta
Thanks sitchai!
There is a rename command in linux;
man rename says:
For example, to rename all files matching "*.bak" to strip the extension, you might say
rename 's/\.bak$//' *.bak
so your command would be something like;
rename 's/\.dta$/.dat/' *.dta
Post a Comment