Skip to main content

Preserving links in Linux

Linux commands like tar and cp have some options that control whether symbolic links are followed or not. When you run tar command which is backing up directories contain multiple links to big files, you would get unnecessary copies of the same data. 

In the case of a cp command if a symbolic link is encountered, the data inside of the file to which the link targets is copied when -L (dereference) option used. But if you use -d (no dereference) option, cp would copy the link itself.

Look at the following example;
A directory copy_dir_1 containing a symbolic link is recursively copied to other directories with -L and -d option:

# ls -l copy_dir_1/
total 4
lrwxrwxrwx 1  root root   12 Mar 18 15:06   test_file_1 -> /test_file_1
-rw-r--r-- 1      root root 273 Mar 18 15:03    test_file_2

# cp -rL copy_dir_1 copy_dir_2
# ls -l copy_dir_2/
total 8
-rw-r--r-- 1 root root   35 Mar 18 15:12   test_file_1
-rw-r--r-- 1 root root 273 Mar 18 15:12   test_file_2

# cp -rd copy_dir_1 copy_dir_3
# ls -l copy_dir_3/
total 4
lrwxrwxrwx 1  root root   12 Mar 18 15:13   test_file_1 -> /test_file_1
-rw-r--r-- 1      root root 273 Mar 18 15:13    test_file_2

Directory copy_dir_2, created with cp -rL, has a copy of the entire test_file1. Directory copy_dir_3, created with cp -rd, is the same as copy_dir_1.

Comments

Popular posts from this blog

Find and replace with sed command in Linux

Find and replace feature is always handy. It can turn into a torture when it comes to change or delete a simple constant string in a text file. There is a handy tool in linux for doing these kind of tihngs. Actually sed is not a text editor but it is used outside of the text file to make changes.