Cor*_*ein 71 directory slash filenames
我在 linux 中使用的大多数命令的行为都完全相同,无论我是否/在目录名称的末尾包含斜杠字符。
例如:
ls /home/cklein
ls /home/cklein/
cp foo bar
cp foo/ bar/
Run Code Online (Sandbox Code Playgroud)
这个尾部斜杠什么时候重要?尾部斜杠的语义是什么?
Kei*_*son 56
一个很好的例子是将文件移动到目录中:
mv some_file foo
Run Code Online (Sandbox Code Playgroud)
对比
mv some_file foo/
Run Code Online (Sandbox Code Playgroud)
如果foo不存在,第一个将重命名some_file为foo,而不是预期的foo/some_file; 第二个会抱怨,这就是你想要的。
如果foo确实存在但不是目录,则第一个可以破坏foo文件;再次,第二个会抱怨。
cp 提出了类似的问题。
在一些旧版本的 SunOS 上,我养成了附加 的习惯/.,因为系统实际上忽略/了文件名的尾随;例如,/etc/motd/将引用文件而不是错误。SunOS / Solaris 的更高版本似乎没有这个问题。
Ign*_*ams 41
它完全依赖于工具。rm如果末尾有斜杠,则不会让您删除指向目录的符号链接,如果源文件规范末尾有斜杠,则 rsync 会做不同的事情。
Sté*_*las 16
foo/就像foo/.,所以如果foo是一个目录的符号链接,foo/是一个目录(不是一个符号链接),如果foo不是一个目录或一个目录的符号链接,那么任何试图访问的东西都会得到一个 ENOTDIR 错误foo/。这就是 Linux 上的行为。
其他系统上的行为可能会有所不同。
请参阅此处和此处以及此处以了解 POSIX/SUS 对此有何看法。
在 rsync 中,手册页内容如下:
A trailing slash on the source changes this behavior to avoid creating
an additional directory level at the destination. You can think of a
trailing / on a source as meaning "copy the contents of this directory"
as opposed to "copy the directory by name", but in both cases the
attributes of the containing directory are transferred to the contain-
ing directory on the destination. In other words, each of the follow-
ing commands copies the files in the same way, including their setting
of the attributes of /dest/foo:
rsync -av /src/foo /dest
rsync -av /src/foo/ /dest/foo
Run Code Online (Sandbox Code Playgroud)
目的地的尾部斜杠根本无关紧要。只在源头上。(然后,当然,仅当源是目录而不是单个文件或 glob 时才重要。)
为了说明目录到目录的用例:
$ mkdir foo bar baz
$ chmod 700 foo
$ chmod 750 bar
$ chmod 705 baz
$ echo hello > foo/file1
$ chmod 606 foo/file1
$ ls -n
total 0
drwxr-x--- 2 501 20 68 Aug 8 15:29 bar/
drwx---r-x 2 501 20 68 Aug 8 15:29 baz/
drwx------ 3 501 20 102 Aug 8 15:30 foo/
$ ls -n foo
total 8
-rw----rw- 1 501 20 6 Aug 8 15:30 file1
$ rsync -a foo bar
$ rsync -a foo baz/
$ rsync -a foo bif
$ rsync -a foo bonk/
$ ls -n
total 0
drwxr-x--- 3 501 20 102 Aug 8 15:30 bar/
drwx---r-x 3 501 20 102 Aug 8 15:30 baz/
drwxr-xr-x 3 501 20 102 Aug 8 15:30 bif/
drwxr-xr-x 3 501 20 102 Aug 8 15:30 bonk/
drwx------ 3 501 20 102 Aug 8 15:30 foo/
$ ls -n *
bar:
total 0
drwx------ 3 501 20 102 Aug 8 15:30 foo/
baz:
total 0
drwx------ 3 501 20 102 Aug 8 15:30 foo/
bif:
total 0
drwx------ 3 501 20 102 Aug 8 15:30 foo/
bonk:
total 0
drwx------ 3 501 20 102 Aug 8 15:30 foo/
foo:
total 8
-rw----rw- 1 501 20 6 Aug 8 15:30 file1
$ rm -rf b*
$ mkdir bar baz
$ chmod 750 bar
$ chmod 705 baz
$ rsync -a foo/ bar
$ rsync -a foo/ baz/
$ rsync -a foo/ bif
$ rsync -a foo/ bonk/
$ ls -n
total 0
drwx------ 3 501 20 102 Aug 8 15:30 bar/
drwx------ 3 501 20 102 Aug 8 15:30 baz/
drwx------ 3 501 20 102 Aug 8 15:30 bif/
drwx------ 3 501 20 102 Aug 8 15:30 bonk/
drwx------ 3 501 20 102 Aug 8 15:30 foo/
$ ls -n *
bar:
total 8
-rw----rw- 1 501 20 6 Aug 8 15:30 file1
baz:
total 8
-rw----rw- 1 501 20 6 Aug 8 15:30 file1
bif:
total 8
-rw----rw- 1 501 20 6 Aug 8 15:30 file1
bonk:
total 8
-rw----rw- 1 501 20 6 Aug 8 15:30 file1
foo:
total 8
-rw----rw- 1 501 20 6 Aug 8 15:30 file1
$
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
52701 次 |
| 最近记录: |