4

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 :)

3
  • 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? Sep 30, 2012 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, 2012 at 21:07
  • In that case, I think that @Petr Baudis's answer is the one you want. Sep 30, 2012 at 21:08

5 Answers 5

2

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.

1
  • 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, 2012 at 21:13
1

Try

rsync -avz ./work ./backup
1
  • Yeah I found info about rsync, but I had to figure out how to use it with cp :)
    – Vilmar
    Oct 1, 2012 at 5:19
0
cp -r ./work/ ./backup/

or

cp -r ./work ./backup/work
1
  • Tried this before, it gives out an error: "Cannot create directory './backup/src': No such file or directory
    – Dmitry Supranovich
    Sep 30, 2012 at 21:04
0
[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.

0

If you're on a Linux system:

cp -rT --parents work backup/work

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .