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 04-14-2020, 09:25 PM   #1
Dman58
Member
 
Registered: Nov 2010
Location: The Danger Zone
Distribution: Slackware & everything else in a VM
Posts: 294

Rep: Reputation: 31
Copy files by skipping parent directory


Hi I'm trying to copy a large list of files from a directory. The directory format is as follows.
Code:
Folder-
   Parent-
      Subfolder-
          files to copy
   Parent-
      Subfolder-
          files to copy
   Parent-
      Subfolder-
          files to copy
What I want is the subfolder and its contents skipping over the parent directory. I believe I need to use either some Regex or some kind of find command solution. I have tried the following with all kinds of various output.

This seems to be a good starting point, it shows the directories I want to begin with but when I try to pipe it into copy it seems to not like the syntax
Code:
find /tmp/box1 -maxdepth 2
This semi worked but files were copied into wrong locations:
Code:
find /tmp/box1 -maxdepth 2 | xargs cp -R /tmp/box2
Not working
Code:
find /tmp/box1 -maxdepth 2 -type f -print0 | xargs cp -R /tmp/box2
 
Old 04-15-2020, 01:54 AM   #2
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 22,047

Rep: Reputation: 7348Reputation: 7348Reputation: 7348Reputation: 7348Reputation: 7348Reputation: 7348Reputation: 7348Reputation: 7348Reputation: 7348Reputation: 7348Reputation: 7348
Probably:
Code:
find /tmp/box1 -maxdepth 2 | xargs cp -t /target/dir
if you use -print0 you need to add -0 to xargs

But probably I misunderstood, would be nice to explain where do you copy from (files) and where do you want to copy to.
"skipping" is not really meaningful.

Last edited by pan64; 04-15-2020 at 01:56 AM.
 
Old 04-15-2020, 02:18 AM   #3
MadeInGermany
Senior Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 2,832

Rep: Reputation: 1219Reputation: 1219Reputation: 1219Reputation: 1219Reputation: 1219Reputation: 1219Reputation: 1219Reputation: 1219Reputation: 1219
I think you want to recursivly copy the following
Code:
find /tmp/box1 -mindepth 2 -maxdepth 2
with cp -rp
Code:
find /tmp/box1 -mindepth 2 -maxdepth 2 -exec cp -rp -t /target/dir {} \;
 
Old 04-15-2020, 06:14 AM   #4
GazL
LQ Veteran
 
Registered: May 2008
Posts: 6,920

Rep: Reputation: 5038Reputation: 5038Reputation: 5038Reputation: 5038Reputation: 5038Reputation: 5038Reputation: 5038Reputation: 5038Reputation: 5038Reputation: 5038Reputation: 5038
How about:
cd Folder && cp -r -t /targetdir */*


P.S.
You might want to be wary of name collisions when you remove a level of a hierarchy like that. '-i' on the cp might not be such a bad idea.

Last edited by GazL; 04-15-2020 at 06:28 AM.
 
1 members found this post helpful.
Old 04-15-2020, 06:21 PM   #5
Dman58
Member
 
Registered: Nov 2010
Location: The Danger Zone
Distribution: Slackware & everything else in a VM
Posts: 294

Original Poster
Rep: Reputation: 31
Thanks for the replies. It didn't work quite as expected on the actual file hierarchy it was intended for but I was still able to transfer them successfully. I will mark this as solved.
The following was able to transfer my files as a normal cp operation. The original sample directory worked with this syntax but when I tried on my production environment I wasn't able to get the results expected.
Code:
find /tmp/box1 -mindepth 2 -maxdepth 2 -exec cp -rp -t /target/dir {} \;
This one worked in the sample but failed on the production all together.
Code:
cd Folder && cp -r -t /targetdir */*
Appreciate the suggestions and will explore different scenarios and syntax.
 
  


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
Create 5 processes from a common parent and ensure that the parent terminates after c pro_learner Linux - Newbie 1 03-06-2013 09:25 AM
find file then copy parent directory shiftomatic Linux - Newbie 7 09-24-2012 11:05 PM
File descriptors shared between child and parent causing parent to hang. bharadiaam Linux - Newbie 1 03-02-2009 01:01 AM
FC2 test 3 mouse skipping a beat, movie skipping also jang Fedora 1 10-28-2004 07:42 PM

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

All times are GMT -5. The time now is 02:54 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