Using cp command in linux shell, how do I copy a whole directory into another directory?

3

1

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

Vilmar

Posted 2012-09-30T20:53:55.707

Reputation: 131

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 – 2012-09-30T20:57:34.187

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") – None – 2012-09-30T21:07:04.417

In that case, I think that @Petr Baudis's answer is the one you want. – Will Palmer – 2012-09-30T21:08:55.247

Answers

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.

Petr Baudis

Posted 2012-09-30T20:53:55.707

Reputation:

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! – None – 2012-09-30T21:13:17.743

1

Try

rsync -avz ./work ./backup

kjohri

Posted 2012-09-30T20:53:55.707

Reputation: 111

Yeah I found info about rsync, but I had to figure out how to use it with cp :) – Vilmar – 2012-10-01T05:19:20.053

0

cp -r ./work/ ./backup/

or

cp -r ./work ./backup/work

API-Beast

Posted 2012-09-30T20:53:55.707

Reputation: 366

Tried this before, it gives out an error: "Cannot create directory './backup/src': No such file or directory – None – 2012-09-30T21:04:25.697

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.

max

Posted 2012-09-30T20:53:55.707

Reputation: 3 329