This is a discussion on Shell script to restructure folders recursively within the Shell scripting forums, part of the Development/Scripting category; Hello all, I need some help. I have to copy hundred of files from the following structure: office/desk/file1.txt office/desk/file2.txt office/chair/file1.txt ...
|
|||||||
| Register | FAQ | Members List | Calendar | Forgotten your password? | Mark Forums Read |
|
|||
|
Hello all,
I need some help. I have to copy hundred of files from the following structure: office/desk/file1.txt office/desk/file2.txt office/chair/file1.txt office/chair/file2.txt office/chair/file3.txt to this structure: office_desk1/file1.txt office_desk2/file2.txt office_chair1/file1.txt office_chair2/file2.txt office_chair3/file3.txt I'm not good at writing shell script. The closes script that I found so far is this: function copy_files() { echo copying $1 files tar -cf - `find . -name "*.$1" -print` | ( cd ../dest && tar xBf - ) } I don't know how to create a folder from the folder it's in and the file number. |
| Sponsored Links | ||
|
|
|
|||
|
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"
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 |
![]() |
| Bookmarks |
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) |
|
| Thread Tools | |
| Display Modes | |
|
|
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Linux Recursively Delete Subdirectories | raj | Linux software | 0 | 04-22-2008 02:50 AM |
| writing a shell script to find out my shell name | jaymob123 | Shell scripting | 1 | 10-08-2007 01:36 AM |
| How to archive files along with folders using TAR? | Charlie82 | Shell scripting | 1 | 07-31-2007 06:26 AM |
| Public Folders In Scalix | zafar466 | Mail Servers | 0 | 02-08-2007 01:52 PM |
| auto deleting folders if empty | hinix | Shell scripting | 3 | 09-08-2005 06:02 PM |