0

Linux之ls命令

 2 years ago
source link: https://segmentfault.com/a/1190000041099544
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Linux之ls命令

发布于 今天 10:28

ls 命令是 linux 下最常用的命令。ls 命令就是 list 的缩写缺省下 ls 用来打印出当前目录的清单,如果 ls 指定其他目录,那么就会显示指定目录里的文件及文件夹清单。 通过 ls 命令不仅可以查看 linux 文件夹包含的文件,而且可以查看文件权限 (包括目录、文件夹、文件权限)查看目录信息等等。ls 命令在日常的 linux 操作中用的很多!

1.命令格式:

ls [选项] [目录名]

2.命令功能:

列出目标目录中所有的子目录和文件。

3.常用参数:

WX20210126-000215@2x

4.常用范例:

例一:列出 /home/rumenz 文件夹下的所有文件和目录的详细资料

命令:ls -l -R /home/rumenz

在使用 ls 命令时要注意命令的格式:在命令提示符后,首先是命令的关键字,接下来是命令参数,在命令参数之前要有一短横线 “-”,所有的命令参数都有特定的作用,自己可以根据需要选用一个或者多个参数,在命令参数的后面是命令的操作对象。在以上这条命令“ ls -l -R /home/rumenz” 中,“ls” 是命令关键字,“-l -R”是参数,“ /home/rumenz”是命令的操作对象。在这条命令中,使用到了两个参数,分别为 “l” 和“R”,当然,你也可以把他们放在一起使用,如下所示:

命令:ls -lR /home/rumenz

这种形式和上面的命令形式执行的结果是完全一样的。另外,如果命令的操作对象位于当前目录中,可以直接对操作对象进行操作; 如果不在当前目录则需要给出操作对象的完整路径,例如上面的例子中,我的当前文件夹是 rumenz 文件夹,我想对 home 文件夹下的 rumenz 文件进行操作,我可以直接输入 ls -lR rumenz,也可以用 ls -lR /home/rumenz。

例二:列出当前目录中所有以 “t” 开头的目录的详细内容,可以使用如下命令:

命令:ls -l t*

可以查看当前目录下文件名以 “t” 开头的所有文件的信息。其实,在命令格式中,方括号内的内容都是可以省略的,对于命令 ls 而言,如果省略命令参数和操作对象,直接输入“ ls ”,则将会列出当前工作目录的内容清单。

例三:只列出文件下的子目录

命令:ls -F /opt/soft |grep /$ 

列出/opt/soft 文件下面的子目录

[root@localhost rumenz]# ls -F $PWD | grep /$

excache/
hsperfdata_deploy/
hsperfdata_root/
poifiles/

命令ls -l /opt/soft | grep "^d"

列出 /opt/soft 文件下面的子目录详细情况

[root@localhost rumenz]#  ls -l $PWD | grep "^d"

drwxr-xr-x 2 root root       21 Jan 13 14:33 app
drwxr-xr-x 4 root root      141 Jan 25 21:20 web
drwxr-xr-x 3 root root       57 Dec 18 22:17 web-test

例四:列出目前工作目录下所有名称是 s 开头的档案,愈新的排愈后面,可以使用如下命令:

命令:ls -ltr s*

[root@localhost rumenz]# ls -ltr w*

web-test:
total 4428
drwxr-xr-x 6 root root     108 Dec 18 22:17 static
-rw-r--r-- 1 root root     572 Dec 18 22:17 index.html
-rw-r--r-- 1 root root 4528805 Dec 18 22:17 dist.tar.gz

web:
total 81656
-rw-r--r-- 1 root root  5277607 Aug 16 00:12 dist815.zip
drwxr-xr-x 6 root root      108 Dec  8 14:21 static
-rw-r--r-- 1 root root      572 Dec  8 14:21 index.html
-rw-r--r-- 1 root root  4514510 Dec  8 14:21 dist.tar.gz

例五:列出目前工作目录下所有档案及目录; 目录于名称后加 "/", 可执行档于名称后加 "*"

命令:ls -AF

[root@localhost rumenz]# ls -AF

log/  script/  soft/  src/  svndata/  web/

例六:计算当前目录下的文件数和目录数

ls -l * |grep "^-"|wc -l --- 文件个数

ls -l * |grep "^d"|wc -l    --- 目录个数

例七: 在 ls 中列出文件的绝对路径

命令:ls | sed "s:^:pwd/:"

[root@localhost rumenz]# ls | sed "s:^:`pwd`/:"
/opt/log
/opt/script
/opt/soft
/opt/src
/opt/svndata
/opt/web

例九:列出当前目录下的所有文件(包括隐藏文件)的绝对路径, 对目录不做递归

命令:find $PWD -maxdepth 1 | xargs ls -ld

[root@localhost rumenz]# find $PWD -maxdepth 1 | xargs ls -ld

drwxrwxrwt. 78 root   root      4096 Jan 25 23:09 /tmp
drwx------   2 root   root         6 Jan  8 16:10 /tmp/20210108_161044-scantem.0ef7dea9d3
drwx------   3 root   root        33 Jan  8 16:12 /tmp/20210108_161248-scantem.62a5f98367
drwx------   2 root   root         6 Jan  8 16:57 /tmp/20210108_165745-scantem.f73926d239

例十:递归列出当前目录下的所有文件(包括隐藏文件)的绝对路径

命令: find $PWD | xargs ls -ld

例十一:指定文件时间输出格式

命令:
ls -tl --time-style=full-iso

[root@localhost soft]# ls -lt --time-style=full-iso

total 0
drwxrwxr-x 3 deploy deploy 18 2021-01-25 15:35:57.075199271 +0800 tomcat.120562076922433750.8080
drwxrwxr-x 2 deploy deploy  6 2021-01-25 15:35:57.047196910 +0800 tomcat-docbase.6189031708285654679.8080
drwxr-xr-x 2 deploy deploy 56 2021-01-25 15:35:51.952767467 +0800 hsperfdata_deploy
[root@localhost soft]# ls -ctl --time-style=long-iso

total 0
drwxrwxr-x 3 deploy deploy 18 2021-01-25 15:35 tomcat.120562076922433750.8080
drwxrwxr-x 2 deploy deploy  6 2021-01-25 15:35 tomcat-docbase.6189031708285654679.8080
drwxr-xr-x 2 deploy deploy 56 2021-01-25 15:35 hsperfdata_deploy
drwxrwxr-x 3 deploy deploy 18 2021-01-25 10:28 tomcat.7686949051420446439.8080

显示彩色目录列表

打开 / etc/bashrc, 加入如下一行:

alias ls="ls --color"

下次启动 bash 时就可以像在 Slackware 里那样显示彩色的目录列表了, 其中颜色的含义如下:

  • 蓝色 --> 目录
  • 绿色 --> 可执行文件
  • 红色 --> 压缩文件
  • 浅蓝色 --> 链接文件
  • 灰色 --> 其他文件

原文链接:https://rumenz.com/rumenbiji/...
微信公众号:入门小站

linux常用命令速查手册PDF下载

3669页vim参考手册PDF下载

阿里云ECS运维Linux系统诊断PDF下载

Docker速查手册PDF下载

Linux学习笔记【强悍总结值得一看】PDF下载

shell简明教程PDF下载


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK