我不知道是否ls
能够显示文件类型列。
进行快速的在线搜索和搜索man
并没有发现这种能力。它有能力做到这一点吗?
说明:如果文件没有扩展名,这尤其有用。
更新
截至 2018 年 4 月 28 日,答案非常有趣,但它们没有提供我想要的。进一步来说,
file
确实提供了实际的文件类型,但它与ls
. 具体来说,我使用ls -lhtrpG --group-directories-first --color=always
ls -F
是一种ls
解决方案,但它提供的符号不是实际文件类型的列。
出于这个原因,我没有将任何答案标记为我正在寻找的答案。
sin*_*ium 22
我认为显示文件类型的最佳方法是使用以下命令:
file <filename>
Run Code Online (Sandbox Code Playgroud)
如果要列出目录中所有文件的类型,只需使用:
file *
Run Code Online (Sandbox Code Playgroud)
有关参数的更多详细信息,请参阅:
man file
Run Code Online (Sandbox Code Playgroud)
des*_*ert 19
file
绝对是获取所需文件类型信息的正确选择。要将其输出与ls
我建议使用的输出相结合find
:
find -maxdepth 1 -type f -ls -exec file -b {} \;
Run Code Online (Sandbox Code Playgroud)
这将查找当前目录中的每个文件,并打印 的输出ls -dils
以及它的输出file -b
,每个文件都在自己的行上。示例输出:
2757145 4 -rw-rw-r-- 1 dessert dessert 914 Apr 26 14:02 ./some.html
HTML document, ASCII text
2757135 4 -rw-rw-r-- 1 dessert dessert 201 Apr 13 15:26 ./a_text_file
UTF-8 Unicode text, with CRLF, LF line terminators
Run Code Online (Sandbox Code Playgroud)
但是,由于您不需要 filetype line而是 filetype column,因此这里有一种摆脱行之间换行符的方法:
2757145 4 -rw-rw-r-- 1 dessert dessert 914 Apr 26 14:02 ./some.html
HTML document, ASCII text
2757135 4 -rw-rw-r-- 1 dessert dessert 201 Apr 13 15:26 ./a_text_file
UTF-8 Unicode text, with CRLF, LF line terminators
Run Code Online (Sandbox Code Playgroud)
示例输出:
-rw-rw-r-- 1 dessert dessert 914 Apr 26 14:02 ./some.html HTML document, ASCII text
-rw-rw-r-- 1 dessert dessert 201 Apr 13 15:26 ./a_text_file UTF-8 Unicode text, with CRLF, LF line terminators
Run Code Online (Sandbox Code Playgroud)
那个新列很长,所以让我们从第一个逗号中删除所有内容:
find -maxdepth 1 -type f -exec sh -c "ls -l {} | tr '\n' '\t'; file -b {}" \;
Run Code Online (Sandbox Code Playgroud)
其输出如下所示:
-rw-rw-r-- 1 dessert dessert 914 Apr 26 14:02 ./some.html HTML document
-rw-rw-r-- 1 dessert dessert 201 Apr 13 15:26 ./a_text_file UTF-8 Unicode text
Run Code Online (Sandbox Code Playgroud)
这不是很方便,那么一个alias
? 使用~/.bash_aliases
文件中的以下行,您只需要运行lsf
即可获取当前目录的上述输出。
-rw-rw-r-- 1 dessert dessert 914 Apr 26 14:02 ./some.html HTML document, ASCII text
-rw-rw-r-- 1 dessert dessert 201 Apr 13 15:26 ./a_text_file UTF-8 Unicode text, with CRLF, LF line terminators
Run Code Online (Sandbox Code Playgroud)
为清楚起见,我将指出您可以使用 来查看基本意义上的文件类型ls
,使用-F
标志(分类)根据文件类型将符号附加到文件名:
‘-F’
‘--classify’
‘--indicator-style=classify’
Append a character to each file name indicating the file type.
Also, for regular files that are executable, append ‘*’. The file
type indicators are ‘/’ for directories, ‘@’ for symbolic links,
‘|’ for FIFOs, ‘=’ for sockets, ‘>’ for doors, and nothing for
regular files.
Run Code Online (Sandbox Code Playgroud)
不过,您可以看到,在 输出的第一个字母中显示的信息稍微不那么神秘ls -l
。wjandrea 的回答更详细地描述了这一点。
但我不认为这就是您所说的文件类型。该ls
命令不会查看常规文件内部 - 仅查看目录列表(存储文件名)和 inode(存储元数据,包括前面提到的“类型”)。
因此,该ls
命令无法从 JPG 图像、二进制文件、文本文件或某种 LibreOffice 文档的意义上显示文件类型,因为它没有这些信息。
为此,正如singrium 的回答指出的那样,您需要该file
命令,该命令查看文件内容的前 50-100kB 左右以确定它们的类型。
文件类型的一种形式是文件是否是常规文件、目录、设备、符号链接等。ls
可以使用选项-l
或-F
.
选项-l
将在列表的开头将文件类型显示为单个字符,例如:
$ ls -l
drwxrwxr-x 2 user user 4096 Dec 11 00:18 DIR
-rw-rw-r-- 1 user user 0 Dec 11 00:18 FILE
lrwxrwxrwx 1 user user 4 Dec 11 00:19 LINK -> FILE
Run Code Online (Sandbox Code Playgroud)
其中-
是常规文件,d
是目录,l
是符号链接。
选项-F
将文件类型显示为后缀,例如:
$ ls -F
DIR/ FILE LINK@
Run Code Online (Sandbox Code Playgroud)
没有后缀的地方是一个普通文件,/
一个目录,@
一个符号链接。
有关这些的更多信息可在 中找到info coreutils 'ls invocation'
,总结如下:
对于-l
:
‘-’ regular file
‘b’ block special file
‘c’ character special file
‘C’ high performance (“contiguous data”) file
‘d’ directory
‘D’ door (Solaris 2.5 and up)
‘l’ symbolic link
‘M’ off-line (“migrated”) file (Cray DMF)
‘n’ network special file (HP-UX)
‘p’ FIFO (named pipe)
‘P’ port (Solaris 10 and up)
‘s’ socket
‘?’ some other file type
Run Code Online (Sandbox Code Playgroud)
对于-F
:
nothing for regular files
‘*’ regular files that are executable
‘/’ directories
‘@’ symbolic links
‘|’ FIFOs
‘=’ sockets
‘>’ doors
Run Code Online (Sandbox Code Playgroud)