Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Move files and change extensions

Status
Not open for further replies.

markuscj

MIS
Feb 15, 2006
35
GB
Hi

In /home/dat I have a few hundred files with a .dat extension.

I would like to copy these to /home/xml but rename them with a .xml file extension.

I've managed to get these into a file and sed them to change the file extension, but cannot get the contents of the file into the target directory.

Any help please? A solution using ksh would be preferable to me.
 
Hi,

try this, it does not move files, it just prints the commands to execute to do so.
After verifying the result, you can redirect the output to a file, edit the file and drop the lines you don't want to move and execute the reaminder of the file as a shell script.

Code:
for FILE in $(ls /home/dat)
do
BN=$(basename $FILE .dat )
print mv /home/dat/$FILE /home/xml/$BN.xml
done

 
I like arrays. Plus it should be faster than a for loop, especially if there were thousands of files and your machine is slow.

Code:
#!/bin/ksh

set -A files $(ls /home/*.dat)
fileNum=${#files[@]}
c=0
newSuffix=xml

while (( c < fileNum )); do
   prefix=${files[c]%%.*}
   suffix=${files[c]##*.}
   mv ${prefix}.${suffix} ${prefix}.${newSuffix}
((c+=1))
done
 

Copy files

Step 1: #find /home/dat -type f -name "*.dat" -exec cp {} /home/xml \;

Renames files

Step 2: #cd /home/xml

#for i in `ls`
do
mv $i `ls $i|sed 's/.dat/.xml/g'`
done


Hope this helps.

aixnag
IBM Certified Specialist - P-series AIX 5L Administration
IBM Certified Specialist - AIX V4 HACMP
IBM eServer Certified Specialist – p690 Technical Support
IBM Certified Solutions Expert - DB2 UDB V7.1 Database Administration for Unix, Linux, Windows and OS/2
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top