nixCraft Linux Forum

nixCraft

Linux Tech Support Forum

Shell script to restructure folders recursively

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 ...


Go Back   nixCraft Linux Forum > Development/Scripting > Shell scripting

Register FAQ Members List Calendar Forgotten your password? Mark Forums Read
  #1 (permalink)  
Old 10-25-2007, 04:23 PM
Junior Member
User
 
Join Date: Oct 2007
My distro: Fedora
Posts: 2
Rep Power: 0
kalt is on a distinguished road
Default Shell script to restructure folders recursively

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.
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 10-25-2007, 08:25 PM
nixcraft's Avatar
Never say die
User
 
Join Date: Jan 2005
Location: BIOS
My distro: Ubuntu
Posts: 1,036
Rep Power: 10
nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute
Default

Try out following code. setup DEST for actual location where you wanna put files. You need to put script in root directory of office/desk. For example it it is /home/tom/office/desk, put script in /home/tom, alternatively you can modify LIST=(find ...) to specify path.

Code:
#!/bin/bash
LIST=$(find . -iname "*.txt")
DEST="/tmp/test"
[ ! -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)
 [ ! -d ${DEST}/${bdirs}_${bdirm} ] && mkdir ${DEST}/${bdirs}_${bdirm} || :
 /bin/cp -f $f ${DEST}/${bdirs}_${bdirm}/${bfile}
done
echo "Done! All files copied to $DEST"
__________________
Vivek | My personal blog
Linux Evangelist
Play hard stay cool
Reply With Quote
  #3 (permalink)  
Old 10-26-2007, 01:47 AM
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
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Similar Threads

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


All times are GMT +5.5. The time now is 09:16 PM.


Powered by vBulletin® Version 3.7.4 - Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.2.0

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36