乐趣区

关于linux:linux-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
how to check file owner in Linux

注:第 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
更多参考如何在 linux 查看文件大小
how to check file size in Linux

实例 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
Linux ls command sort by size

实例 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
ls command

实例 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/16×16/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.h…
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.

参考信息:
how to get file path in Linux

ls command

退出移动版