Skip to content
Advertisement

Symbolic link difference between cp -sr and ln -s [closed]

I’m trying to make symbolic links in my Dropbox so that I don’t have duplicate files all over my computer (file space is limited). I’ve come across some confusing behavior in ln -s vs. cp -s. Basically, ln -s seems to make a link to the original folder, but then copies all the files within the folder to a new location. cp -sr recursively makes links to all files within the folder (which is the desired behavior).

For example:

nate@auerilas ~ $ mkdir ~/Desktop/test_ln
nate@auerilas ~ $ mkdir ~/Desktop/test_cp
nate@auerilas ~ $ cp -sr ~/Documents/GIS_Files/WorldClim2/Monthly/Prec/ ~/Desktop/test_cp
nate@auerilas ~ $ ln -s ~/Documents/GIS_Files/WorldClim2/Monthly/Prec/ ~/Desktop/test_ln

nate@auerilas ~ $ cd ~/Desktop/test_cp/Prec
nate@auerilas ~/Desktop/test_cp/Prec $ ls -ls
total 52
4 lrwxrwxrwx 1 nate nate 65 Apr 27 11:40 readme.txt -> /home/nate/Documents/GIS_Files/WorldClim2/Monthly/Prec/readme.txt
4 lrwxrwxrwx 1 nate nate 76 Apr 27 11:40 wc2.0_30s_prec_01.tif -> /home/nate/Documents/GIS_Files/WorldClim2/Monthly/Prec/wc2.0_30s_prec_01.tif
4 lrwxrwxrwx 1 nate nate 76 Apr 27 11:40 wc2.0_30s_prec_02.tif -> /home/nate/Documents/GIS_Files/WorldClim2/Monthly/Prec/wc2.0_30s_prec_02.tif
4 lrwxrwxrwx 1 nate nate 76 Apr 27 11:40 wc2.0_30s_prec_03.tif -> /home/nate/Documents/GIS_Files/WorldClim2/Monthly/Prec/wc2.0_30s_prec_03.tif
4 lrwxrwxrwx 1 nate nate 76 Apr 27 11:40 wc2.0_30s_prec_04.tif -> /home/nate/Documents/GIS_Files/WorldClim2/Monthly/Prec/wc2.0_30s_prec_04.tif
4 lrwxrwxrwx 1 nate nate 76 Apr 27 11:40 wc2.0_30s_prec_05.tif -> /home/nate/Documents/GIS_Files/WorldClim2/Monthly/Prec/wc2.0_30s_prec_05.tif
4 lrwxrwxrwx 1 nate nate 76 Apr 27 11:40 wc2.0_30s_prec_06.tif -> /home/nate/Documents/GIS_Files/WorldClim2/Monthly/Prec/wc2.0_30s_prec_06.tif
4 lrwxrwxrwx 1 nate nate 76 Apr 27 11:40 wc2.0_30s_prec_07.tif -> /home/nate/Documents/GIS_Files/WorldClim2/Monthly/Prec/wc2.0_30s_prec_07.tif
4 lrwxrwxrwx 1 nate nate 76 Apr 27 11:40 wc2.0_30s_prec_08.tif -> /home/nate/Documents/GIS_Files/WorldClim2/Monthly/Prec/wc2.0_30s_prec_08.tif
4 lrwxrwxrwx 1 nate nate 76 Apr 27 11:40 wc2.0_30s_prec_09.tif -> /home/nate/Documents/GIS_Files/WorldClim2/Monthly/Prec/wc2.0_30s_prec_09.tif
4 lrwxrwxrwx 1 nate nate 76 Apr 27 11:40 wc2.0_30s_prec_10.tif -> /home/nate/Documents/GIS_Files/WorldClim2/Monthly/Prec/wc2.0_30s_prec_10.tif
4 lrwxrwxrwx 1 nate nate 76 Apr 27 11:40 wc2.0_30s_prec_11.tif -> /home/nate/Documents/GIS_Files/WorldClim2/Monthly/Prec/wc2.0_30s_prec_11.tif
4 lrwxrwxrwx 1 nate nate 76 Apr 27 11:40 wc2.0_30s_prec_12.tif -> /home/nate/Documents/GIS_Files/WorldClim2/Monthly/Prec/wc2.0_30s_prec_12.tif

nate@auerilas ~ $ cd ~/Desktop/test_ln/Prec
nate@auerilas ~/Desktop/test_ln/Prec $ ls -ls
total 1030608
    8 -rw-rw-r-- 1 nate nate      256 Jul  4  2016 readme.txt
74828 -rw-rw-r-- 1 nate nate 76618603 Jun 30  2016 wc2.0_30s_prec_01.tif
76620 -rw-rw-r-- 1 nate nate 78453928 Jun 30  2016 wc2.0_30s_prec_02.tif
86664 -rw-rw-r-- 1 nate nate 88738160 Jun 30  2016 wc2.0_30s_prec_03.tif
89424 -rw-rw-r-- 1 nate nate 91564709 Jun 30  2016 wc2.0_30s_prec_04.tif
88272 -rw-rw-r-- 1 nate nate 90386408 Jun 30  2016 wc2.0_30s_prec_05.tif
90364 -rw-rw-r-- 1 nate nate 92524861 Jun 30  2016 wc2.0_30s_prec_06.tif
92252 -rw-rw-r-- 1 nate nate 94459125 Jun 30  2016 wc2.0_30s_prec_07.tif
94700 -rw-rw-r-- 1 nate nate 96964870 Jun 30  2016 wc2.0_30s_prec_08.tif
88708 -rw-rw-r-- 1 nate nate 90829431 Jun 30  2016 wc2.0_30s_prec_09.tif
92756 -rw-rw-r-- 1 nate nate 94977702 Jun 30  2016 wc2.0_30s_prec_10.tif
79808 -rw-rw-r-- 1 nate nate 81716504 Jun 30  2016 wc2.0_30s_prec_11.tif
76204 -rw-rw-r-- 1 nate nate 78025342 Jun 30  2016 wc2.0_30s_prec_12.tif

Note the difference in file sizes between the two methods, and the lack of a link for the files made by ln -s. If I go into my file browser and right click to look at the properties, I also see something different.

ln -s

Type Image
Size 76.6 MB
Location /home/nate/Desktop/test_ln/Prec

cp -sr

Type Link to Image
Link target /home/nate/Documents/GIS_File..nthly/Prec/wc2.0...
Size 76.6 MB
Location /home/nate/Desktop/test_cp/Prec

So cp -sr seems to be actually linking each file in a directory, whereas ln -s seems to link the top folder only and copying the files within the folder, thus duplicating files.

Is that correct?

The problem is that if I use cp -sr, only the files themselves are linked, not the top folder. So if I add or delete a file on my harddrive, the change isn’t then copied to Dropbox

Running Linux Mint 18.3

Advertisement

Answer

You’re correct. Here’s info cp explicitly saying that it only links non-directories:

‘-s’
‘--symbolic-link’
     Make symbolic links instead of copies of non-directories.
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement