“ls --dired - l”或“ls -D -l”实际打印什么?

sam*_*han 3 linux ls

ls --dired -l 打印所有目录和文件以及一些数字,后跟

//DIRED// ***66 69 122 131 ....***
//DIRED-OPTIONS// --quoting-style=literal
Run Code Online (Sandbox Code Playgroud)

这些加粗的数字是什么意思?

--dired 是使用目录的 Emacs 选项,但我不明白这里的数字。

Mat*_*teo 6

的输出ls -D旨在由 Emacs 的dired mode解析。

来自GNU Coreutils 手册

‘-D’
‘--dired’
Run Code Online (Sandbox Code Playgroud)

使用长列表 ( -l) 格式,在主输出后打印额外的一行:

//DIRED// beg1 end1 beg2 end2 …
Run Code Online (Sandbox Code Playgroud)

begnendn是无符号整数该记录输出每个文件名的开头和结尾的字节位置。这使得 Emacs 可以轻松找到名称,即使它们包含不常见的字符(如空格或换行符),而无需花哨的搜索。

如果目录被递归列出 ( -R),输出一个类似的行,每个子目录名称的偏移量:

//SUBDIRED// beg1 end1 …
Run Code Online (Sandbox Code Playgroud)

最后,输出一行表格:

//DIRED-OPTIONS// --quoting-style=word
Run Code Online (Sandbox Code Playgroud)

其中 word 是引用样式(请参阅格式化文件名)。

下面是一个实际的例子:

$ mkdir -p a/sub/deeper a/sub2
$ touch a/f1 a/f2
$ touch a/sub/deeper/file
$ ls -gloRF --dired a
  a:
  total 8
  -rw-r--r-- 1    0 Jun 10 12:27 f1
  -rw-r--r-- 1    0 Jun 10 12:27 f2
  drwxr-xr-x 3 4096 Jun 10 12:27 sub/
  drwxr-xr-x 2 4096 Jun 10 12:27 sub2/

  a/sub:
  total 4
  drwxr-xr-x 2 4096 Jun 10 12:27 deeper/

  a/sub/deeper:
  total 0
  -rw-r--r-- 1 0 Jun 10 12:27 file

  a/sub2:
  total 0
//DIRED// 48 50 84 86 120 123 158 162 217 223 282 286
//SUBDIRED// 2 3 167 172 228 240 290 296
//DIRED-OPTIONS// --quoting-style=literal
Run Code Online (Sandbox Code Playgroud)

请注意,上面“//DIRED//”行上的偏移对分隔了这些名称:f1、f2、sub、sub2、deep、file。'//SUBDIRED//' 行上的偏移量分隔以下目录名称:a, a/sub, a/sub/deeper, a/sub2.

以下是如何提取与deeper偏移量对 222 和 228 对应的第五个条目名称 的示例:

$ ls -gloRF --dired a > out
$ dd bs=1 skip=222 count=6 < out 2>/dev/null; echo
deeper
Run Code Online (Sandbox Code Playgroud)

请注意,虽然上面的列表包含deeper条目的尾部斜杠,但偏移量选择没有尾部斜杠的名称。但是,如果您ls与(aka ) 之--dired类的选项一起调用并对名称包含特殊字符的文件进行操作,请注意包含反斜杠:--escape-b

$ touch 'a b'
$ ls -blog --dired 'a b'
  -rw-r--r-- 1 0 Jun 10 12:28 a\ b
//DIRED// 30 34
//DIRED-OPTIONS// --quoting-style=escape
Run Code Online (Sandbox Code Playgroud)

如果您使用添加引号(例如,--quoting-style=c)的引用样式,则偏移量包括引号。因此请注意,用户可能会通过环境变量选择引用样式QUOTING_STYLE。因此,使用的应用程序--dired应该在命令行上指定一个显式--quoting-style=literal选项(又名-N--literal),或者准备好解析转义的名称。

数字是文件名在输出中的位置

begn 和 endn 是无符号整数,它们记录输出中每个文件名的开头和结尾的字节位置。