copying a directory structure with a particular file in directories below

copying a directory structure with a particular file in directories below

Post by ROuNI » Thu, 15 Jun 2006 22:32:59



Hello,
I need some help/advise on how to copy a directory structure with a
particular file in directories below the start directory.

Let's say I have many text files [*.txt] with many data files and other
files [*.data, *.*]
The directory I want to copy across to another area with only the text
files but which also retains the directory structure.

i.e.,

[root_directory]
 |__[data]
       |__[directory_1]
       |__[direcotry_2]
       |    |__[directory_2.1]
       |
       |__[directory_3]
       |
       |__[directory_N]

I want to copy the directory [data] to another area with just the *.txt
files but retain the directory structure.

I tried to do this

cp -r `find /[root_directory]/[data] -name "*.txt"` .

NOTE: The above when I include the square brackets is meant to
represent the full name of the
directory and is not to be confused with anything else.

cp -r `find /root_directory/data/ -name "*.txt"` .

When doing this, all I get is the *.txt files without the directory
structure. If the *.txt files have the same name it will cause
problems. So I can copy the directory structure it would be great.

I hope you can understand the issue/problem statement from my
explanation.
If you have any questions please ask me.

Thanks,
ROuNIN

 
 
 

copying a directory structure with a particular file in directories below

Post by Stephane Chazela » Thu, 15 Jun 2006 22:44:41



[...]
Quote:> I want to copy the directory [data] to another area with just the *.txt
> files but retain the directory structure.

> I tried to do this

> cp -r `find /[root_directory]/[data] -name "*.txt"` .

[...]

cd /path/to/data &&
  find . -name '*.txt' -type f | cpio -dvp /path/to/new/area

(assuming the file names don't contain newline characters).

(the directories will not necessarily be recreated with the same
ownership and permissions as the original ones).

If you don't have cpio, you can look at pax (which is a POSIX
command but is less common than cpio).

--
Stephane

 
 
 

copying a directory structure with a particular file in directories below

Post by ROuNI » Thu, 15 Jun 2006 23:05:55


Hello Stephane,

Quote:> cd /path/to/data &&
>   find . -name '*.txt' -type f | cpio -dvp /path/to/new/area

Thank you it works! :-) But why do I need to do "&&"? Can you explain?

Thanks,
ROuNIN

 
 
 

copying a directory structure with a particular file in directories below

Post by Dave » Thu, 15 Jun 2006 23:48:18


Sometime on Wed, 14 Jun 2006 08:05:55 -0700, ROuNIN scribbled:

Quote:> Hello Stephane,

>> cd /path/to/data &&
>>   find . -name '*.txt' -type f | cpio -dvp /path/to/new/area

> Thank you it works! :-) But why do I need to do "&&"? Can you explain?

It was meant to be a single line of text.  The && tells the shell to wait
for the command before the && to finish before executing the command after
the &&

It could just as easily have been:
(two separate lines)

cd /path/to/data
find . -name '*.txt' -type f | cpio -dvp /path/to/new/area

or even

find /path/to/data -name '*.txt' -type f | cpio -dvp /path/to/new/area

--
Dave
Our business in life is not to succeed,
but to continue to fail in good spirits.
             Robert Louis Stevenson

 
 
 

copying a directory structure with a particular file in directories below

Post by Stephane Chazela » Thu, 15 Jun 2006 23:54:59



> Sometime on Wed, 14 Jun 2006 08:05:55 -0700, ROuNIN scribbled:

>> Hello Stephane,

>>> cd /path/to/data &&
>>>   find . -name '*.txt' -type f | cpio -dvp /path/to/new/area

>> Thank you it works! :-) But why do I need to do "&&"? Can you explain?

> It was meant to be a single line of text.  The && tells the shell to wait
> for the command before the && to finish before executing the command after
> the &&

Not, it was not. It can be on one line as well as on two.

It tells to run find only if the cd command is successful, to
prevent cpio to move the wrong files.

Quote:> It could just as easily have been:
> (two separate lines)

> cd /path/to/data
> find . -name '*.txt' -type f | cpio -dvp /path/to/new/area

No. If the cd command fails, then that will move the txt files
from the current directory instead of from the data directory.

Quote:> find /path/to/data -name '*.txt' -type f | cpio -dvp /path/to/new/area

No. As it will redo the /path/to/data prefix in the new area.

--
Stephane

 
 
 

copying a directory structure with a particular file in directories below

Post by Dave » Fri, 16 Jun 2006 00:01:41


Sometime on Wed, 14 Jun 2006 15:54:59 +0000, Stephane Chazelas scribbled:

Quote:> Not, it was not. It can be on one line as well as on two.

> It tells to run find only if the cd command is successful, to
> prevent cpio to move the wrong files.

>> It could just as easily have been:
>> (two separate lines)

>> cd /path/to/data
>> find . -name '*.txt' -type f | cpio -dvp /path/to/new/area

> No. If the cd command fails, then that will move the txt files
> from the current directory instead of from the data directory.

>> find /path/to/data -name '*.txt' -type f | cpio -dvp /path/to/new/area

> No. As it will redo the /path/to/data prefix in the new area.

Thanks, I didn't realise there was more to it.

--
Dave
Our business in life is not to succeed,
but to continue to fail in good spirits.
             Robert Louis Stevenson

 
 
 

copying a directory structure with a particular file in directories below

Post by dfeus.. » Fri, 16 Jun 2006 00:26:32



> I tried to do this

> cp -r `find /[root_directory]/[data] -name "*.txt"` .

Try cp -R ...
--
Using OpenBSD with or without X & KDE?
See Dave's OpenBSD | X | KDE corner at
http://dfeustel.home.mindspring.com !!!
 
 
 

1. Bash shell script to sort files by date and copy to directory structure

I've got a shell script which takes files of various dates from
multiple servers and dumps them all into one local directory.

Now I need to make a script that sorts all of these files by date and
moves them into a directory structure such as:

/2005/December/
/2006/January/
/2006/February/
etc.

Any ideas on the best way to accomplish this?

2. RPC on Linux and NT

3. configure images directory below executable directory

4. Port of Linux to IBM Microkernel

5. Q: copy directory (containing links) as directory (as files)

6. error in compiling pine 4.33

7. HELP:2 ques: Copying large directory structure and mkfs

8. wanna be linux user

9. copying directory structure only

10. How do I copy a whole structure of directory ?

11. automatically copy and rename directory structure

12. Copy entire directory structure from one system to another (Solaris v2.5.1)

13. Copying a directory structure from Unix -> NT via FTP