最新消息:XAMPP默认安装之后是很不安全的,我们只需要点击左方菜单的 "安全"选项,按照向导操作即可完成安全设置。

Linux之ls命令示例代码

XAMPP相关 admin 222浏览 0评论

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

1.命令格式:

ls [选项] [目录名]

2.命令功能:

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

3.常用参数:

dqz035

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 里那样显示彩色的目录列表了, 其中颜色的含义如下:

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

 linux中的15个基本ls命令示例

1. 使用 ls 不带选项列出文件

`>  ls`

0001.pcap        Desktop    Downloads         index.html   install.log.syslog  Pictures  Templates
anaconda-ks.cfg  Documents  fbcmd_update.php  install.log  Music               Public    Videos

2 使用选项 -l 列出文件

这里, ls -l (-l 是字符不是一个)显示文件或目录、大小、修改日期和时间、文件或文件夹名称以及文件的所有者及其权限。

`>  ls -l`

total 176
-rw-r--r--. 1 root root   683 Aug 19 09:59 0001.pcap
-rw-------. 1 root root  1586 Jul 31 02:17 anaconda-ks.cfg
drwxr-xr-x. 2 root root  4096 Jul 31 02:48 Desktop
drwxr-xr-x. 2 root root  4096 Jul 31 02:48 Documents
drwxr-xr-x. 4 root root  4096 Aug 16 02:55 Downloads
-rw-r--r--. 1 root root 21262 Aug 12 12:42 fbcmd_update.php
-rw-r--r--. 1 root root 46701 Jul 31 09:58 index.html
-rw-r--r--. 1 root root 48867 Jul 31 02:17 install.log
-rw-r--r--. 1 root root 11439 Jul 31 02:13 install.log.syslog
drwxr-xr-x. 2 root root  4096 Jul 31 02:48 Music
drwxr-xr-x. 2 root root  4096 Jul 31 02:48 Pictures
drwxr-xr-x. 2 root root  4096 Jul 31 02:48 Public
drwxr-xr-x. 2 root root  4096 Jul 31 02:48 Templates
drwxr-xr-x. 2 root root  4096 Jul 31 02:48 Videos

3. 查看隐藏文件

列出所有文件,包括以 ‘ 开头的隐藏文件.‘。

`>  ls -a`

.                .bashrc  Documents         .gconfd          install.log         .nautilus     .pulse-cookie
..               .cache   Downloads         .gnome2          install.log.syslog  .netstat.swp  .recently-used.xbel
0001.pcap        .config  .elinks           .gnome2_private  .kde                .opera        .spice-vdagent
anaconda-ks.cfg  .cshrc   .esd_auth         .gtk-bookmarks   .libreoffice        Pictures      .tcshrc
.bash_history    .dbus    .fbcmd            .gvfs            .local              .pki          Templates
.bash_logout     Desktop  fbcmd_update.php  .ICEauthority    .mozilla            Public        Videos
.bash_profile    .digrc   .gconf            index.html       Music               .pulse        .wireshark

4. 使用选项 -lh 列出具有人类可读格式的文件

freestar.config.enabled_slots.push({ placementName: “tecmint_incontent”, slotId: “tecmint_incontent” });

与组合 -lh 选项,以人类可读的格式显示尺寸。

`>  ls -lh`

total 176K
-rw-r--r--. 1 root root  683 Aug 19 09:59 0001.pcap
-rw-------. 1 root root 1.6K Jul 31 02:17 anaconda-ks.cfg
drwxr-xr-x. 2 root root 4.0K Jul 31 02:48 Desktop
drwxr-xr-x. 2 root root 4.0K Jul 31 02:48 Documents
drwxr-xr-x. 4 root root 4.0K Aug 16 02:55 Downloads
-rw-r--r--. 1 root root  21K Aug 12 12:42 fbcmd_update.php
-rw-r--r--. 1 root root  46K Jul 31 09:58 index.html
-rw-r--r--. 1 root root  48K Jul 31 02:17 install.log
-rw-r--r--. 1 root root  12K Jul 31 02:13 install.log.syslog
drwxr-xr-x. 2 root root 4.0K Jul 31 02:48 Music
drwxr-xr-x. 2 root root 4.0K Jul 31 02:48 Pictures
drwxr-xr-x. 2 root root 4.0K Jul 31 02:48 Public
drwxr-xr-x. 2 root root 4.0K Jul 31 02:48 Templates
drwxr-xr-x. 2 root root 4.0K Jul 31 02:48 Videos

5. 以/字符结尾的列出文件和目录

使用 -F 选项与 ls 命令,将添加 ‘/’ 每个目录末尾的字符。

`>  ls -F`
0001.pcap        Desktop/    Downloads/        index.html   install.log.syslog  Pictures/  Templates/
anaconda-ks.cfg  Documents/  fbcmd_update.php  install.log  Music/              Public/    Videos/

6. 以相反的顺序列出文件

以下命令与 ls -r 选项以相反的顺序显示文件和目录。

`>  ls -r`

Videos     Public    Music               install.log  fbcmd_update.php  Documents  anaconda-ks.cfg
Templates  Pictures  install.log.syslog  index.html   Downloads         Desktop    0001.pcap

7.递归列出子目录

ls -R选项将列出很长的目录树。请参阅命令输出示例。

`>  ls -R`

total 1384
-rw-------. 1 root     root      33408 Aug  8 17:25 anaconda.log
-rw-------. 1 root     root      30508 Aug  8 17:25 anaconda.program.log

./httpd:
total 132
-rw-r--r--  1 root root     0 Aug 19 03:14 access_log
-rw-r--r--. 1 root root 61916 Aug 10 17:55 access_log-20120812

./lighttpd:
total 68
-rw-r--r--  1 lighttpd lighttpd  7858 Aug 21 15:26 access.log
-rw-r--r--. 1 lighttpd lighttpd 37531 Aug 17 18:21 access.log-20120819

./nginx:
total 12
-rw-r--r--. 1 root root    0 Aug 12 03:17 access.log
-rw-r--r--. 1 root root  390 Aug 12 03:17 access.log-20120812.gz

8. 反转输出顺序

与组合 -ltr 将显示最新的修改文件或目录日期为最后。

`>  ls -ltr`

total 176
-rw-r--r--. 1 root root 11439 Jul 31 02:13 install.log.syslog
-rw-r--r--. 1 root root 48867 Jul 31 02:17 install.log
-rw-------. 1 root root  1586 Jul 31 02:17 anaconda-ks.cfg
drwxr-xr-x. 2 root root  4096 Jul 31 02:48 Desktop
drwxr-xr-x. 2 root root  4096 Jul 31 02:48 Videos
drwxr-xr-x. 2 root root  4096 Jul 31 02:48 Templates
drwxr-xr-x. 2 root root  4096 Jul 31 02:48 Public
drwxr-xr-x. 2 root root  4096 Jul 31 02:48 Pictures
drwxr-xr-x. 2 root root  4096 Jul 31 02:48 Music
drwxr-xr-x. 2 root root  4096 Jul 31 02:48 Documents
-rw-r--r--. 1 root root 46701 Jul 31 09:58 index.html
-rw-r--r--. 1 root root 21262 Aug 12 12:42 fbcmd_update.php
drwxr-xr-x. 4 root root  4096 Aug 16 02:55 Downloads
-rw-r--r--. 1 root root   683 Aug 19 09:59 0001.pcap

9. 按文件大小排序文件

与组合 -lS 按顺序显示文件大小,先显示大的。

`>  ls -lS`

total 176
-rw-r--r--. 1 root root 48867 Jul 31 02:17 install.log
-rw-r--r--. 1 root root 46701 Jul 31 09:58 index.html
-rw-r--r--. 1 root root 21262 Aug 12 12:42 fbcmd_update.php
-rw-r--r--. 1 root root 11439 Jul 31 02:13 install.log.syslog
drwxr-xr-x. 2 root root  4096 Jul 31 02:48 Desktop
drwxr-xr-x. 2 root root  4096 Jul 31 02:48 Documents
drwxr-xr-x. 4 root root  4096 Aug 16 02:55 Downloads
drwxr-xr-x. 2 root root  4096 Jul 31 02:48 Music
drwxr-xr-x. 2 root root  4096 Jul 31 02:48 Pictures
drwxr-xr-x. 2 root root  4096 Jul 31 02:48 Public
drwxr-xr-x. 2 root root  4096 Jul 31 02:48 Templates
drwxr-xr-x. 2 root root  4096 Jul 31 02:48 Videos
-rw-------. 1 root root  1586 Jul 31 02:17 anaconda-ks.cfg
-rw-r--r--. 1 root root   683 Aug 19 09:59 0001.pcap

10.显示文件或目录的inode编号

我们可以看到在文件之前打印了一些数字 /目录名。和-i 选项列表文件 / 具有 inode 编号的目录。

`>  ls -i`

20112 0001.pcap        23610 Documents         23793 index.html          23611 Music     23597 Templates
23564 anaconda-ks.cfg  23595 Downloads            22 install.log         23612 Pictures  23613 Videos
23594 Desktop          23585 fbcmd_update.php     35 install.log.syslog  23601 Public

11.显示ls命令的版本

检查 ls 命令的版本。

`>  ls --version`

ls (GNU coreutils) 8.4
Copyright (C) 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.

12. 显示帮助页面

列出 ls 命令的帮助页面及其选项。

`>  ls --help`

Usage: ls [OPTION]... [FILE]...

13. 列出目录信息

ls -l 命令列出目录下的文件 /tmp. 其中与-ld 参数显示信息 /tmp 目录。

`>  ls -l /tmp`
total 408
drwx------. 2 narad narad   4096 Aug  2 02:00 CRX_75DAF8CB7768
-r--------. 1 root  root  384683 Aug  4 12:28 htop-1.0.1.tar.gz
drwx------. 2 root  root    4096 Aug  4 11:20 keyring-6Mfjnk
drwx------. 2 root  root    4096 Aug 16 01:33 keyring-pioZJr
drwx------. 2 gdm   gdm     4096 Aug 21 11:26 orbit-gdm
drwx------. 2 root  root    4096 Aug 19 08:41 pulse-gl6o4ZdxQVrX
drwx------. 2 narad narad   4096 Aug  4 08:16 pulse-UDH76ExwUVoU
drwx------. 2 gdm   gdm     4096 Aug 21 11:26 pulse-wJtcweUCtvhn
-rw-------. 1 root  root     300 Aug 16 03:34 yum_save_tx-2012-08-16-03-34LJTAa1.yumtx
`>  ls -ld /tmp/`

drwxrwxrwt. 13 root root 4096 Aug 21 12:48 /tmp/

14. 显示文件的 UID 和 GID

显示 UIDGID的文件和目录。使用选项-n 用 ls 命令。

`>  ls -n`

total 36
drwxr-xr-x. 2 500 500 4096 Aug  2 01:52 Downloads
drwxr-xr-x. 2 500 500 4096 Aug  2 01:52 Music
drwxr-xr-x. 2 500 500 4096 Aug  2 01:52 Pictures
-rw-rw-r--. 1 500 500   12 Aug 21 13:06 tmp.txt
drwxr-xr-x. 2 500 500 4096 Aug  2 01:52 Videos

15. ls 命令及其别名

我们为 ls 命令,当我们执行 ls 命令时,它会 -l 选项默认并显示前面提到的长列表。

`>  alias ls="ls -l"`

Note:我们可以使用以下别名命令查看系统中可用的别名数量,并且可以取消别名,如下例所示。

`>  alias`

alias cp='cp -i'
alias l.='ls -d .* --color=auto'
alias ll='ls -l --color=auto'
alias ls='ls --color=auto'
alias mv='mv -i'
alias rm='rm -i'
alias which='alias | /usr/bin/which --tty-only --read-alias --show-dot --show-tilde'

要删除先前定义的别名,只需使用 unalias 命令。

`>  unalias ls`

 

转载请注明:XAMPP中文组官网 » Linux之ls命令示例代码

您必须 登录 才能发表评论!