前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Linux ls 命令使用方法总结

Linux ls 命令使用方法总结

作者头像
用户8418197
发布2022-03-25 20:08:54
1.2K0
发布2022-03-25 20:08:54
举报
文章被收录于专栏:howtouselinuxhowtouselinux

实例1:查看当前目录下的文件或目录

root@howtouselinux tmp# ls

1d 2d 3d 4d TEST

实例2:使用长清单模式,-l参数

root@howtouselinux 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@howtouselinux 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@howtouselinux 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@howtouselinux 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@howtouselinux 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@howtouselinux tmp# ls -a

. … 1d 2d 3d 4d .ICE-unix TEST

实例7:只列出目录,使用-d参数

root@howtouselinux 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@howtouselinux 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@howtouselinux 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@howtouselinux 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@howtouselinux 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@howtouselinux 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@howtouselinux 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@howtouselinux 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@howtouselinux 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@howtouselinux 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@howtouselinux 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@howtouselinux etc# ls ~

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

实例19:列出父目录

root@howtouselinux 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@howtouselinux 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.

参考信息:

how to get file path in Linux

ls command

本文系转载,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文系转载前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
作者已关闭评论
0 条评论
热度
最新
推荐阅读
目录
  • 实例1:查看当前目录下的文件或目录
  • 实例2:使用长清单模式,-l参数
  • 实例3:显示文件大小
  • 实例4:排序文件大小,-S参数
  • 实例5:测量大小,使用–block-size=key
  • 实例6:显示隐藏文件,即以.开头的文件,使用-a参数
  • 实例7:只列出目录,使用-d参数
  • 实例8:不打印所有者信息,使用-g参数
  • 实例9:不打印组信息,使用-G参数
  • 实例10:打印UID和GID代替用户名和组名,使用-n参数
  • 实例11:不带颜色打印,使用–color=never参数
  • 实例12:打印每个文件的索引号,使用-i参数
  • 实例13:增加/(斜线)标记目录,-p参数
  • 实例14:排序时反转顺序,使用-r参数
  • 实例15:递归列出子目录,使用-R参数
  • 实例16:扩展名排序,使用-X参数或者–sort=extension
  • 实例17:通过修改时间列出,比较新的会在前面,使用-t参数
  • 实例18:列出主目录
  • 实例19:列出父目录
  • 实例20:打印ls命令版本
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档