linux - cp -sr 和 ln -s 之间的符号链接(symbolic link)差异

标签 linux bash unix symlink

<分区>

我正在尝试在我的 Dropbox 中创建符号链接(symbolic link),这样我的计算机上就不会出现重复的文件(文件空间有限)。我在 ln -s 和 cp -s 中遇到了一些令人困惑的行为。基本上, ln -s 似乎链接到原始文件夹,但随后将文件夹中的所有文件复制到新位置。 cp -sr 递归地链接到文件夹中的所有文件(这是所需的行为)。

例如:

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

请注意这两种方法之间的文件大小差异,以及缺少 ln -s 生成的文件的链接。如果我进入我的文件浏览器并右键单击以查看属性,我也会看到一些不同的东西。

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

所以 cp -sr 似乎实际上链接了目录中的每个文件,而 ln -s 似乎只链接了顶级文件夹并复制文件夹内的文件,从而复制了文件。

对吗?

问题是,如果我使用 cp -sr,则只会链接文件本身,而不是顶级文件夹。因此,如果我在硬盘驱动器上添加或删除文件,所做的更改不会复制到 Dropbox

运行 Linux Mint 18.3

最佳答案

你是对的。这里的 info cp 明确表示它只链接非目录:

‘-s’
‘--symbolic-link’
     Make symbolic links instead of copies of non-directories.

关于linux - cp -sr 和 ln -s 之间的符号链接(symbolic link)差异,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50067829/

相关文章:

c - 100% cpu 使用率和一个 libpcap 简单示例

c - 如何在 bash 脚本中调用 C 程序并将其返回值存储到变量中?

linux - 在 shell 脚本中格式化文本

c - 如何正确实现 alarm() 方法来杀死 fork() 创建的所有工作进程?

python - 如何使用分隔符将csv文件分割成多个文件? Python

Linux : Search for a Particular word in a List of files under a directory

linux - 自动重命名文件至今 (YY-MM-DD)

linux - 如何在 Fedora 9 和 Ubuntu 11.10 之间进行双启动

bash - 除了通常的 stdin/stdout/stderr (0, 1, 2) 之外,我们还能有更多的文件描述符吗?

unix - 从 weblogic 服务器删除临时目录