Linux ls命令

ls命令
1、 介绍
ls - list directory contents
ls命令是Linux中最常用的命令之一。ls是list的缩写
2、 命令格式
ls [选项] [文件名]
3、 命令功能
用来打印当前目录清单或者打印出指定目录下的文件及文件清单。ls命令在打印文件清单时,还可以查看文件权限、目录信息等等。
4、 命令参数
可以使用帮助命令查看详细命令参数:man ls或ls --help
-a, --all
do not ignore entries starting with .(列出目录下的所有文件,包括以.开头的隐含文件)

-A, --almost-all
do not list implied . and …(列出除了.及…以外的所有文件)

–author
with -l, print the author of each file(打印出每一个文件的作者)

-b, --escape
print octal escapes for nongraphic characters(不能输出的字符用反斜杠加字符编号的形式输出)

–block-size=SIZE
use SIZE-byte blocks.(使用SIZE-byte的大小的字节为单位)

-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
(输出文件的ctime(文件状态最后更改的时间),并根据ctime排序)

-C list entries by columns(由上至下的列出项目)

–color[=WHEN]
colorize the output. WHEN defaults to ‘always’ or can be ‘never’ or ‘auto’. More info below(控制是否使用色彩分辨文件。WHEN可以是always、never或者auto其中一个)

-d, --directory
list directory entries instead of contents, and do not dereference symbolic links
(将目录像文件一样显示,而不显示目录下面的内容)

-D, --dired
generate output designed for Emacs’ dired mode(产生适合Emacs的dired模 式使用的结果)

-f do not sort, enable -aU, disable -ls --color(对输出的文件不进行排序,-aU选线生效,-ls选项失效)

-F, --classify
append indicator (one of /=>@|) to entries(加上文件类型的指示符号(/=@| 其中一个))

–file-type
likewise, except do not append ‘’(和-F一样,除了不追加“”)

–format=WORD
across -x, commas -m, horizontal -x, long -l, single-column -1, verbose -l, vertical -C(–format=关键之,关键字可以是“across -x, commas -m, horizontal -x, long -l, single-column -1, verbose -l, vertical -C”)

–full-time
like -l --time-style=full-iso(即 -l --time-style=full-iso)

-g like -l, but do not list owner(像-l,但是不列出所有者)

–group-directories-first
group directories before files.(组目录在文件目录之前)

augment with a --sort option, but any use of --sort=none (-U) disables grouping
(用-sort选项进行扩展,但任何使用-sort=none(-U)禁用组)

-G, --no-group
in a long listing, don’t print group names(不列出任何有关于组的信息)

-h, --human-readable
with -l, print sizes in human readable format (e.g., 1K 234M 2G)(类似于-l,以人容易理解的格式列出文件大小)

–si likewise, but use powers of 1000 not 1024(像-h,但是文件大小取1000而不是1024)

-H, --dereference-command-line
follow symbolic links listed on the command line(使用命令列中的符号链接指示的 真正目的地)

–dereference-command-line-symlink-to-dir
follow each command line symbolic link that points to a directory(遵循指向目录的 每个命令行符号链接)

–hide=PATTERN
do not list implied entries matching shell PATTERN (overridden by -a or -A)(不 要列出与shell模式匹配的隐含条目(由-a或-A重写))

–indicator-style=WORD
append indicator with style WORD to entry names: none (default), slash (- p), file-type (–file-type), classify (-F)(指定在每个项目名称后加上指示符号<方 式>:none (default), slash (-p), file-type (–file-type), classify (-F))

-i, --inode
print the index number of each file(输出每个文件的inode号)

-I, --ignore=PATTERN
do not list implied entries matching shell PATTERN(不列出任何符合sell万用字 符<样式>的项目)

-k like --block-size=1K(像 --block-size=1K,以k字节的形式表示文件的大小)

-l use a long listing format(使用长格式,即列出文件详细信息)

-L, --dereference
when showing file information for a symbolic link, show information for the file the link references rather than for the link itself(当显示符号连接的文件信息时,显示符号链接所指示的对象而并非符号链接本身的信息)

-m fill width with a comma separated list of entries(所有项目以逗号隔开,并填满整行行宽)

-n, --numeric-uid-gid
like -l, but list numeric user and group IDs(类似-l,用数字UID和GID代替名称)

-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 non graphic characters(以?字符代替无法打印的字符)

–show-control-chars
show non graphic characters as-is (default unless program is ‘ls’ and output is a terminal)(直接显示无法打印的字符(这是默认方式,除非调用的程序名称时‘ls’而且在终端机画面输出结果))

-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(使用指定的quotin方式类型: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(根据文件大小排序)

–sort=WORD
sort by WORD instead of name: none -U, extension -X, size -S, time -t, version -v(根据关键词代替名字排序:none -U, extension -X, size -S, time -t, version -v)

–time=WORD
with -l, show time as WORD instead of modification time: atime -u, access -u, use -u, ctime -c, or status-c; use specified time as sort key if --sort=time(类似于-l,根据关键词显示时间代替默认的修改时间:atime -u, access -u, use -u, ctime -c, or status)

–time-style=STYLE
with -l, show times using style STYLE: full-iso, long-iso, iso, locale, +FORMAT. FORMAT is interpreted like ‘date’; if FORMAT is FORMAT1FORMAT2, FORMAT1 applies to non-recent files and FORMAT2 to recent files; if STYLE is prefixed with ‘posix-’, STYLE takes effect only outside the POSIX locale(显示时间使用style:…………)

-t sort by modification time(以文件修改时间排序)

-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(-u配合-lt:显示访问时间而依访问时间排序;
配合-l:显示访问时间但根据名称排序;否则,根据访问时间排序)

-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(每行只列出一个文件)

–help 显示此帮助信息并退出

-version 显示版本信息并退出

5、命令范例
由于ls选项过多,这里仅使用一些常用选项做举例
实例1:查看当前目录下的文件或目录
[root@lianxi tmp]# ls
1d 2d 3d 4d TEST

实例2:使用长清单模式,-l参数
[root@lianxi tmp]# ls -l
total 20
drwxr-xr-x. 2 root root 4096 Dec 8 00:44 1d
drwxr-xr-x. 2 root root 4096 Dec 8 00:44 2d
drwxr-xr-x. 2 root root 4096 Dec 8 00:44 3d
drwxr-xr-x. 2 root root 4096 Dec 8 00:44 4d
drwxr-xr-x. 6 root root 4096 Dec 8 00:46 TEST

注:第1列
第一个字母d意味着内容是目录或者文件。如果是’-‘(减号),这意味着它的内容是文件。当它是l(小写l字符),意味这内容是链接文件。
下面的9个字符是关于文件权限。前3个rwx字符是文件的拥有者的权限,第二组3rwx是文件的所有组的权限,最后的rwx是对其他人访问文件的权限。
第2列 这行告诉我们有多少链接指向这个文件。
第3列 这行告诉我们谁是这个文件/文件夹的所有者。
第4列 这行告诉我们谁是这个文件/文件夹的所有组。
第5列 这行告诉我们这个文件/文件夹的以字节为单位的大小。 目录的大小总是4096字节。
第6列 这告诉我们文件最后的修改时间。
第7列 这告诉我们文件名或者目录名。

实例3:显示文件大小
① 以便于人阅读的方式显示文件大小,-h参数
[root@lianxi tmp]# ls -lh
total 20K
drwxr-xr-x. 2 root root 4.0K Dec 8 00:44 1d
drwxr-xr-x. 2 root root 4.0K Dec 8 00:44 2d
drwxr-xr-x. 2 root root 4.0K Dec 8 00:44 3d
drwxr-xr-x. 2 root root 4.0K Dec 8 00:44 4d
drwxr-xr-x. 6 root root 4.0K Dec 8 00:46 TEST
② 使用–si参数,此参数和-h类似,但-si是以1000为单位,而-h是以1024为单位
[root@lianxi tmp]# ls -l --si
total 21k
drwxr-xr-x. 2 root root 4.1k Dec 8 00:44 1d
drwxr-xr-x. 2 root root 4.1k Dec 8 00:44 2d
drwxr-xr-x. 2 root root 4.1k Dec 8 00:44 3d
drwxr-xr-x. 2 root root 4.1k Dec 8 00:44 4d
drwxr-xr-x. 6 root root 4.1k Dec 8 00:46 TEST

实例4:排序文件大小,-S参数
[root@lianxi tmp]# ls -lhS /etc/
total 1.7M
-rw-r–r--. 1 root root 626K Oct 2 2013 services
-rw-r–r--. 1 root root 150K Dec 8 03:14 prelink.cache
-rw-r–r--. 1 root root 87K Dec 7 14:44 termcap
-rw-r–r--. 1 root root 43K Sep 23 2011 mime.types
drwxr-xr-x. 3 root root 4.0K Dec 7 14:43 abrt
drwxr-xr-x. 4 root root 4.0K Dec 7 14:44 acpi

实例5:测量大小,使用–block-size=key
ls可以通过使用–block-size=SIZE改单位大小。这里的SIZE是:
K = Kilobyte M = Megabyte G = Gigabyte T = Terabyte
P = Petabyte E = Exabyte Z = Zettabyte Y = Yottabyte
比如,我们希望使用KB作为单位大小。所以语法就会像这样:
[root@lianxi tmp]# ls -l --block-size=K /etc/
total 1680K
drwxr-xr-x. 3 root root 4K Dec 7 14:43 abrt
drwxr-xr-x. 4 root root 4K Dec 7 14:44 acpi
-rw-r–r--. 1 root root 1K Dec 7 23:31 adjtime
-rw-r–r--. 1 root root 2K Jan 12 2010 aliases
-rw-r–r--. 1 root root 12K Dec 7 14:45 aliases.db
drwxr-xr-x. 2 root root 4K Dec 7 14:44 alsa
drwxr-xr-x. 2 root root 4K Dec 7 14:43 alternatives

实例6:显示隐藏文件,即以.开头的文件,使用-a参数
[root@lianxi tmp]# ls -a
. … 1d 2d 3d 4d .ICE-unix TEST

实例7:只列出目录,使用-d参数
[root@lianxi etc]# ls -ld */
drwxr-xr-x. 3 root root 4096 Dec 7 14:43 abrt/
drwxr-xr-x. 4 root root 4096 Dec 7 14:44 acpi/
drwxr-xr-x. 2 root root 4096 Dec 7 14:44 alsa/
drwxr-xr-x. 2 root root 4096 Dec 7 14:43 alternatives/
drwxr-x—. 3 root root 4096 Dec 7 14:44 audisp/
drwxr-x—. 2 root root 4096 Dec 7 14:44 audit/
drwxr-xr-x. 2 root root 4096 Dec 7 14:44 bash_completion.d/
drwxr-xr-x. 2 root root 4096 Dec 7 14:45 blkid/

实例8:不打印所有者信息,使用-g参数
[root@lianxi etc]# ls -g
total 1680
drwxr-xr-x. 3 root 4096 Dec 7 14:43 abrt
drwxr-xr-x. 4 root 4096 Dec 7 14:44 acpi
-rw-r–r--. 1 root 46 Dec 7 23:31 adjtime
-rw-r–r--. 1 root 1512 Jan 12 2010 aliases
-rw-r–r--. 1 root 12288 Dec 7 14:45 aliases.db
drwxr-xr-x. 2 root 4096 Dec 7 14:44 alsa
drwxr-xr-x. 2 root 4096 Dec 7 14:43 alternatives
-rw-------. 1 root 541 Nov 23 2013 anacrontab
-rw-r–r--. 1 root 148 May 15 2009 asound.conf
-rw-r–r--. 1 root 1 Jan 30 2012 at.deny

实例9:不打印组信息,使用-G参数
[root@lianxi etc]# ls -lG
total 1680
drwxr-xr-x. 3 root 4096 Dec 7 14:43 abrt
drwxr-xr-x. 4 root 4096 Dec 7 14:44 acpi
-rw-r–r--. 1 root 46 Dec 7 23:31 adjtime
-rw-r–r--. 1 root 1512 Jan 12 2010 aliases
-rw-r–r--. 1 root 12288 Dec 7 14:45 aliases.db
drwxr-xr-x. 2 root 4096 Dec 7 14:44 alsa
drwxr-xr-x. 2 root 4096 Dec 7 14:43 alternatives
-rw-------. 1 root 541 Nov 23 2013 anacrontab
-rw-r–r--. 1 root 148 May 15 2009 asound.conf
-rw-r–r--. 1 root 1 Jan 30 2012 at.deny

实例10:打印UID和GID代替用户名和组名,使用-n参数
[root@lianxi etc]# ls -n
total 1680
drwxr-xr-x. 3 0 0 4096 Dec 7 14:43 abrt
drwxr-xr-x. 4 0 0 4096 Dec 7 14:44 acpi
-rw-r–r--. 1 0 0 46 Dec 7 23:31 adjtime
-rw-r–r--. 1 0 0 1512 Jan 12 2010 aliases
-rw-r–r--. 1 0 0 12288 Dec 7 14:45 aliases.db
drwxr-xr-x. 2 0 0 4096 Dec 7 14:44 alsa
drwxr-xr-x. 2 0 0 4096 Dec 7 14:43 alternatives
-rw-------. 1 0 0 541 Nov 23 2013 anacrontab
-rw-r–r--. 1 0 0 148 May 15 2009 asound.conf
-rw-r–r--. 1 0 0 1 Jan 30 2012 at.deny
drwxr-x—. 3 0 0 4096 Dec 7 14:44 audisp

实例11:不带颜色打印,使用–color=never参数
[root@lianxi etc]# ls -l --color=never
total 1680
drwxr-xr-x. 3 root root 4096 Dec 7 14:43 abrt
drwxr-xr-x. 4 root root 4096 Dec 7 14:44 acpi
-rw-r–r--. 1 root root 46 Dec 7 23:31 adjtime
-rw-r–r--. 1 root root 1512 Jan 12 2010 aliases
-rw-r–r--. 1 root root 12288 Dec 7 14:45 aliases.db
drwxr-xr-x. 2 root root 4096 Dec 7 14:44 alsa
drwxr-xr-x. 2 root root 4096 Dec 7 14:43 alternatives
-rw-------. 1 root root 541 Nov 23 2013 anacrontab
-rw-r–r--. 1 root root 148 May 15 2009 asound.conf
在这里插入图片描述
在这里插入图片描述

实例12:打印每个文件的索引号,使用-i参数
[root@lianxi etc]# ls -li
total 1680
918741 drwxr-xr-x. 3 root root 4096 Dec 7 14:43 abrt
791386 drwxr-xr-x. 4 root root 4096 Dec 7 14:44 acpi
790584 -rw-r–r--. 1 root root 46 Dec 7 23:31 adjtime
784914 -rw-r–r--. 1 root root 1512 Jan 12 2010 aliases
791636 -rw-r–r--. 1 root root 12288 Dec 7 14:45 aliases.db
791495 drwxr-xr-x. 2 root root 4096 Dec 7 14:44 alsa
785057 drwxr-xr-x. 2 root root 4096 Dec 7 14:43 alternatives
790861 -rw-------. 1 root root 541 Nov 23 2013 anacrontab
786368 -rw-r–r--. 1 root root 148 May 15 2009 asound.conf

实例13:增加/(斜线)标记目录,-p参数
[root@lianxi etc]# ls -lp
total 1680
drwxr-xr-x. 3 root root 4096 Dec 7 14:43 abrt/
drwxr-xr-x. 4 root root 4096 Dec 7 14:44 acpi/
-rw-r–r--. 1 root root 46 Dec 7 23:31 adjtime
-rw-r–r--. 1 root root 1512 Jan 12 2010 aliases
-rw-r–r--. 1 root root 12288 Dec 7 14:45 aliases.db
drwxr-xr-x. 2 root root 4096 Dec 7 14:44 alsa/
drwxr-xr-x. 2 root root 4096 Dec 7 14:43 alternatives/
-rw-------. 1 root root 541 Nov 23 2013 anacrontab
-rw-r–r--. 1 root root 148 May 15 2009 asound.conf

实例14:排序时反转顺序,使用-r参数
[root@lianxi etc]# ls -lr
total 1680
drwxr-xr-x. 2 root root 4096 Dec 7 14:43 yum.repos.d
-rw-r–r--. 1 root root 969 Feb 22 2013 yum.conf
drwxr-xr-x. 5 root root 4096 Dec 7 14:43 yum
drwxr-xr-x. 2 root root 4096 Dec 7 14:43 xinetd.d
drwxr-xr-x. 3 root root 4096 Dec 7 14:42 xdg
drwxr-xr-x. 4 root root 4096 Dec 7 14:43 X11
-rw-r–r--. 1 root root 0 Dec 7 14:47 wvdial.conf
-rw-r–r--. 1 root root 4479 Oct 10 2012 wgetrc
-rw-r–r--. 1 root root 3008 Nov 22 2013 warnquota.conf
-rw-r–r--. 1 root root 1962 Feb 17 2012 virc

实例15:递归列出子目录,使用-R参数
[root@lianxi etc]# ls -R /tmp/
/tmp/:
1d 2d 3d 4d TEST
/tmp/1d:
/tmp/2d:
/tmp/3d:
/tmp/4d:
/tmp/TEST:

实例16:扩展名排序,使用-X参数或者–sort=extension
[root@lianxi etc]# ls -lrX
total 1680
drwxr-xr-x. 2 root root 4096 Sep 27 2011 cron.weekly
-rw-r–r--. 1 root root 1112 Mar 31 2003 minicom.users
-rw-r–r--. 1 root root 43591 Sep 23 2011 mime.types
lrwxrwxrwx. 1 root root 15 Dec 7 14:43 rc.sysinit -> rc.d/rc.sysinit
-rw-r–r--. 1 root root 1 Dec 7 23:23 resolv.conf.save
-rw-r–r--. 1 root root 1909 Aug 1 2013 mail.rc
lrwxrwxrwx. 1 root root 56 Dec 7 14:42 favicon.png -> /usr/share/icons/hicolor/16x16/apps/system-logo-icon.png
-rw-r–r--. 1 root root 46 Nov 27 2013 issue.net
drwxr-xr-x. 2 root root 4096 Dec 7 14:44 cron.monthly

实例17:通过修改时间列出,比较新的会在前面,使用-t参数
[root@lianxi etc]# ls -lt
total 1680
-rw-r–r--. 1 root root 152927 Dec 8 03:14 prelink.cache
-rw-r–r--. 1 root root 264 Dec 7 23:31 mtab
-rw-r–r--. 2 root root 53 Dec 7 23:31 resolv.conf
-rw-r–r--. 1 root root 46 Dec 7 23:31 adjtime
drwxr-xr-x. 7 root root 4096 Dec 7 23:29 sysconfig
-rw-r–r--. 2 root root 165 Dec 7 23:28 hosts
-rw-r–r--. 1 root root 1 Dec 7 23:23 resolv.conf.save

实例18:列出主目录
[root@lianxi etc]# ls ~
anaconda-ks.cfg install.log install.log.syslog

实例19:列出父目录
[root@lianxi etc]# ls …/
bin boot data dev etc home lib lib64 lost+found media mnt opt proc root sbin selinux srv sys tmp usr var

实例20:打印ls命令版本
[root@lianxi etc]# ls --version
ls (GNU coreutils) 8.4
Copyright © 2010 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later http://gnu.org/licenses/gpl.html.
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.

Written by Richard M. Stallman and David MacKenzie.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值