用户工具

站点工具


man1:ls

ls 命令

Linux ls命令用于列出文件写信息(默认是当前工作目录),如果制定了特定的排序选项,将会对信息进行排序。

<!-- more -->

用法

ls [选项] [文件名...]
   [-1abcdfgiklmnopqrstuxABCDFGLNQRSUX] [-w cols] [-T cols] [-I pattern] [--full-time] 
   [--format={long,verbose,commas,across,vertical,single-col‐umn}] 
   [--sort={none,time,size,extension}] [--time={atime,access,use,ctime,status}] 
   [--color[={none,auto,always}]] [--help] [--version] [--]

选项

选项 说明
-a, –all 显示所有文件及目录 (包括以“.”开头的隐藏文件)
-A, –almost-all 显示所有文件(排除 ...)
–author -l一起使用,显示文件作者
-b, –escape 把文件名中不可输出的字符用反斜杠加字符编号(就象在 C 语言里一样)的形式列出。
–block-size=SIZE -l一起使用,显示文件大小是使用SIZE进行缩放
–B, –ignore-backups 不显示以~结尾的文件
-c 使用“状态改变时间”代替“文件修改时间”为依据来排序(使用“-t”选项时)或列出(使用“-l”选项时)。
-C 多列输出,纵向排序
–color[=WHRN] 指定是否使用颜色区别文件类别。环境变量 LS_COLORS 指定使用的颜色。 when 可以被省略,或是以下几项之一:none (不使用颜色,这是缺省项); auto 仅当标准输出是终端时使用。 always 总是使用颜色。指定 –color 而且省略 when 时就等同于 –color=always 。
-d, –direnctory 将目录名象其它文件一样列出,而不是列出它们的内容。
-D, –dired 生成Emacs的 Dired 模式信息
-f 不排序目录内容;按它们在磁盘上存储的顺序列出。同时启动“ -a ”选项,如果在“ -f ”之前存在“-l#”、“ –color ”或“ -s ”,则禁止它们。
-F, –classify 每个文件名后附上一个字符以说明该文件的类型。“ * ”表示普通的可执行文件; “ / ”表示目录;“ @ ”表示符号链接;“
–file-type 同 -F, 但是不添加 *
–format=WORD across -x, commas -m, horizontal -x, long -l, single-column -1, verbose -l, vertical -C
–full-time tong -l --time-style=full-iso
-g -l, 不列出所有者
–group-directories-first group directories before files
-G,–no-group 不列出组名
-h, –human-readable 显示人性化的文件大小
–si 使用1000代替 1024
-H, –dereference-command-line 跟随符号链接
–dereference-command-line-symlink-to-dir跟随指向目录的符号链接
–hide=PATTEN 不显示匹配模式的文件(被-A,-a覆盖)
–hyperlink[=when] hyperlink file names; WHEN can be ‘always’ (默认值), ‘auto’, or ‘never’
–indicator-style=WORD append indicator with style WORD to entry names: none (default), slash (-p), file-type (–file-type), classify (-F)
-i , –inode 显示文件的inode号
-I, –ignore=PATTERN 不显示模式匹配的项
-k, –kibibytes default to 1024-byte blocks for disk usage; used only with -s and per directory totals
-l 使用长格式
-L, –dereference 跟随符号链接,而不是本身
-m ,作为分隔符
-n, –numeric-uid-gid -l,但是列出 uid、gid
-N, –literal 名字加上引号
-o -l, 但是不显示组信息
-p, –indicator-style= slash append / indicator to directories
-q, –hide-control-chars print ? instead of nongraphic characters
–show-control-chars show nongraphic characters as-is (the default, unless program is ‘ls’ and output is a terminal)
-Q, –quto-name 用双引号引起文件名,非打印字符以 C 语言的方法表示
–quoting-style=WORD use quoting style WORD for entry names: literal, locale, shell, shell-always, shell-escape, shell-escape-always, c, escape (overrides QUOTING_STYLE environment variable)
-r, –reverse 排序翻转
-R, –rscusive 递归子目录
-s, –size 显示分配给文件的尺寸(in blocks)
-S 以文件尺寸进行排序,大文件在前
–sort=word sort by WORD instead of name: none (-U), size (-S), time (-t), version (-v), extension (-X)
–time=word with -l, show time as WORD instead of default modification time: atime or access or use (-u); ctime or status (-c); also use specified time as sort key if –sort=time (newest first)
–time-style=TIME_STYLE time/date format with -l; see TIME_STYLE below
-t 以文件修改时间排序,最新的文件在前
-T, –tabsize=COLS assume tab stops at each COLS instead of 8
-u with -lt: sort by, and show, access time; with -l: show access time and sort by name; otherwise: sort by access time, newest first
-U 不排序
-v natural sort of (version) numbers within text
-w, –width=COLS set output width to COLS. 0 means no limit
-x list entries by lines instead of by columns
-X sort alphabetically by entry extension
-Z, –context print any security context of each file
-1 list one file per line. Avoid ‘’ with -q or -b
–help display this help and exit
–version output version information and exit

The SIZE argument is an integer and optional unit (example: 10K is 10*1024). Units are K,M,G,T,P,E,Z,Y (powers of 1024) or KB,MB,… (powers of 1000). Binary prefixes can be used, too: KiB=K, MiB=M, and so on.

The TIME_STYLE argument can be full-iso, long-iso, iso, locale, or +FORMAT. FORMAT is interpreted like in date(1). If FORMAT is FORMAT1<newline>FORMAT2, then FORMAT1 applies to non-recent files and FORMAT2 to recent files. TIME_STYLE prefixed with ‘posix-’ takes effect only outside the POSIX locale. Also the TIME_STYLE environment variable sets the default style to use.

Using color to distinguish file types is disabled both by default and with –color=never. With –color=auto, ls emits color codes only when standard output is connected to a terminal. The LS_COLORS environment variable can change the settings. Use the dircolors command to set it.

退出

  • 0 成功
  • 1 出现小问题(如无法访问子目录)
  • 2 出现严重问题(如无法访问命令行参数)

实例

$ ls       # 仅列出当前目录可见文件
$ ls -l    # 列出当前目录可见文件详细信息
$ ls -hl   # 列出详细信息并以可读大小显示文件大小
$ ls -al   # 列出所有文件(包括隐藏)的详细信息

显示当前目录下包括影藏文件在内的所有文件列表

[root@localhost ~]# ls -a
.   anaconda-ks.cfg  .bash_logout   .bashrc  install.log         .mysql_history  satools  .tcshrc   .vimrc
..  .bash_history    .bash_profile  .cshrc   install.log.syslog  .rnd            .ssh     .viminfo

输出长格式列表

[root@localhost ~]# ls -1

anaconda-ks.cfg
install.log
install.log.syslog
satools

显示文件的inode信息

索引节点(index inode简称为“inode”)是Linux中一个特殊的概念,具有相同的索引节点号的两个文本本质上是同一个文件(除文件名不同外)。

[root@localhost ~]# ls -i -l anaconda-ks.cfg install.log
2345481 -rw------- 1 root root   859 Jun 11 22:49 anaconda-ks.cfg
2345474 -rw-r--r-- 1 root root 13837 Jun 11 22:49 install.log

水平输出文件列表

[root@localhost /]# ls -m

bin, boot, data, dev, etc, home, lib, lost+found, media, misc, mnt, opt, proc, root, sbin, selinux, srv, sys, tmp, usr, var

修改最后一次编辑的文件

最近修改的文件显示在最上面。

[root@localhost /]# ls -t

tmp  root  etc  dev  lib  boot  sys  proc  data  home  bin  sbin  usr  var  lost+found  media  mnt  opt  selinux  srv  misc

显示递归文件

[root@localhost ~]# ls -R
.:
anaconda-ks.cfg  install.log  install.log.syslog  satools

./satools:
black.txt  freemem.sh  iptables.sh  lnmp.sh  mysql  php502_check.sh  ssh_safe.sh

打印文件的UID和GID

[root@localhost /]# ls -n

total 254
drwxr-xr-x   2 0 0  4096 Jun 12 04:03 bin
drwxr-xr-x   4 0 0  1024 Jun 15 14:45 boot
drwxr-xr-x   6 0 0  4096 Jun 12 10:26 data
drwxr-xr-x  10 0 0  3520 Sep 26 15:38 dev
drwxr-xr-x  75 0 0  4096 Oct 16 04:02 etc
drwxr-xr-x   4 0 0  4096 Jun 12 10:26 home
drwxr-xr-x  14 0 0 12288 Jun 16 04:02 lib
drwx------   2 0 0 16384 Jun 11 22:46 lost+found
drwxr-xr-x   2 0 0  4096 May 11  2011 media
drwxr-xr-x   2 0 0  4096 Nov  8  2010 misc
drwxr-xr-x   2 0 0  4096 May 11  2011 mnt
drwxr-xr-x   2 0 0  4096 May 11  2011 opt
dr-xr-xr-x 232 0 0     0 Jun 15 11:04 proc
drwxr-x---   4 0 0  4096 Oct 15 14:43 root
drwxr-xr-x   2 0 0 12288 Jun 12 04:03 sbin
drwxr-xr-x   2 0 0  4096 May 11  2011 selinux
drwxr-xr-x   2 0 0  4096 May 11  2011 srv
drwxr-xr-x  11 0 0     0 Jun 15 11:04 sys
drwxrwxrwt   3 0 0 98304 Oct 16 08:45 tmp
drwxr-xr-x  13 0 0  4096 Jun 11 23:38 usr
drwxr-xr-x  19 0 0  4096 Jun 11 23:38 var

列出文件和文件夹的详细信息

[root@localhost /]# ls -l

total 254
drwxr-xr-x   2 root root  4096 Jun 12 04:03 bin
drwxr-xr-x   4 root root  1024 Jun 15 14:45 boot
drwxr-xr-x   6 root root  4096 Jun 12 10:26 data
drwxr-xr-x  10 root root  3520 Sep 26 15:38 dev
drwxr-xr-x  75 root root  4096 Oct 16 04:02 etc
drwxr-xr-x   4 root root  4096 Jun 12 10:26 home
drwxr-xr-x  14 root root 12288 Jun 16 04:02 lib
drwx------   2 root root 16384 Jun 11 22:46 lost+found
drwxr-xr-x   2 root root  4096 May 11  2011 media
drwxr-xr-x   2 root root  4096 Nov  8  2010 misc
drwxr-xr-x   2 root root  4096 May 11  2011 mnt
drwxr-xr-x   2 root root  4096 May 11  2011 opt
dr-xr-xr-x 232 root root     0 Jun 15 11:04 proc
drwxr-x---   4 root root  4096 Oct 15 14:43 root
drwxr-xr-x   2 root root 12288 Jun 12 04:03 sbin
drwxr-xr-x   2 root root  4096 May 11  2011 selinux
drwxr-xr-x   2 root root  4096 May 11  2011 srv
drwxr-xr-x  11 root root     0 Jun 15 11:04 sys
drwxrwxrwt   3 root root 98304 Oct 16 08:48 tmp
drwxr-xr-x  13 root root  4096 Jun 11 23:38 usr
drwxr-xr-x  19 root root  4096 Jun 11 23:38 var

列出可读文件和文件夹详细信息

[root@localhost /]# ls -lh

total 254K
drwxr-xr-x   2 root root 4.0K Jun 12 04:03 bin
drwxr-xr-x   4 root root 1.0K Jun 15 14:45 boot
drwxr-xr-x   6 root root 4.0K Jun 12 10:26 data
drwxr-xr-x  10 root root 3.5K Sep 26 15:38 dev
drwxr-xr-x  75 root root 4.0K Oct 16 04:02 etc
drwxr-xr-x   4 root root 4.0K Jun 12 10:26 home
drwxr-xr-x  14 root root  12K Jun 16 04:02 lib
drwx------   2 root root  16K Jun 11 22:46 lost+found
drwxr-xr-x   2 root root 4.0K May 11  2011 media
drwxr-xr-x   2 root root 4.0K Nov  8  2010 misc
drwxr-xr-x   2 root root 4.0K May 11  2011 mnt
drwxr-xr-x   2 root root 4.0K May 11  2011 opt
dr-xr-xr-x 235 root root    0 Jun 15 11:04 proc
drwxr-x---   4 root root 4.0K Oct 15 14:43 root
drwxr-xr-x   2 root root  12K Jun 12 04:03 sbin
drwxr-xr-x   2 root root 4.0K May 11  2011 selinux
drwxr-xr-x   2 root root 4.0K May 11  2011 srv
drwxr-xr-x  11 root root    0 Jun 15 11:04 sys
drwxrwxrwt   3 root root  96K Oct 16 08:49 tmp
drwxr-xr-x  13 root root 4.0K Jun 11 23:38 usr
drwxr-xr-x  19 root root 4.0K Jun 11 23:38 var

显示文件夹信息

[root@localhost /]# ls -ld /etc/

drwxr-xr-x 75 root root 4096 Oct 16 04:02 /etc/

按时间列出文件和文件夹详细信息

[root@localhost /]# ls -lt

total 254
drwxrwxrwt   3 root root 98304 Oct 16 08:53 tmp
drwxr-xr-x  75 root root  4096 Oct 16 04:02 etc
drwxr-x---   4 root root  4096 Oct 15 14:43 root
drwxr-xr-x  10 root root  3520 Sep 26 15:38 dev
drwxr-xr-x  14 root root 12288 Jun 16 04:02 lib
drwxr-xr-x   4 root root  1024 Jun 15 14:45 boot
drwxr-xr-x  11 root root     0 Jun 15 11:04 sys
dr-xr-xr-x 232 root root     0 Jun 15 11:04 proc
drwxr-xr-x   6 root root  4096 Jun 12 10:26 data
drwxr-xr-x   4 root root  4096 Jun 12 10:26 home
drwxr-xr-x   2 root root  4096 Jun 12 04:03 bin
drwxr-xr-x   2 root root 12288 Jun 12 04:03 sbin
drwxr-xr-x  13 root root  4096 Jun 11 23:38 usr
drwxr-xr-x  19 root root  4096 Jun 11 23:38 var
drwx------   2 root root 16384 Jun 11 22:46 lost+found
drwxr-xr-x   2 root root  4096 May 11  2011 media
drwxr-xr-x   2 root root  4096 May 11  2011 mnt
drwxr-xr-x   2 root root  4096 May 11  2011 opt
drwxr-xr-x   2 root root  4096 May 11  2011 selinux
drwxr-xr-x   2 root root  4096 May 11  2011 srv
drwxr-xr-x   2 root root  4096 Nov  8  2010 misc

按修改时间列出文件和文件夹详细信息

[root@localhost /]# ls -ltr

total 254
drwxr-xr-x   2 root root  4096 Nov  8  2010 misc
drwxr-xr-x   2 root root  4096 May 11  2011 srv
drwxr-xr-x   2 root root  4096 May 11  2011 selinux
drwxr-xr-x   2 root root  4096 May 11  2011 opt
drwxr-xr-x   2 root root  4096 May 11  2011 mnt
drwxr-xr-x   2 root root  4096 May 11  2011 media
drwx------   2 root root 16384 Jun 11 22:46 lost+found
drwxr-xr-x  19 root root  4096 Jun 11 23:38 var
drwxr-xr-x  13 root root  4096 Jun 11 23:38 usr
drwxr-xr-x   2 root root 12288 Jun 12 04:03 sbin
drwxr-xr-x   2 root root  4096 Jun 12 04:03 bin
drwxr-xr-x   4 root root  4096 Jun 12 10:26 home
drwxr-xr-x   6 root root  4096 Jun 12 10:26 data
dr-xr-xr-x 232 root root     0 Jun 15 11:04 proc
drwxr-xr-x  11 root root     0 Jun 15 11:04 sys
drwxr-xr-x   4 root root  1024 Jun 15 14:45 boot
drwxr-xr-x  14 root root 12288 Jun 16 04:02 lib
drwxr-xr-x  10 root root  3520 Sep 26 15:38 dev
drwxr-x---   4 root root  4096 Oct 15 14:43 root
drwxr-xr-x  75 root root  4096 Oct 16 04:02 etc
drwxrwxrwt   3 root root 98304 Oct 16 08:54 tmp

按照特殊字符对文件进行分类

[root@localhost nginx-1.2.1]# ls -F

auto/  CHANGES  CHANGES.ru  conf/  configure*  contrib/  html/  LICENSE  Makefile  man/  objs/  README  src/

列出文件并标记颜色分类

[root@localhost nginx-1.2.1]# ls --color=auto

auto  CHANGES  CHANGES.ru  conf  configure  contrib  html  LICENSE  Makefile  man  objs  README  src
man1/ls.txt · 最后更改: 2021/06/29 13:34 (外部编辑)