LinuxQuestions.org
Review your favorite Linux distribution.
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 04-30-2009, 07:13 PM   #1
brian.m
LQ Newbie
 
Registered: Nov 2008
Posts: 11

Rep: Reputation: 0
Bash script to copy contents of folder


*** SOLUTION ***
find /Source/*/*/*/*/*/ -type d -exec cp -R {} /Dest/ \;
*** SOLUTION ***

I have been looking for a bash (.sh) script or alike (anything that can be run as a linux script) which will copy the files contained within a folder. The catch is that i want a new directory created with only part of the directory structure. For example, i want the following copied:

Code:
- Test
   - 11
      - 111
         - 8888
   - 12
      - 123
         - 9999
to:

Code:
- Folder
   - 8888
   - 9999
Any ideas?

Last edited by brian.m; 05-01-2009 at 05:35 PM.
 
Old 04-30-2009, 07:22 PM   #2
billymayday
Guru
 
Registered: Mar 2006
Location: Sydney, Australia
Distribution: Fedora, CentOS, OpenSuse, Slack, Gentoo, Debian, Arch, PCBSD
Posts: 6,678

Rep: Reputation: 122Reputation: 122
Could you give a bit more detail on the rules to follow? Is it just 3 levels down, How did you determine the numbering? Etc.
 
Old 04-30-2009, 07:36 PM   #3
brian.m
LQ Newbie
 
Registered: Nov 2008
Posts: 11

Original Poster
Rep: Reputation: 0
It is actually 5 levels down (i.e., \1\2\3\4\5), consistently. The numbering and structure of the directory is automatically generated through a php upload script. If possible, it would be nice to be able to filter the files to copy based on file type (i.e., *.zip) but this is the least of my concern.

Last edited by brian.m; 04-30-2009 at 07:40 PM.
 
Old 04-30-2009, 08:07 PM   #4
billymayday
Guru
 
Registered: Mar 2006
Location: Sydney, Australia
Distribution: Fedora, CentOS, OpenSuse, Slack, Gentoo, Debian, Arch, PCBSD
Posts: 6,678

Rep: Reputation: 122Reputation: 122
How about

find ./*/*/*/*/*/ -type d -exec cp -R {} (need sed processing on {} to ingnore leading \'s for destination) \;
 
Old 04-30-2009, 08:40 PM   #5
brian.m
LQ Newbie
 
Registered: Nov 2008
Posts: 11

Original Poster
Rep: Reputation: 0
I ran this command inside ~/temp1 :

Code:
find /home/username/folder/*/*/*/*/*/ -type d -exec cp -R {} . \;
which yielded the following:

Code:
cp: cannot stat `/home/username/folder/0/1/2/3/9999/temp1/9999/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1/temp1': File name too long
What am I doing wrong?
 
Old 04-30-2009, 10:05 PM   #6
billymayday
Guru
 
Registered: Mar 2006
Location: Sydney, Australia
Distribution: Fedora, CentOS, OpenSuse, Slack, Gentoo, Debian, Arch, PCBSD
Posts: 6,678

Rep: Reputation: 122Reputation: 122
I'm not sure, since it seems to work for me.

Are you by any chance currently in the path that's being found? You may be creating a recursion
 
Old 04-30-2009, 10:23 PM   #7
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,695
Blog Entries: 5

Rep: Reputation: 241Reputation: 241Reputation: 241
you can use -mindepth (GNU find)
Code:
 find . -mindepth 4 -type d .....
 
Old 05-01-2009, 08:50 AM   #8
brian.m
LQ Newbie
 
Registered: Nov 2008
Posts: 11

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by billymayday View Post
I'm not sure, since it seems to work for me.

Are you by any chance currently in the path that's being found? You may be creating a recursion
No, I wasn't in the current path. I'm not sure what I did, but it is working now. Thank you very much for your help!
 
Old 05-01-2009, 06:08 PM   #9
brian.m
LQ Newbie
 
Registered: Nov 2008
Posts: 11

Original Poster
Rep: Reputation: 0
I appreciate your help! The solution is to use the following command:
find /Source/*/*/*/*/*/ -type d -exec cp -R {} /Dest/ \;

Is there a way to use additional criteria to only copy directories which contain specific files? For example, I want the directory created but to only copy file with the naming convention 1.jpg and not 1a.jpg?

Last edited by brian.m; 05-01-2009 at 06:13 PM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
Script to Compare Folder Structures and File Contents rajaniyer123 Solaris / OpenSolaris 2 11-11-2008 01:31 PM
A script within a folder to delete the folder, script, and the folder's contents Cyberman Programming 15 10-17-2007 07:32 AM
synchronize contents two files with a bash script paul_mat Linux - Software 5 01-17-2007 06:31 AM
Bash script to compare dir contents Boffy Programming 2 08-02-2005 06:08 AM
Bash Programming, Recursive/Iterative Calls on Folder Contents gtwilliams Linux - Newbie 1 07-06-2005 06:44 PM


All times are GMT -5. The time now is 12:59 AM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration