LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 11-05-2010, 03:45 PM   #1
benjam1nrk
LQ Newbie
 
Registered: Mar 2010
Distribution: CentOS
Posts: 24

Rep: Reputation: 1
Question 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!
 
Old 11-05-2010, 05:41 PM   #2
fcintron
Member
 
Registered: Oct 2010
Location: México
Distribution: Centos
Posts: 72

Rep: Reputation: 4
Quote:
Originally Posted by benjam1nrk View Post
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
 
Old 11-05-2010, 07:18 PM   #3
unSpawn
Moderator
 
Registered: May 2001
Posts: 29,415
Blog Entries: 55

Rep: Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600
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"?
 
1 members found this post helpful.
Old 11-05-2010, 07:41 PM   #4
crts
Senior Member
 
Registered: Jan 2010
Posts: 2,020

Rep: Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757
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
 
Old 11-06-2010, 01:03 PM   #5
frieza
Senior Member
 
Registered: Feb 2002
Location: harvard, il
Distribution: Ubuntu 11.4,DD-WRT micro plus ssh,lfs-6.6,Fedora 15,Fedora 16
Posts: 3,233

Rep: Reputation: 406Reputation: 406Reputation: 406Reputation: 406Reputation: 406
or a more simple answer
Code:
cp -rv {folder1,folder2,folder3,...,folderN} /path/to/destination
 
Old 11-08-2010, 09:52 AM   #6
benjam1nrk
LQ Newbie
 
Registered: Mar 2010
Distribution: CentOS
Posts: 24

Original Poster
Rep: Reputation: 1
Thank you for all of the detailed responses! Will attempt this afternoon to determine which method works best in this situation.
 
Old 11-08-2010, 10:50 AM   #7
benjam1nrk
LQ Newbie
 
Registered: Mar 2010
Distribution: CentOS
Posts: 24

Original Poster
Rep: Reputation: 1
Quote:
Originally Posted by fcintron View Post
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.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
K3b Recursive Copy tronayne Slackware 6 08-21-2007 07:43 AM
Recursive Copy / Create Luvz2Fly Programming 5 11-18-2006 04:52 PM
Recursive Copy of certain files Whiskerz Linux - Newbie 5 12-18-2005 07:06 PM
Recursive Copy fizgig10 Linux - Newbie 1 07-16-2005 11:58 PM
How to do recursive file copy of directory for specific files? Arodef Linux - Newbie 4 06-29-2004 05:35 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 08:15 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration