LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - General (https://www.linuxquestions.org/questions/linux-general-1/)
-   -   using cp -u command (https://www.linuxquestions.org/questions/linux-general-1/using-cp-u-command-764368/)

newbiesforever 10-25-2009 02:04 PM

using cp -u command
 
Would someone explain to me how to use "cp -u" properly, or what I'm doing wrong in using it? According to man cp, it is supposed to back up only files that had changed since the last backup or files that don't exist on the last backup. So I entered this:
Quote:

josh@mepis1:~$ cp -u '/home/josh' '/mnt/sdb1/josh'
which resulted in only this:
Quote:

cp: omitting directory `/home/josh'
Why did that happen? I'm sure I have at least one file in the source folder and subfolders that has been changed.

janoszen 10-25-2009 02:32 PM

CP
 
Why not try /my/folder/* ?

bigrigdriver 10-25-2009 02:33 PM

From the cp man page:
Quote:

DESCRIPTION
Copy SOURCE to DEST, or multiple SOURCE(s) to DIRECTORY.
Try modifying your command to include multiple sources to the desired directory:
Code:

josh@mepis1:~$ cp -u '/home/josh/*' '/mnt/sdb1/josh'
Note the /* after /home/josh.

newbiesforever 10-25-2009 03:26 PM

Okay, I just tried that. The response was:
Quote:

cp: cannot stat `/home/josh/*': No such file or directory
That's very odd...it sure sounded as though it would work.

druuna 10-25-2009 03:36 PM

Hi,

Do away with the single quotes, use double quotes if you have to.

Single quotes prevents bash from doing expansion, so /home/josh/* is taken literal (a file called * in /home/josh).

Hope this helps.

AlucardZero 10-25-2009 03:43 PM

Alternately, use cp's -r option

druuna 10-25-2009 03:53 PM

@AlucardZero: You need to loose the single quotes, even when using -r. Expansion is done before the cp command takes effect.

I do suspect that newbiesforever's initial command (cp -u '/home/josh' '/mnt/sdb1/josh') would have worked without the single quotes (ie: cp -u /home/josh /mnt/sdb1/josh).

AlucardZero 10-25-2009 03:59 PM

If it has a voiced Z sound, then it’s “lose.” If it has a hissy S sound, then it’s “loose.” Here are examples of correct usage: “He tends to lose his keys.” “She lets her dog run loose.” Note that when “lose” turns into “losing” it loses its “E.”

Code:

alucard@organa:/tmp$ mkdir -p a/b/c
alucard@organa:/tmp$ mkdir -p d
alucard@organa:/tmp$ cp a d
cp: omitting directory `a'
alucard@organa:/tmp$ cp 'a' 'd'
cp: omitting directory `a'
alucard@organa:/tmp$ cp -r 'a' 'd'
alucard@organa:/tmp$ ls d/a/b
c
alucard@organa:/tmp$ mkdir a/e
alucard@organa:/tmp$ ls d/a
b
alucard@organa:/tmp$ cp -ur 'a' 'd'
alucard@organa:/tmp$ ls d/a
b  e

There is no expansion in the following command because there are no wildcards:
Code:

josh@mepis1:~$ cp -u '/home/josh' '/mnt/sdb1/josh'
The crux of the matter is that cp does not recurse (sub)directories unless you tell it to.

druuna 10-25-2009 04:18 PM

What are you trying to say with the cp examples?

If you put files inside the directories you'll see it fails:

Code:

mkdir -p a/b/c
mkdir -p d
touch a/{1,2} a/b/{3,4} a/b/c/{5,6}
ls  a a/b a/b/c
a:
1  2  b
a/b:
3  4  c
a/b/c:
5  6

cp a d (omitted, same result)
cp 'a' 'd' (omitted, same result)

cp -r 'a' 'd'

ls -l *
a:
total 4
-rw-r----- 1 druuna internet    0 Oct 25 22:08 1
-rw-r----- 1 druuna internet    0 Oct 25 22:08 2
drwxr-x--- 3 druuna internet 4096 Oct 25 22:08 b

d:
total 4
drwxr-x--- 3 druuna internet 4096 Oct 25 22:08 a

ls d/a/b
3  4  c

file 1 and 2 are missing from d, same is tru for the cp -ur 'a' 'd' command.

This cp -r a/* d will work. Without the single quotes!

newbiesforever 10-25-2009 04:29 PM

what is copying files recursively?
 
Okay, I'll try these procedures, but I don't understand what copying files recursively (cp -r) means. I saw it in man cp, but since I didn't understand what copying files recursively meant, I didn't know how it could help me.

druuna 10-25-2009 04:36 PM

Hi,

If you use: cp /somedir/* /someotherdir only the files in /somedir/ are copied, not the subdirectories and all its files.

The -r option tells cp that it should also copy subdirectories and its files.

I think you are looking for something like this: cp -ur /home/josh/* /mnt/sdb1/josh/

Hope this helps.

newbiesforever 10-25-2009 04:40 PM

Yes, thank you. If that's what it means, I guess I obviously need it. Since I understand only in the most abstract sense what recursion means (procedures referring to themselves), I thought it might be more complicated than copying all the subdirectories and their files.

AlucardZero 10-25-2009 05:09 PM

Quote:

Originally Posted by druuna (Post 3732164)
What are you trying to say with the cp examples?

That this statement
Quote:

I do suspect that newbiesforever's initial command (cp -u '/home/josh' '/mnt/sdb1/josh') would have worked without the single quotes (ie: cp -u /home/josh /mnt/sdb1/josh).
is incorrect.

Code:

alucard@organa:~$ mkdir -p a/b/c
alucard@organa:~$ touch a/b/c/{d,e}
alucard@organa:~$ mkdir f
alucard@organa:~$ cp -u a f
cp: omitting directory `a'
alucard@organa:~$ cp -u 'a' 'f'
cp: omitting directory `a'
alucard@organa:~$


Other than that I think we're saying the same thing.

newbiesforever 10-25-2009 05:23 PM

cp -r appears to have worked, judging by the new dates of files and folders in the backup copy--they are reflecting the date and time they were changed. But in the console window, the output stopped displaying for some reason. After displaying
Quote:

cp -ur '/home/josh' '/mnt/sdb1'
...it never continued, but never returned to the command prompt either.

AlucardZero 10-25-2009 06:09 PM

It was probably still going. Give it -v too. And I like -a as well.


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