LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 09-10-2022, 03:46 PM   #1
RJ MacReady
LQ Newbie
 
Registered: Feb 2012
Location: Smithson
Posts: 8

Rep: Reputation: Disabled
Problem trying to copy directories recursively with cp -r


I have a directory on my computer (Kubuntu 22.04) that I want to copy to an external drive. The directory has subdirectories with multiple files in them. The external drive has the same folder structure and also contains some of the folders and some of the files. I would like to copy only files that do not exist on the destination drive.

I am not getting the result that I want so to simplify the issue I made a test setup to see if I can figure this out. In the test setup the source directory is located at /home/dave/Pictures and there are two subdirectories within. I want to copy both of those directories to the external drive at /media/dave/NO_LABEL/Pictures

The first photo shows the source on the left and the destination on the right, with the copy command below. I am using:

cp -r -n /home/dave/Pictures/ /media/dave/NO_LABEL/Pictures/

Click image for larger version

Name:	1.jpg
Views:	9
Size:	140.2 KB
ID:	39570


I was expecting that the directory structure on the right would be maintained and any missing files updated and any existing files skipped. However, as shown in picture 2, it simply copied the entire 'Pictures' directory from the source and put it inside the "Pictures" directory in the destination.

Click image for larger version

Name:	2.jpg
Views:	10
Size:	141.8 KB
ID:	39571

Is there something simple that I am missing with the cp command?
 
Old 09-10-2022, 04:00 PM   #2
astrogeek
Moderator
 
Registered: Oct 2008
Distribution: Slackware [64]-X.{0|1|2|37|-current} ::12<=X<=15, FreeBSD_12{.0|.1}
Posts: 6,269
Blog Entries: 24

Rep: Reputation: 4206Reputation: 4206Reputation: 4206Reputation: 4206Reputation: 4206Reputation: 4206Reputation: 4206Reputation: 4206Reputation: 4206Reputation: 4206Reputation: 4206
Quote:
Originally Posted by RJ MacReady View Post

cp -r -n /home/dave/Pictures/ /media/dave/NO_LABEL/Pictures/

I was expecting that the directory structure on the right would be maintained and any missing files updated and any existing files skipped. However, as shown in picture 2, it simply copied the entire 'Pictures' directory from the source and put it inside the "Pictures" directory in the destination.
Your command tells it to recursively copy /home/dave/Pictures/ into /media/dave/NO_LABEL/Pictures/, and that is exactly what it did.

What you appear to want is to copy everything contained in /home/dave/Pictures/ into /media/dave/NO_LABEL/Pictures/, which you can do like this:

Code:
cp -r -n /home/dave/Pictures/* /media/dave/NO_LABEL/Pictures/
Alternatively, like this, note the difference:

Code:
cp -r -n /home/dave/Pictures/ /media/dave/NO_LABEL/

Last edited by astrogeek; 09-10-2022 at 04:07 PM.
 
Old 09-10-2022, 04:40 PM   #3
RJ MacReady
LQ Newbie
 
Registered: Feb 2012
Location: Smithson
Posts: 8

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by astrogeek View Post
Your command tells it to recursively copy /home/dave/Pictures/ into /media/dave/NO_LABEL/Pictures/, and that is exactly what it did.

What you appear to want is to copy everything contained in /home/dave/Pictures/ into /media/dave/NO_LABEL/Pictures/, which you can do like this:

Code:
cp -r -n /home/dave/Pictures/* /media/dave/NO_LABEL/Pictures/
Alternatively, like this, note the difference:

Code:
cp -r -n /home/dave/Pictures/ /media/dave/NO_LABEL/
Ok, that does make sense to me know. Thanks!
 
Old 09-10-2022, 05:03 PM   #4
astrogeek
Moderator
 
Registered: Oct 2008
Distribution: Slackware [64]-X.{0|1|2|37|-current} ::12<=X<=15, FreeBSD_12{.0|.1}
Posts: 6,269
Blog Entries: 24

Rep: Reputation: 4206Reputation: 4206Reputation: 4206Reputation: 4206Reputation: 4206Reputation: 4206Reputation: 4206Reputation: 4206Reputation: 4206Reputation: 4206Reputation: 4206
You are welcome!

Let us know if you need help with any other Thing!

Last edited by astrogeek; 09-10-2022 at 05:09 PM.
 
Old 09-11-2022, 10:59 AM   #5
computersavvy
Senior Member
 
Registered: Aug 2016
Posts: 3,345

Rep: Reputation: 1484Reputation: 1484Reputation: 1484Reputation: 1484Reputation: 1484Reputation: 1484Reputation: 1484Reputation: 1484Reputation: 1484Reputation: 1484
You also can use rsync to copy ONLY the missing/changed files.
One advantage to rsync is that it will copy ALL the content of a directory whereas cp may only copy the visible files.
Compare the results of
Code:
 cp -ar /home/USER/* /mnt/USER/
with
Code:
 rsync -ar /home/USER/ /mnt/USER/
and you will find that the first does not copy any of the dot files/directories while the second makes an exact copy. 'ls -a /mnt/USER/' run after the first command and again after the second command will clearly show the difference
 
1 members found this post helpful.
Old 09-17-2022, 10:53 AM   #6
amplitude
LQ Newbie
 
Registered: Jul 2022
Distribution: Ubuntu 20.04
Posts: 20

Rep: Reputation: 2
Also check permissions on that folders, it must be executable to copy to another folder.
 
  


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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
can't recursively copy directories/files with non-ascii names on freeNAS Rascale *BSD 2 03-30-2009 01:07 PM
copy recursively across directories stefaandk *BSD 1 11-09-2008 10:01 PM
copy directories and files recursively using C shell or bash shell bostonuser Programming 7 06-06-2008 01:24 AM
trying to recursively copy jpegs DJOtaku Linux - General 10 08-26-2005 01:09 PM
recursively copy php.ini to www directories osio Programming 3 07-01-2005 04:24 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

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

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