Unix/Linux Go Back    


Shell Programming and Scripting BSD, Linux, and UNIX shell scripting — Post awk, bash, csh, ksh, perl, php, python, sed, sh, shell scripts, and other shell scripting languages questions here.

Recursively move directories along with files/specific files

Shell Programming and Scripting


Closed    
 
Thread Tools Search this Thread Display Modes
    #1  
Old Unix and Linux 05-06-2011
frintocf frintocf is offline
Registered User
 
Join Date: May 2011
Last Activity: 5 October 2013, 11:32 PM EDT
Posts: 43
Thanks: 18
Thanked 0 Times in 0 Posts
Recursively move directories along with files/specific files

I would like to transfer all files ending with .log from /tmp and to /tmp/archive (using find )
The directory structure looks like :-

Code:
/tmp
    a.log
    b.log
    c.log
         /abcd
             d.log
             e.log

When I tried the following command , it movies all the log files to /tmp/archive (does not maintain the directory structure )

Code:

find /tmp -name "*.log" -type f -exec mv {} /tmp/archive \; 

The output is


Code:
/tmp/archive
     a.log
     b.log
     c.log
     d.log
     e.log

whereas the output should be


Code:
/tmp/archive
     a.log
     b.log
     c.log
          /abcd
             d.log
             e.log

For copying (cp) ,this can be done using -R but does not work with move (mv) .Is there any simple command to achieve this .

I had gone through forums and come across few commands rsync ,tar ......


Code:
find /tmp -name "*.log" -type f -exec rsync {} /tmp/archive \;   /* gives the same output */

Also , could anyone explain the difference between cp and rsync
What is the usage of the command tar?

Last edited by Franklin52; 05-24-2011 at 11:52 AM.. Reason: Please use code tags
Sponsored Links
    #2  
Old Unix and Linux 05-06-2011
bartus11's Unix or Linux Image
bartus11 bartus11 is offline
Registered User
 
Join Date: Apr 2009
Last Activity: 3 August 2016, 11:03 AM EDT
Posts: 3,733
Thanks: 7
Thanked 1,153 Times in 1,123 Posts
Try:
Code:
cd /tmp;find . -name "*.log" | xargs -i bash -c "echo {} | cpio -pdmu /tmp/arc; rm -f {}"


Last edited by bartus11; 05-06-2011 at 06:22 AM..
The Following User Says Thank You to bartus11 For This Useful Post:
frintocf (05-06-2011)
Sponsored Links
    #3  
Old Unix and Linux 05-06-2011
pravin27 pravin27 is offline Forum Advisor  
Advisor
 
Join Date: Sep 2009
Last Activity: 21 June 2017, 2:44 PM EDT
Location: ./India/Bangalore
Posts: 1,271
Thanks: 70
Thanked 299 Times in 290 Posts
Bartus11,
could you please explain the code ?
    #4  
Old Unix and Linux 05-06-2011
frintocf frintocf is offline
Registered User
 
Join Date: May 2011
Last Activity: 5 October 2013, 11:32 PM EDT
Posts: 43
Thanks: 18
Thanked 0 Times in 0 Posts
Bartus ,
I did understand the first part of the command
Could you also please explain the later part of the command ,
xargs -i bash -c "echo {} | cpio -pdmu /tmp/arc; rm -f {}" What is the usage of xargs , bash , cpio -pdmu

Is there any other suggestions ?
Sponsored Links
    #5  
Old Unix and Linux 05-06-2011
bartus11's Unix or Linux Image
bartus11 bartus11 is offline
Registered User
 
Join Date: Apr 2009
Last Activity: 3 August 2016, 11:03 AM EDT
Posts: 3,733
Thanks: 7
Thanked 1,153 Times in 1,123 Posts
First tell us if it worked in your case Linux. xargs is passing the output from find as "{}". It is then used in bash command to execute two utilities: cpio and rm. cpio copies the file with its subdirectories if necessary (-d cpio option), while rm removes the original file after it has been copied.
Sponsored Links
    #6  
Old Unix and Linux 05-06-2011
methyl methyl is offline
Registered User
 
Join Date: Mar 2008
Last Activity: 8 June 2016, 9:58 PM EDT
Posts: 6,402
Thanks: 288
Thanked 676 Times in 645 Posts
What Operating System and version do you have?

Code:
uname -a

What Shell do you use?

Code:
echo $SHELL

On a design point, is this a one-off exercise or a job which will be run more than once? There are real scripting issues with your archive directory /tmp/archive being under /tmp .
Also on most unix systems /tmp is of limited size and considered volatile storage for files used by the Operating System. It is usual to create separate filesystems for application data and logs.
Sponsored Links
    #7  
Old Unix and Linux 05-06-2011
frintocf frintocf is offline
Registered User
 
Join Date: May 2011
Last Activity: 5 October 2013, 11:32 PM EDT
Posts: 43
Thanks: 18
Thanked 0 Times in 0 Posts
Sorry Bartus , It didnt work

Here is one other example which I have tried

/* This is the structure for abcd*/

$ ls -R /usr/bin/abcd
/usr/bin/abcd:
efgh fff1.log fff2.log fff3.log

/usr/bin/abcd/efgh:
fff4.log fff5.log

/* I am creating another directory called abcd1*/

$ mkdir /usr/bin/abcd1

/* Using your command , I am moving the *.log files from abcd to abcd1 ,including the directory structure ( sub directories ). As you can see , it fails with the output below */

$ find /usr/bin/abcd -name "*.log" | xargs -i bash -c "echo {} | cpio -pdmu /usr/bin/abcd1 ; rm -f {} "

0 blocks
0 blocks
0 blocks
0 blocks
0 blocks

if xargs is for passing the output then what is the usage -exec {} .Is there any difference between these two

---------- Post updated at 07:10 AM ---------- Previous update was at 07:02 AM ----------

Hi Methyl ,

The information you asked for

Operating System uname -a

Linux blrlx232n1 2.6.16.60-0.21-smp #1 SMP Tue May 6 12:41:02 UTC 2008 x86_64 x86_64 x86_64 GNU/Linux

echo $SHELL

/bin/ksh
Sponsored Links
Closed

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Linux More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
List directories and sub directories recursively excluding files pointers UNIX for Dummies Questions & Answers 3 02-23-2011 08:39 PM
Loop to move files in different directories acc01 Shell Programming and Scripting 6 10-06-2010 09:41 PM
want to move files in a dir into different directories based on the filename Sriranga UNIX for Dummies Questions & Answers 2 03-10-2010 04:49 AM
grep'ing for specific directories, and using the output to move files JayC89 Shell Programming and Scripting 1 12-06-2008 02:37 AM
Recursively copy only specific files from a directory tree sharpsharkrocks Shell Programming and Scripting 3 01-07-2008 01:16 PM



All times are GMT -4. The time now is 10:29 PM.