Copy directory in linux

Posted on October 5, 2012

It’s very easy to copy directory in linux,normally we can just use:

cp -r dir1 dir2

to copy directory recursively in linux

And we can add option -L to follow symbolic link when copy directory,use option -p to preserve(keep) to old permission of the directory you want to copy.

Please see below commands for examples:

bash-3.2$ ls -l
total 4
drwxr-x--- 3 user1 group1 4096 Feb  2 10:23 dir1
-rw-r----- 1 user1 group1    0 Feb  2 10:23 file1
bash-3.2$ ls -l dir1/
total 4
drwxr-x--- 2 user1 group1 4096 Feb  2 10:23 dir2
lrwxrwxrwx 1 user1 group1    8 Feb  2 10:23 file1 -> ../file1
-rw-r----- 1 user1 group1    0 Feb  2 10:23 file2
bash-3.2$ cp -r dir1 dir2
bash-3.2$ ls -l dir2
total 4
drwxr-x--- 2 user1 group1 4096 Feb  2 10:29 dir2
lrwxrwxrwx 1 user1 group1    8 Feb  2 10:29 file1 -> ../file1
-rw-r----- 1 user1 group1    0 Feb  2 10:29 file2
bash-3.2$ cp -r -L dir1 dir3
bash-3.2$ ls -l dir3/
total 4
drwxr-x--- 2 user1 group1 4096 Feb  2 10:29 dir2
-rw-r----- 1 user1 group1    0 Feb  2 10:29 file1
-rw-r----- 1 user1 group1    0 Feb  2 10:29 file2
bash-3.2$ cp -r -p dir1 dir4
bash-3.2$

» Filed Under Linux

Comments

Leave a Reply