NAME(名称)
ls - list directory contents
SYNOPSIS(语法)
ls [OPTION]… [FILE]…
DESCRIPTION(描述)
List information about the FILEs (the current directory by default). Sort entries alphabetically if none of -cftuvSUX nor --sort is specified.Mandatory arguments to long options are mandatory for short options too.
列出关于文件(默认当前目录)的信息。如果没有-cftuvSUX 和 --sort没有指定选项,就按找字母顺序给条目排序。对长选项的强制参数对于短选项也是强制的。(感觉应该是长选项等价于短选项的意思 )
以下就不一一翻译了,反正英语很重要,不懂的自己查字典吧。
-a, --all
do not ignore entries starting with .
-l use a long listing format
-d, --directory
list directories themselves, not their contents
-R, --recursive
list subdirectories recursively
-S sort by file size
–sort=WORD
sort by WORD instead of name: none (-U), size (-S), time (-t), version (-v), exten‐
sion (-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
–time-style=STYLE
with -l, show times using style STYLE: full-iso, long-iso, iso, locale, or +FORMAT;
FORMAT is interpreted like in ‘date’; if FORMAT is FORMAT1FORMAT2, then
FORMAT1 applies to non-recent files and FORMAT2 to recent files; if STYLE is pre‐
fixed with ‘posix-’, STYLE takes effect only outside the POSIX locale
-t sort by modification time, newest first
-U do not sort; list entries in directory order
以上是常用选项的man帮助内容,ls命令在日常使用频繁,所以需要灵活掌握此命令。
ls命令使用语法为 ls [选项][文件] ,其中[]内为可选项,下面介绍常用命令的使用方法。
1 不加选项和参数
[root@cent7 ~]#ls
anaconda-ks.cfg Desktop Documents Downloads f3 Music Pictures Public Templates Videos
解析 没有选项和参数时列出当前文件夹下的所有非隐藏内容。
2 选项-a
[root@cent7 ~]#ls -a /root
. .bash_logout .config Documents .ICEauthority Pictures Videos
.. .bash_profile .cshrc Downloads .lesshst Public .viminfo
anaconda-ks.cfg .bashrc .dbus .esd_auth .local .tcshrc
.bash_history .cache Desktop f3 Music Templates
-a选项列出/root目录下所有文件和文件夹(包括隐藏文件)。
3 选项-d
[root@cent7 ~]# ls -d /root
/root
仅列出目录本身
[root@cent7 ~]#ls -d /root/
/root/
仅列出目录本身
[root@cent7 ~]#ls -d /root/*
/root/anaconda-ks.cfg /root/etc /root/ss.log
/root/core.2431 /root/etc2018-12-21 /root/Templates
/root/dead.letter /root/motd /root/test
/root/Desktop /root/Music /root/txt
/root/Documents /root/Pictures /root/txt2
/root/Downloads /root/Public /root/Videos
通配符*代表所有(但不包括.)
[root@cent7 ~]#ls -d /root/.*
/root/. /root/.bashrc /root/.esd_auth /root/.tcshrc
/root/.. /root/.cache /root/.ICEauthority /root/.viminfo
/root/.bash_history /root/.config /root/.lesshst /root/.vimrc
/root/.bash_logout /root/.cshrc /root/.local
/root/.bash_profile /root/.dbus /root/.ssh
.表示root文件夹下所有隐藏文件,包括上级目录…(此处的包括了. 见上例"/root/…",再次补充一下,*表示第一个字符不能为.第二个字符随意的意思,个人观点··· )
4 选项-R
[root@cent7 ~]#ls -R /root/
/root/:
anaconda-ks.cfg Desktop etc Music ss.log txt
core.2431 Documents etc2018-12-21 Pictures Templates txt2
dead.letter Downloads motd Public test Videos
/root/Desktop:
/root/Documents:
/root/Downloads:
/root/Music:
/root/Pictures:
/root/Public:
/root/Templates:
/root/Videos:
R为递归,表示列出root文件夹下以及其子文件夹子子文件夹(只要往上级文件夹数能数到root)下的所有非隐藏文件
可以自己试试[root@cent7 ~]#ls -aR /root/,观察两者不同之处。
5 选项-l和-S
[root@cent7 ~]#ls -l --sort=size /root/ 等价于 ls -lS /root/
total 5148
-rw-------. 1 root root 5246976 Dec 18 11:17 core.2431
-rw-------. 1 root root 1945 Dec 11 14:20 anaconda-ks.cfg
-rw-r--r--. 1 root root 559 Dec 19 16:08 ss.log
-rw-r--r--. 1 root root 72 Dec 20 14:47 txt
-rw-r--r--. 1 root root 22 Dec 20 14:52 txt2
-rw-------. 1 root root 18 Dec 17 20:38 dead.letter
lrwxrwxrwx. 1 root root 7 Dec 15 15:25 test -> ../date
drwxr-xr-x. 2 root root 6 Dec 12 08:53 Desktop
drwxr-xr-x. 2 root root 6 Dec 12 08:53 Documents
drwxr-xr-x. 2 root root 6 Dec 12 08:53 Downloads
drwxr-xr-x. 2 root root 6 Dec 12 08:53 Music
drwxr-xr-x. 2 root root 6 Dec 12 08:53 Pictures
drwxr-xr-x. 2 root root 6 Dec 12 08:53 Public
drwxr-xr-x. 2 root root 6 Dec 12 08:53 Templates
drwxr-xr-x. 2 root root 6 Dec 12 08:53 Videos
-rw-r--r--. 1 root root 3 Dec 21 16:18 etc
-rw-r--r--. 1 root root 0 Dec 21 16:32 etc2018-12-21
-rw-r--r--. 1 root root 0 Dec 19 15:35 motd
l为列出条目的metadata(源属性)S是按照大小排序
6 选项-t
[root@cent7 ~]#ls -lt /root/
total 5148
-rw-r--r--. 1 root root 0 Dec 21 16:32 etc2018-12-21
-rw-r--r--. 1 root root 3 Dec 21 16:18 etc
-rw-r--r--. 1 root root 22 Dec 20 14:52 txt2
-rw-r--r--. 1 root root 72 Dec 20 14:47 txt
-rw-r--r--. 1 root root 559 Dec 19 16:08 ss.log
-rw-r--r--. 1 root root 0 Dec 19 15:35 motd
-rw-------. 1 root root 5246976 Dec 18 11:17 core.2431
-rw-------. 1 root root 18 Dec 17 20:38 dead.letter
lrwxrwxrwx. 1 root root 7 Dec 15 15:25 test -> ../date
drwxr-xr-x. 2 root root 6 Dec 12 08:53 Desktop
drwxr-xr-x. 2 root root 6 Dec 12 08:53 Documents
drwxr-xr-x. 2 root root 6 Dec 12 08:53 Downloads
drwxr-xr-x. 2 root root 6 Dec 12 08:53 Music
drwxr-xr-x. 2 root root 6 Dec 12 08:53 Pictures
drwxr-xr-x. 2 root root 6 Dec 12 08:53 Public
drwxr-xr-x. 2 root root 6 Dec 12 08:53 Templates
drwxr-xr-x. 2 root root 6 Dec 12 08:53 Videos
-rw-------. 1 root root 1945 Dec 11 14:20 anaconda-ks.cfg
-t选项为按照mtime排序 另外-u配合-t可以按照atime排序也常用 ls -ltu /root/
7选项数字1和-X
[root@cent7 ~]#ls -1X /root/
Desktop
Documents
Downloads
etc
etc2018-12-21
motd
Music
Pictures
Public
Templates
test
txt
txt2
Videos
core.2431
anaconda-ks.cfg
dead.letter
ss.log
此处选项为数字1与X结合1表示内容分行显示,X为按文件后缀排序。
8 选项-U
[root@cent7 ~]#ls -lU /root/
total 5148
-rw-------. 1 root root 1945 Dec 11 14:20 anaconda-ks.cfg
drwxr-xr-x. 2 root root 6 Dec 12 08:53 Desktop
drwxr-xr-x. 2 root root 6 Dec 12 08:53 Downloads
drwxr-xr-x. 2 root root 6 Dec 12 08:53 Templates
drwxr-xr-x. 2 root root 6 Dec 12 08:53 Public
drwxr-xr-x. 2 root root 6 Dec 12 08:53 Documents
drwxr-xr-x. 2 root root 6 Dec 12 08:53 Pictures
drwxr-xr-x. 2 root root 6 Dec 12 08:53 Videos
drwxr-xr-x. 2 root root 6 Dec 12 08:53 Music
lrwxrwxrwx. 1 root root 7 Dec 15 15:25 test -> ../date
-rw-------. 1 root root 18 Dec 17 20:38 dead.letter
-rw-------. 1 root root 5246976 Dec 18 11:17 core.2431
-rw-r--r--. 1 root root 0 Dec 19 15:35 motd
-rw-r--r--. 1 root root 559 Dec 19 16:08 ss.log
-rw-r--r--. 1 root root 72 Dec 20 14:47 txt
-rw-r--r--. 1 root root 22 Dec 20 14:52 txt2
-rw-r--r--. 1 root root 3 Dec 21 16:18 etc
-rw-r--r--. 1 root root 0 Dec 21 16:32 etc2018-12-21
-U按照目录存放顺序显示