Recursively move directories along with files/specific files


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Recursively move directories along with files/specific files
# 1  
Old 05-06-2011
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 01:52 PM.. Reason: Please use code tags
# 2  
Old 05-06-2011
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 08:22 AM..
This User Gave Thanks to bartus11 For This Post:
# 3  
Old 05-06-2011
Bartus11,
could you please explain the code ?
# 4  
Old 05-06-2011
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 ?
# 5  
Old 05-06-2011
First tell us if it worked in your case Smilie. 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.
# 6  
Old 05-06-2011
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.
# 7  
Old 05-06-2011
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
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Move several files into specific directories with a loop

Hello, I'm a first time poster looking for help in scripting a task in my daily routine. I am new in unix but i am attracted to its use as a mac user. Bear with me... I have several files (20) that I manually drag via the mouse into several named directories over a network. I've used rsync... (14 Replies)
Discussion started by: SonnyClark
14 Replies

2. Shell Programming and Scripting

Copy Specific Files Recursively

Is it possible to only copy selected files+its directories when you are copying recursively? find /OriginalFolder/* -type -d \{ -mtime 1 -o -mtime 2 \ } -exec cp -R {} /CopyTo/'hostname'__CopyTo/ \; -print From the above line, I want to only copy *txt and *ini files from /OriginalFolder/* ... (4 Replies)
Discussion started by: apacheLinux
4 Replies

3. Shell Programming and Scripting

Find Large Files Recursively From Specific Directory

Hi. I found many scripts in the web of achieving this. But I like to use this one find /EDWH-DMT03 -xdev -size +10000 -exec ls -la {} \;|sort -n -k 5 > LARGE.rst But the problem is, why it still list out files with 89 bytes as the output? Is there anything wrong with the command? My... (7 Replies)
Discussion started by: aimy
7 Replies

4. UNIX for Dummies Questions & Answers

Move multipe files to corresponding directories

Hi, In a parent directory there are several files in the form IDENTIFIER1x IDENTIFIER1.yyy IDENTIFIER1_Z, etc IDENTIFIER2x IDENTIFIER2.yyy IDENTIFIER2_Z, etc IDENTIFIER3x IDENTIFIER3.yyy, IDENTIFIER3_Z, etcIn the same parent directory there are corresponding directories named... (7 Replies)
Discussion started by: spirospap
7 Replies

5. Shell Programming and Scripting

Deleting all files recursively from directories while ignoring one file type

Hi, Seems like I need help again with a problem: I want to delete all files from my lets say "Music" Directory inkluding all of the subfolders except for .mp3 and .MP3 files. I tried it with globalignoring mp3 files, finding and deleting all other files, which resulted in all files... (3 Replies)
Discussion started by: pasc
3 Replies

6. UNIX for Dummies Questions & Answers

List directories and sub directories recursively excluding files

Hi, Please help me, how to get all the direcotries, its sub directories and its sub directories recursively, need to exclude all the files in the process. I wanted to disply using a unix command all the directories recursively excluding files. I tried 'ls -FR' but that display files as... (3 Replies)
Discussion started by: pointers
3 Replies

7. Shell Programming and Scripting

Loop to move files in different directories

Hi, I have various log files in different paths. e.g. a/b/c/d/e/server.log a/b/c/d/f/server.log a/b/c/d/g/server.log a/b/c/h/e/server.log a/b/c/h/f/server.log a/b/c/h/g/server.log a/b/c/i/e/server.log a/b/c/i/e/server.log a/b/c/i/e/server.log and above these have an archive folder... (6 Replies)
Discussion started by: acc01
6 Replies

8. Shell Programming and Scripting

grep'ing for specific directories, and using the output to move files

Hello, this is probably another really simple tasks for most of you gurus, however I am trying to make a script which takes an input, greps a specific file for that input, prints back to screen the results (which are directory names) and then be able to use the directory names to move files.... (1 Reply)
Discussion started by: JayC89
1 Replies

9. Shell Programming and Scripting

Recursively copy only specific files from a directory tree

Hi I am a shell-script newbie and am looking to synchronize certain files in two directory structures. Both these directory-trees are in CVS and so I dont want the CVS directory to be copied over. I want only .sh and .pl files in each subdirectory under these directory trees to be... (3 Replies)
Discussion started by: sharpsharkrocks
3 Replies
Login or Register to Ask a Question