LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - General (https://www.linuxquestions.org/questions/linux-general-1/)
-   -   Recursive directory copy (https://www.linuxquestions.org/questions/linux-general-1/recursive-directory-copy-842614/)

benjam1nrk 11-05-2010 03:45 PM

Recursive directory copy
 
I am attempting to copy a set of sub folders from their multiple parent directories to a new location.

For example, I have three folders to copy:
/home/user/folder1/photos
/home/user/folder2/photos
/home/user/folder3/photos

I would like them to be copied to:
/newlocation/folder1
/newlocation/folder2
/newlocation/folder3

In actuality there are many folders besides folder1, folder2, folder3, and no numerical order exists. So, the folder named 'photos' would be copied to its parent folder's name in a new location. I would need this to occur for all folders in the '/home/user' directory.

Sorry if confusing, I would appreciate any assistance. Thank you!

fcintron 11-05-2010 05:41 PM

Quote:

Originally Posted by benjam1nrk (Post 4150525)
I am attempting to copy a set of sub folders from their multiple parent directories to a new location.

For example, I have three folders to copy:
/home/user/folder1/photos
/home/user/folder2/photos
/home/user/folder3/photos

I would like them to be copied to:
/newlocation/folder1
/newlocation/folder2
/newlocation/folder3

In actuality there are many folders besides folder1, folder2, folder3, and no numerical order exists. So, the folder named 'photos' would be copied to its parent folder's name in a new location. I would need this to occur for all folders in the '/home/user' directory.

Sorry if confusing, I would appreciate any assistance. Thank you!

Firs of all make a backup of your directories, just to be sure.

After that, try to use this shell script:

OLD_LOCATION='/home/user'
NEW_LOCATION='/newlocation'

for i in `ls -l $OLD_LOCATION | grep ^d | awk '{print $9}'`; do
cp $OLD_LOCATION/$i/photos $NEW_LOCATION/$i
done


Regards

unSpawn 11-05-2010 07:18 PM

How about just using 'rsync -nvra /home/user/ /newlocation/'? The "-n" will only --dry-run the process (no modifs) and the "-v" will show you what it's doing, so after you read 'man rsync' and agree with what it's doing remove the "-n"?

crts 11-05-2010 07:41 PM

How about
Code:

for (i=1;i<=3;i++);do
# mkdir -p /newlocation/folder$1 # if target does not exist
cp -a /home/user/folder$i/photos/* /newlocation/folder$i
done


frieza 11-06-2010 01:03 PM

or a more simple answer
Code:

cp -rv {folder1,folder2,folder3,...,folderN} /path/to/destination

benjam1nrk 11-08-2010 09:52 AM

Thank you for all of the detailed responses! Will attempt this afternoon to determine which method works best in this situation.

benjam1nrk 11-08-2010 10:50 AM

Quote:

Originally Posted by fcintron (Post 4150613)
Firs of all make a backup of your directories, just to be sure.

After that, try to use this shell script:

OLD_LOCATION='/home/user'
NEW_LOCATION='/newlocation'

for i in `ls -l $OLD_LOCATION | grep ^d | awk '{print $9}'`; do
cp $OLD_LOCATION/$i/photos $NEW_LOCATION/$i
done


Regards

Thank you, this worked absolutely perfect, especially since the original folder names were unknown, and were too extensive to place in a pre defined array.

The only change I made was to the cp command, changing to:
Code:

cp -af        $OLD_LOCATION/$i/photos $NEW_LOCATION/$i
to avoid any directory omission.


All times are GMT -5. The time now is 09:39 AM.