LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Copy folder using find mkdir and copy (https://www.linuxquestions.org/questions/linux-newbie-8/copy-folder-using-find-mkdir-and-copy-4175467578/)

TL23 06-27-2013 08:03 AM

Copy folder using find mkdir and copy
 
I have complex folder structure and only want to copy a subfolder and its contents that I have been working on to a new directory

for example:
/data/group1/nmr/exp1/20/pdata/1..
/data/group1/nmr/exp1/20/pdata/10..
/data/group1/nmr/exp1/40/pdata/1..
/data/group1/nmr/exp1/40/pdata/10..

I want to copy
/data/group1/exp1/40/pdata/10..
to
/data/group2/exp1/40/pdata/10..

I have up to a hundred exp# with the same subfolders and only want to target the pattern ..40/pdata/10
so I
#cd /data/group1/nmr/
#find -path ./*/40/pdata/10 -type d -exec cp "{}" /data/group2/nmr/"{}" \;
but this wont work because destination looks like this:
/data/group2/nmr/.exp1/40/pdata/10
So How do I copy contents of folder 10 (keeping the parent architecture that 10 is in to a new parent directory (group2)? In other words I need to remove the "." of my destination

I have had success with making the directory architecture without files in the folders:
#find . -type d -name "10" -exec mkdir -p -- /group1/nmr/"{}" \;
AND
I have found success in copying a subfolder 1 to a new folder 10 in the same directory by:
#find -path .*/40/pdata -type d -exec cp "{}/1" "{}/10" \;

jpollard 06-27-2013 09:34 AM

Why not just use "cp -r src dest"?

TL23 06-27-2013 09:57 AM

because then I would have to delete all sufolders with .../20/pdata/1.. ../20/pdata/10 ..40/pdata/1..
and the list goes on ../999/pata/* and .../998/pdata/* so forth

So I just want to focus on copying my experiments that have the subfolders...40/pdata/10/* and copy only that keeping the architecture otherwise the programme I use wont find my files - it uses this very specific architecture

jpollard 06-27-2013 12:59 PM

Quote:

Originally Posted by TL23 (Post 4979634)
because then I would have to delete all sufolders with .../20/pdata/1.. ../20/pdata/10 ..40/pdata/1..
and the list goes on ../999/pata/* and .../998/pdata/* so forth

So I just want to focus on copying my experiments that have the subfolders...40/pdata/10/* and copy only that keeping the architecture otherwise the programme I use wont find my files - it uses this very specific architecture

Only if you copied them in the first place. That is what file globbing is for.

Beryllos 06-27-2013 01:13 PM

[deleted a long and unnecessary digression on rsync]

Beryllos 06-27-2013 01:32 PM

Quote:

Originally Posted by jpollard (Post 4979749)
Only if you copied them in the first place. That is what file globbing is for.

Oh, how I hate to make a fool of myself. jpollard's remark reminded me to read the manual (man cp) and here's what I got:
Code:

cp -vr --parents exp*/40/pdata/10 ../../group2/nmr/
Omit the -v switch if you do not need to see what happened to every file.

TL23 06-28-2013 06:04 AM

Perfect Thanks berryloss I tried that and it worked... I began playing around with cpio (got various complaints about chmod permissions but still worked)

find -path .*/40/pdata/10/* -type f | cpio -pdv /data/group2/nmr

David the H. 06-28-2013 10:45 AM

A more general technique is start by building an array with the directory names you want to work on. Then you can loop over them to modify the names, copy subfiles, or whatever.


All times are GMT -5. The time now is 11:55 AM.