ls常用命令详解

小编 2026-07-06 阅读:1830 评论:0
NAME(名称) ls - list directory contents SYNOPSIS(语法) ls [OPTION]… [FILE]… DESCRIPTION(描述) List inf...

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没有指定选项,就按找字母顺序给条目排序。对长选项的强制参数对于短选项也是强制的。(这句话我也不理解,因为ls可以不加任何选项嘛)

以下就不一一翻译了,反正英语很重要,不懂的自己查字典吧。
-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按照目录存放顺序显示

版权声明

本文仅代表作者观点,不代表百度立场。
本文系作者授权百度百家发表,未经许可,不得转载。

上一篇:Python--sys 下一篇:mongodb慢查询记录
热门文章
  • Sequential Monte Carlo Methods (SMC) 序列蒙特卡洛/粒子滤波/Bootstrap Filtering

    Sequential Monte Carlo Methods (SMC) 序列蒙特卡洛/粒子滤波/Bootstrap Filtering
    Problem Statement 我们考虑一个具有马尔可夫性质、非线性、非高斯的状态空间模型(State Space Model):对于一个时间序列上的观测结果{yt,t∈N}\\{ y_t , t \\in N \\}{yt​,t∈N},我们认为每个观测结果yty_tyt​的生成依赖于一个无法直接观察的隐变量xt∈{xt,t∈N}x_t \\in \\{x_t , t \\in N \\}xt​∈{xt​,t∈N},即:p(...
  • 机房智能化温湿度解决方式之POE供电以太网温湿度传感器

    机房智能化温湿度解决方式之POE供电以太网温湿度传感器
    机房智能化温湿度解决方式之POE供电以太网温湿度传感器 北京盈创力和电子科技有限公司 智能型TCP网口温湿度记录仪 北京IP网络温湿度记录仪厂家,北京盈创力和 北京智能型TCP网口温湿度记录仪IP网络温湿度记录仪是一种新型的基于TCP/IP协议双绞线以太网标准温湿度采集模块,利用它可以实现现场温度值、相对湿度值的采集,同时利用其自身的RJ45通信接口可以方便地和机房监控主机或交换机集线器进行联网。 工作于-40℃~85℃工业级带...
  • Hive 系统函数及示例

    Hive 系统函数及示例
    查看所有系统函数 show functions; 函数分类 内置函数【系统函数】 数学函数: floor、round、ceil、cos、log2等 字符串函数: length、reverse、trim、lower、get_json_object、repeat等 收集函数: size 转换函数: cast 日期函数: year、month、datediff、date、date_add等 条件函数: coalesce、case…w...
  • HTTP状态保持的原理

    HTTP状态保持的原理
    a)在用户登录之后,浏览器返回响应的时候会在响应中添加上cookieb)浏览器接收到cookie之后会自动保存c)当用户再次请求同一服务器中的其他网页的时候,浏览器会自动带上之前保存的cookied)服务接收到请求之后可以请 request 对象中取到cookie 判断当前用户是否登录  Http是无状态的,就是连接时数据互通,关闭后...
  • CSRF的原理和防范措施

    CSRF的原理和防范措施
    a)攻击原理:i.用户C访问正常网站A时进行登录,浏览器保存A的cookieii.用户C再访问攻击网站B,网站B上有某个隐藏的链接或者图片标签会自动请求网站A的URL地址,例如表单提交,传指定的参数iii.而攻击网站B在访问网站A的时候,浏览器会自动带上网站A的cookieiv.所以网站A在接收到请求之后可判断当前用户是登录状态,所以...
标签列表