一、命令详解
1.命令说明
ls
命令就是list
的缩写,用来打印出当前目录的清单。如果ls指定其他目录,那么就会显示指定目录里的文件及文件夹清单。 通过ls 命令不仅可以查看linux文件夹包含的文件,而且可以查看文件权限(包括目录、文件夹、文件权限)查看目录信息等等。
2.语法格式
ls [option] [file]
ls [选项] [文件或目录]
3.选项描述
DESCRIPTION
-a, --all do not ignore entries starting with .
#等同于 –all, 列出目录下的所有文件,包括以 . 开头的隐藏文件。
-A, --almost-all do not list implied . and ..
--author with -l, print the author of each file
#显示指定目录下所有子目录与文件,包括隐藏文件。但不列出“.”和 “..”。
-b, --escape print C-style escapes for nongraphic characters
#对文件名中的不可显示字符用八进制逃逸字符显示。
# 块以指定<大小>的字节为单位
-B, --ignore-backups do not list implied entries ending with ~
#忽略备份文件和目录,即不列出任何以"~"字符结束的项目。
-c with -lt: sort by, and show, ctime
(time of last modification of file status information);
with -l: show ctime and sort by name;
otherwise: sort by ctime, newest first
# 输出结果以文件的 ctime (文件状态最后更改的时间) 排序。
-C list entries by columns
#分成多列的顺序输出文件列表;
--color[=WHEN] colorize the output; WHEN can be 'never', 'auto',
or 'always' (the default); more info below
#是否使用不同的颜色区分文件类型,可能的值:
never:从不;
always:总是;
auto:自动。
例如:ls is aliased to ls `--color=auto`
-d, --directory list directories themselves, not their contents
#显示目录名称而非其内容。
-D, --dired generate output designed for Emacs dired mode
#用Emacs的模式设计产生文件和目录列表。
-f do not sort, enable -aU, disable -ls --color
#对输出的文件不进行排序,-aU 选项生效,-lst 选项失效。
-F, --classify append indicator (one of */=>@|) to entries
#附加文件类型的指示符号 (*/=@| 其中一个) 在目录名后面标记“/”,可执行文件后面标记“*”,符号链接后面标记 “@”,管道(或FIFO)后面标记“|”,socket文件后面标记“=”。
-g like -l, but do not list owner
#类似 -l,但不列出所有者。
-G, --no-group in a long listing, don’t print group names
#不列出任何有关组的信息
-h, --human-readable with -l, print sizes in human readable format
#与 -l 配合使用,如 ls -lh以容易理解的格式列出文件大小,大小加单位,如 10k、20M、100G。
-H, --dereference-command-line
follow symbolic links listed on the command line
#使用命令列中的符号链接指示的 真正目的地
-i, --inode print the index number of each file
#打印出每个文件的 inode 号
-I, --ignore=PATTERN do not list implied entries matching shell PATTERN
# 不列出符合范本样式的文件或目录名称。
-k, --kibibytes default to 1024-byte blocks for disk usage
#此参数的效果和指定"block-size=1024"参数相同。
-l use a long listing format
#使用详细格式列表,除了文件名之外,还将文件的权限、所有者、文件大小等信息详细列出来。ls -l 等于 ll
-L, --dereference when showing file information for a symbolic
link, show information for the file the link
# 当显示符号链接的文件信息时,显示符号链接所指示的对象而并非符号链接本身的信息
-m fill width with a comma separated list of entries
#用","号区隔每个文件和目录的名称。
-n, --numeric-uid-gid like -l, but list numeric user and group IDs
#以用户识别码和群组识别码替代其名称。
-N, --literal print raw entry names (don’t treat e.g. control
#接列出文件和目录名称,包括控制字符。
characters specially)
-o like -l, but do not list group information
# 类似 -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, --quote-name enclose entry names in double quotes
--quoting-style=WORD use quoting style WORD for entry names:
literal, locale, shell, shell-always, c, escape
#把文件和目录名称以""号标示起来。
-r, --reverse reverse order while sorting
#依相反次序排列-倒叙
-R, --recursive list subdirectories recursively
#递归处理,将指定目录下的所有文件及子目录一并处理。
-s, --size print the allocated size of each file, in blocks
# 以块大小为单位列出所有文件的大小
-S sort by file size #根据文件大小排序
-t sort by modification time, newest first
#用文件和目录的更改时间排序
-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;
#以最后存取时间排序,显示文件和目录。
#同-lt 一起使用:按照访问时间排序并显示。
#同-l一起使用:显示访问时间并按文件名排序。
#其他:按照访问时间排序。
otherwise: sort by access time
-U do not sort; list entries in directory order
#不进行排序;依文件系统原有的次序列出项目
-v natural sort of (version) numbers within text
#根据版本进行排序
-w, --width=COLS assume screen width instead of current value
#设置每列的最大字符数。
-x list entries by lines instead of by columns
# 逐行列出项目而不是逐栏列出
-X sort alphabetically by entry extension
#根据扩展名排序
-1 list one file per line。
#每行只列出一个文件
二、命令示例
注意:
ls -l
的别名是ll
,常用ll
再加别的参数。
案例1:查看当前目录下的文件和目录#ls
[root@centos7 ~]#ls
d3 Documents f[1-9][a-b] f333 Pictures Templates
Desktop Downloads f{1-9}{a-b} Music Public Videos
案例2:查看文件列表#ll
[root@centos7 app]#ll
total 32
drwxr-xr-x. 2 root root 6 Dec 8 13:53 dir1
-rw-r--r--. 1 root root 19 Dec 12 17:32 f1
lrwxrwxrwx. 1 root root 2 Dec 7 21:57 f1_softlink -> f1
[root@centos7 app]#type ll
ll is aliased to `ls -l --color=auto'
案例3:按照修改时间排序#ll-t
[root@centos7 app]#ll -t
total 32
-rw-r--r--. 2 root root 4 Dec 13 10:47 f2
-rw-r--r--. 2 root root 4 Dec 13 10:47 f2_hardlink
-rw-r--r--. 1 root root 19 Dec 12 17:32 f1
-rwxrwxrwx. 1 root root 1703 Dec 11 21:14 merrys
[root@centos7 app]#ls -lt
total 32
-rw-r--r--. 2 root root 4 Dec 13 10:47 f2
-rw-r--r--. 2 root root 4 Dec 13 10:47 f2_hardlink
-rw-r--r--. 1 root root 19 Dec 12 17:32 f1
-rwxrwxrwx. 1 root root 1703 Dec 11 21:14 merrys
案例4:按照时间倒叙排列#ll -tr
[root@centos7 app]#ll -tr
total 32
-rw-r--r--. 1 root root 329 Nov 17 22:34 ls.log
drwx------. 3 tom tom 78 Nov 20 18:24 tomhome
lrwxrwxrwx. 1 root root 2 Dec 7 21:57 f1_softlink -> f1
prw-r--r--. 1 root root 0 Dec 7 23:14 fifo_file
drwxr-xr-x. 2 root root 6 Dec 8 13:53 dir1
-rw-r--r--. 1 root root 88 Dec 8 22:25 fi
-rw-r--r--. 1 root root 88 Dec 8 22:39 fo
-rw-r--r--. 1 root root 114 Dec 9 14:54 f4
-rwxrwxrwx. 1 root root 1703 Dec 11 21:14 merrys
-rw-r--r--. 1 root root 19 Dec 12 17:32 f1
-rw-r--r--. 2 root root 4 Dec 13 10:47 f2_hardlink
-rw-r--r--. 2 root root 4 Dec 13 10:47 f2
案例5:按照文件大小排列#ll -S
[root@centos7 app]#ll -S
total 32
-rwxrwxrwx. 1 root root 1703 Dec 11 21:14 merrys
-rw-r--r--. 1 root root 329 Nov 17 22:34 ls.log
-rw-r--r--. 1 root root 114 Dec 9 14:54 f4
-rw-r--r--. 1 root root 88 Dec 8 22:25 fi
-rw-r--r--. 1 root root 88 Dec 8 22:39 fo
drwx------. 3 tom tom 78 Nov 20 18:24 tomhome
-rw-r--r--. 1 root root 19 Dec 12 17:32 f1
drwxr-xr-x. 2 root root 6 Dec 8 13:53 dir1
-rw-r--r--. 2 root root 4 Dec 13 10:47 f2
-rw-r--r--. 2 root root 4 Dec 13 10:47 f2_hardlink
lrwxrwxrwx. 1 root root 2 Dec 7 21:57 f1_softlink -> f1
prw-r--r--. 1 root root 0 Dec 7 23:14 fifo_file
案例6:按照比较方便阅读方式显示文件大小#ll -hS
-h
给大小加单位,S
是按文件大小排列,更容易方便阅读。
[root@centos7 app]#ll -hS
total 32K
-rwxrwxrwx. 1 root root 1.7K Dec 11 21:14 merrys
-rw-r--r--. 1 root root 329 Nov 17 22:34 ls.log
-rw-r--r--. 1 root root 114 Dec 9 14:54 f4
-rw-r--r--. 1 root root 88 Dec 8 22:25 fi
案例7:结尾处显示文件类型#ll -F
给文件名末尾添加一个标识符(/=@|中的一个):
“*”:可执行文件;
“/”:目录;
“=”:socket文件;
“@”:符号链接(symbolic link)文件;
“|”:FIFOs文件;
[root@centos7 app]#ll -F
total 32
drwxr-xr-x. 2 root root 6 Dec 8 13:53 dir1/
-rw-r--r--. 1 root root 19 Dec 12 17:32 f1
lrwxrwxrwx. 1 root root 2 Dec 7 21:57 f1_softlink -> f1
-rw-r--r--. 2 root root 4 Dec 13 10:47 f2
-rw-r--r--. 2 root root 4 Dec 13 10:47 f2_hardlink
-rw-r--r--. 1 root root 114 Dec 9 14:54 f4
-rw-r--r--. 1 root root 88 Dec 8 22:25 fi
prw-r--r--. 1 root root 0 Dec 7 23:14 fifo_file|
-rw-r--r--. 1 root root 88 Dec 8 22:39 fo
-rw-r--r--. 1 root root 329 Nov 17 22:34 ls.log
-rwxrwxrwx. 1 root root 1703 Dec 11 21:14 merrys*
drwx------. 3 tom tom 78 Nov 20 18:24 tomhome/
案例8:递归列出指定文件路径#ls -lR dir1
ls -l -R dir1
,在这条命令中,使用到了两个参数,分别为“l”和“R”,可以把他们放在一起使用,ls -lR dir1
,最简单的为ll -R dir1
[root@centos7 app]#ls -lR dir1
dir1:
total 0
drwxr-xr-x. 3 root root 18 Dec 13 11:15 dir2
dir1/dir2:
total 0
drwxr-xr-x. 3 root root 18 Dec 13 11:15 dir3
dir1/dir2/dir3:
total 0
drwxr-xr-x. 3 root root 18 Dec 13 11:15 dir4
案例9:计算当前目录下的文件数和目录数
[root@centos7 app]#ls -l * |grep "^-"|wc -l
8
[root@centos7 app]#ls -l * |grep "^d"|wc -l
2