Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I have a directory, let's say, "work": ~/work/ This directory has some sub-folders (d1, d2...) in it and files in these sub-folders. I want to make a backup copy in the same folder, so it would be like: ~/backup/work/ However, when I use cp -r ./work ./backup the folder "work" is not copied, only its subfoders (so now it's ~/backup/d1 ~/backup/d2...) Any idea how to make it work? I'm quite new to shell, so I'm missing something :)

share|improve this question

migrated from stackoverflow.com Oct 1 '12 at 4:27

This question came from our site for professional and enthusiast programmers.

    
are you saying that the above case is what you want to happen, or what is currently happening? Can you rephrase / reformat your question to make this more clear? –  Will Palmer Sep 30 '12 at 20:57
    
I want to copy ./work to ./backup, so it will be ./backup/work. I could only come up with the command above, at it moves not "work", but its subfolders, so it looks like ./backup/d1 ("d1" is a subfolder in "work") –  Dmitry Supranovich Sep 30 '12 at 21:07
    
In that case, I think that @Petr Baudis's answer is the one you want. –  Will Palmer Sep 30 '12 at 21:08
    
Thanks for your help! –  Dmitry Supranovich Sep 30 '12 at 21:14

4 Answers 4

up vote 2 down vote accepted

cp will not create leading directories; when it sees a path that does not exist, it will assume that it shall be the new name of the directory being copied.

Try copying into a directory that already exists: mkdir work first, then cp -r work backup.

share|improve this answer
    
Thanks, I thought there was a way of copying the folder without creating a new folder with the same name, but now I see that is impossible with just cp command. Thanks! –  Dmitry Supranovich Sep 30 '12 at 21:13

Try

rsync -avz ./work ./backup
share|improve this answer
    
Yeah I found info about rsync, but I had to figure out how to use it with cp :) –  Vilmar Oct 1 '12 at 5:19
cp -r ./work/ ./backup/

or

cp -r ./work ./backup/work
share|improve this answer
    
Tried this before, it gives out an error: "Cannot create directory './backup/src': No such file or directory –  Dmitry Supranovich Sep 30 '12 at 21:04
[max@localhost ~]$ mkdir aaa
[max@localhost ~]$ cd aaa
[max@localhost aaa]$ touch 1 2 3
[max@localhost aaa]$ mkdir bbb
[max@localhost aaa]$ touch 3 4 5
[max@localhost aaa]$ cd

This is The content of directory aaa

[max@localhost ~]$ ls -l aaa/
total 4
-rw-rw-r-- 1 max max    0 Oct 19 17:29 1
-rw-rw-r-- 1 max max    0 Oct 19 17:29 2
-rw-rw-r-- 1 max max    0 Oct 19 17:29 3
-rw-rw-r-- 1 max max    0 Oct 19 17:29 4
-rw-rw-r-- 1 max max    0 Oct 19 17:29 5
drwxrwxr-x 2 max max 4096 Oct 19 17:29 bbb

To copy any directory use cp -r or cp -R or cp --recursive command

Here -r, -R, --recursive means copy directories recursively

[max@localhost ~]$ cp -r aaa/ ccc/
[max@localhost ~]$ cd ccc/
[max@localhost ccc]$ ls -l
total 4
-rw-rw-r--  1 max max    0 Oct 19 17:30 1
-rw-rw-r--  1 max max    0 Oct 19 17:30 2
-rw-rw-r--  1 max max    0 Oct 19 17:30 3
-rw-rw-r--  1 max max    0 Oct 19 17:30 4
-rw-rw-r--  1 max max    0 Oct 19 17:30 5
drwxrwxr-x  2 max max 4096 Oct 19 17:30 bbb

Here content of directory aaa is copied to directory ccc including files and sub directory contents.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.