View Single Post

  #3 (permalink)  
Old 10-26-2007, 01:47 AM
kalt kalt is offline
Junior Member
User
 
Join Date: Oct 2007
My distro: Fedora
Posts: 2
Rep Power: 0
kalt is on a distinguished road
Default

Thanks a lot. That was very much helped. I modified your code a bit to suit my need.

Code:
#!/bin/bash
SRC="./office"
DEST="./new_office"
LIST=$(find $SRC -type f -iname "file*.txt")
[ ! -d $DEST ] && mkdir $DEST || :
echo "Starting copy ..."
for f in $LIST
do
 bdirs=$(echo $f | cut -d'/' -f2)
 bdirm=$(echo $f | cut -d'/' -f3)
 bfile=$(echo $f | cut -d'/' -f4)
 bfilenum=$(echo $bfile | cut -c 5- | cut -d'.' -f1 | sed 's/[a-z]*$//')

 [ ! -d ${DEST}/${bdirs}_${bdirm}${bfilenum} ] && mkdir ${DEST}/${bdirs}_${bdirm}${bfilenum} || :
 echo -e "  $f \t--> ${DEST}/${bdirs}_${bdirm}${bfilenum}/${bfile}"
 /bin/cp -f $f ${DEST}/${bdirs}_${bdirm}${bfilenum}/${bfile}
done
echo "Done! All files copied from $SRC to $DEST"
The output:
Starting copy ...
./office/chair/file1.txt --> ./new_office/office_chair1/file1.txt
./office/chair/file3.txt --> ./new_office/office_chair3/file3.txt
./office/chair/file2.txt --> ./new_office/office_chair2/file2.txt
./office/desk/file1.txt --> ./new_office/office_desk1/file1.txt
./office/desk/file2.txt --> ./new_office/office_desk2/file2.txt
Done! All files copied from ./office to ./new_office
Reply With Quote