Linux中 15个基本的“ls”命令举例

Linux中 15个基本的“ls”命令举例

ls命令是Linux系统中使用频率最高的命令之一,我相信当你进入Linux系统提示符后,ls是你第一个要使用的命令。

我们每天频繁的使用ls命令,可是我很少知道它的一些可用选项,并且从来没用过这些选项。这篇文件,我们讨论基础的带有更多参数的ls命令

1. 列表文件,使用没有任何选项的ls

没有任何选项的列出文件和目录,使用这种光秃秃的格式我们查看不到一些例如文件类型、文件大小、文件修改日期和时间,权限和链接等的详细信息

# ls

abcd            Desktop    grepstrings         Music     shell     Videos

anaconda-ks.cfg Documents install.log         Pictures  Templates

data.f          Downloads  install.log.syslog  Public   testfile

2 列表文件使用-l选项

这里,ls –l(是英文字母小写的l,不是阿拉伯数字1)可以显示文件或目录,包括大小,修改日期和时间,文件名或目录名,文件的宿主(文件的拥有者)和宿主的权限

# ls-l

total124

drwxr-xr-x.2 root root 4096 Sep 25 14:31 abcd

-rw-------.1 root root 1344 Sep 21  2015 anaconda-ks.cfg

-rw-r--r--.1 root root  253 Sep 25 15:42 data.f

drwxr-xr-x.2 root root 4096 Sep 20  2015 Desktop

drwxr-xr-x.2 root root 4096 Sep 20  2015 Documents

drwxr-xr-x.2 root root 4096 Sep 20  2015 Downloads

-rw-r--r--.1 root root    7 Sep 25 17:15grepstrings

-rw-r--r--.1 root root 50010 Sep 21 2015install.log

-rw-r--r--.1 root root 10033 Sep 21 2015install.log.syslog

drwxr-xr-x.2 root root 4096 Sep 20  2015 Music

drwxr-xr-x.2 root root 4096 Sep 20  2015 Pictures

drwxr-xr-x.2 root root 4096 Sep 20  2015 Public

drwxr-xr-x.2 root root 4096 Oct  9  2015shell

drwxr-xr-x.2 root root 4096 Sep 20  2015 Templates

-rw-r--r--.1 root root   24 Sep 25 16:59 testfile

drwxr-xr-x.2 root root 4096 Sep 20  2015 Videos

3. 查看隐藏文件

查看所有的文件,包括以.开头的隐藏文件

# ls-a

.               .dbus            .gtk-bookmarks      Public

..              Desktop          .gvfs               .pulse

abcd            Documents        .ICEauthority       .pulse-cookie

.abrt           Downloads        .icons              .purple

anaconda-ks.cfg .esd_auth        .imsettings.log     shell

.bash_history   .gconf           install.log         .ssh

.bash_logout    .gconfd          install.log.syslog  .tcshrc

.bash_profile   .gnome2          .local              Templates

.bashrc         .gnome2_private  .mozilla            testfile

.cache          .gnote           Music               .themes

.config         .gnupg           .nautilus           .thumbnails

.cshrc          grepstrings      Pictures            Videos

data.f          .gstreamer-0.10  .pki                .viminfo

4. 列表文件以人类可读的格式,使用-lh参数

使用结合的-lh选项,以人类可读的格式显示文件大小

# ls-lh

total124K

drwxr-xr-x.2 root root 4.0K Sep 25 14:31 abcd

-rw-------.1 root root 1.4K Sep 21 2015anaconda-ks.cfg

-rw-r--r--.1 root root 253 Sep 25 15:42 data.f

drwxr-xr-x.2 root root 4.0K Sep 20 2015 Desktop

drwxr-xr-x.2 root root 4.0K Sep 20 2015 Documents

drwxr-xr-x.2 root root 4.0K Sep 20 2015 Downloads

-rw-r--r--.1 root root   7 Sep 25 17:15 grepstrings

-rw-r--r--.1 root root 49K Sep 21  2015 install.log

-rw-r--r--.1 root root 9.8K Sep 21 2015install.log.syslog

drwxr-xr-x.2 root root 4.0K Sep 20 2015 Music

drwxr-xr-x.2 root root 4.0K Sep 20 2015 Pictures

drwxr-xr-x.2 root root 4.0K Sep 20 2015 Public

drwxr-xr-x.2 root root 4.0K Oct 9  2015 shell

drwxr-xr-x.2 root root 4.0K Sep 20 2015 Templates

-rw-r--r--.1 root root  24 Sep 25 16:59 testfile

drwxr-xr-x.2 root root 4.0K Sep 20 2015 Videos

5. 列表文件和目录,其中目录以‘/’字符结尾

使用带有-F选项的ls命令,将在目录的结尾加上’/’字符

# ls-F

abcd/           Desktop/    grepstrings         Music/     shell/     Videos/

anaconda-ks.cfg Documents/ install.log         Pictures/  Templates/

data.f          Downloads/  install.log.syslog  Public/   testfile

6. 以降序显示文件

使用ls –r选项,可以降序显示文件和目录

# ls-r

Videos    shell    Music              grepstrings  Desktop          abcd

testfile  Public   install.log.syslog  Downloads    data.f

Templates Pictures install.log         Documents    anaconda-ks.cfg

7. 递归的列表子目录

使用ls -R 选项将显示目录树的长列表,看一下这条目录输出的例

# ls-R

.:

abcd            Desktop    grepstrings         Music     shell     Videos

anaconda-ks.cfg Documents install.log         Pictures  Templates

data.f          Downloads  install.log.syslog  Public   testfile

 

./abcd:

aaa  abc bbb

 

./Desktop:

 

./Documents:

 

./Downloads:

 

./Music:

 

./Pictures:

 

./Public:

 

./shell:

 

./Templates:

 

./Videos:

8. 反向输出序列

使用组合选项 -ltr以修改时间倒序的格式列表文件和目录

# ls -ltr

total 124

drwxr-xr-x. 2 root root 4096 Sep 20 2015 Desktop

drwxr-xr-x. 2 root root 4096 Sep 20 2015 Videos

drwxr-xr-x. 2 root root 4096 Sep 20 2015 Templates

drwxr-xr-x. 2 root root 4096 Sep 20 2015 Public

drwxr-xr-x. 2 root root 4096 Sep 20 2015 Pictures

drwxr-xr-x. 2 root root 4096 Sep 20 2015 Music

drwxr-xr-x. 2 root root 4096 Sep 20 2015 Downloads

drwxr-xr-x. 2 root root 4096 Sep 20 2015 Documents

-rw-r--r--. 1 root root 10033Sep 21 2015 install.log.syslog

-rw-r--r--. 1 root root 50010Sep 21 2015 install.log

-rw-------. 1 root root 1344 Sep 21 2015 anaconda-ks.cfg

drwxr-xr-x. 2 root root 4096 Oct 9  2015 shell

drwxr-xr-x. 2 root root 4096 Sep 25 14:31 abcd

-rw-r--r--. 1 root root  253 Sep 25 15:42 data.f

-rw-r--r--. 1 root root   24 Sep 25 16:59 testfile

-rw-r--r--. 1 root root    7 Sep 25 17:15 grepstrings

9. 以文件大小进行排序SortFiles by File Size

使用组合选项-lS 对文件的大小进行排序,第一个文件是尺寸最大的

# ls-lS

total124

-rw-r--r--.1 root root 50010 Sep 21 2015install.log

-rw-r--r--.1 root root 10033 Sep 21 2015install.log.syslog

drwxr-xr-x.2 root root 4096 Sep 25 14:31 abcd

drwxr-xr-x.2 root root 4096 Sep 20  2015 Desktop

drwxr-xr-x.2 root root 4096 Sep 20  2015 Documents

drwxr-xr-x.2 root root 4096 Sep 20  2015 Downloads

drwxr-xr-x.2 root root 4096 Sep 20  2015 Music

drwxr-xr-x.2 root root 4096 Sep 20  2015 Pictures

drwxr-xr-x.2 root root 4096 Sep 20  2015 Public

drwxr-xr-x.2 root root 4096 Oct  9  2015shell

drwxr-xr-x.2 root root 4096 Sep 20  2015 Templates

drwxr-xr-x.2 root root 4096 Sep 20  2015 Videos

-rw-------.1 root root 1344 Sep 21  2015 anaconda-ks.cfg

-rw-r--r--.1 root root  253 Sep 25 15:42 data.f

-rw-r--r--.1 root root   24 Sep 25 16:59 testfile

-rw-r--r--.1 root root    7 Sep 25 17:15grepstrings

10.显示文件或目录的结点数Display Inode number of File or Directory

我们可以看到在文件或目录的前面显示了一些数字,用使用-i选项可以查看文件和目录的节点数。

# ls -i

652842 abcd            664201 data.f   664012 Documents  664406 grepstrings  652803 install.log.syslog  664014 Pictures  664230 shell      664141 testfile

663994 anaconda-ks.cfg 664008 Desktop  664009 Downloads  652802 install.log  664013 Music               664011 Public    664010 Templates  664015 Videos

11.显示ls命令的版本号

查看ls命令的版本.

# ls--vision

ls:unrecognized option '--vision'

Try`ls --help' for more information.

[root@test18~]# ls --version

ls(GNU coreutils) 8.4

Copyright(C) 2010 Free Software Foundation, Inc.

LicenseGPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>.

Thisis free software: you are free to change and redistribute it.

Thereis NO WARRANTY, to the extent permitted by law.

 

Writtenby Richard M. Stallman and David MacKenzie. (竟然是理查德.斯托曼这位长发侠写的)

12. 查看帮助页面

使用这个选项,可以查看ls命令的帮助页面

# ls--help

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

Listinformation about the FILEs (the current directory by default).

Sortentries alphabetically if none of -cftuvSUX nor --sort.

 

Mandatoryarguments to long options are mandatory for short options too.

  -a, --all                  do not ignore entries starting with .

  -A, --almost-all           do not list implied . and ..

      --author               with -l, print the author ofeach file

  -b, --escape               print octal escapes fornongraphic characters

(此处省略若干字。。)

13. 显示目录信息

使用ls –l命令显示/tmp目录下的文件列表

# ls -l /tmp

total 60

drwx------. 2 root root 4096 Sep25 14:24 keyring-nB3DVI

drwx------. 2 root root 4096 Oct14 2015 keyring-PiINCf

drwx------. 2 root root 4096 Sep22 09:14 keyring-V7JnhH

drwx------. 2 gdm gdm 4096 Sep 25 14:24 orbit-gdm

drwx------. 2 root root 4096 Sep25 14:24 orbit-root

drwx------. 2 ysp ysp 4096 Sep 22 12:18 pulse-EWl1d0suWriC

drwx------. 2 gdm gdm 4096 Sep 25 14:24 pulse-Kvocbq2FtDGa

drwx------. 2 root root 4096 Sep25 14:24 pulse-Po2CyLGcobxQ

drwx------. 2 root root 4096 Sep22 10:28 tmpeQWvou

drwx------. 2 root root 4096 Sep25 14:24 virtual-root.ERHUmg

drwx------. 2 root root 4096 Sep22 09:32 virtual-root.FnfsuU

drwx------. 2 root root 4096 Sep22 09:14 virtual-root.IwhpRj

drwx------. 2 root root 4096 Sep22 08:49 virtual-root.Yevghu

drwx------. 2 ysp ysp 4096 Sep 22 09:25 virtual-ysp.FcNTcI

drwx------. 2 ysp ysp 4096 Sep 22 03:37 virtual-ysp.VYs9Pl

使用 –ld参数显示/tmp目录的信息

# ls-ld /tmp

drwxrwxrwt.21 root root 4096 Sep 25 18:10 /tmp

14. 显示文件的UID和GID

为了显示文件和目录的UID和GID,可以使用ls 命令的-n选项

# ls-n

total124

drwxr-xr-x.2 0 0 4096 Sep 25 14:31 abcd

-rw-------.1 0 0 1344 Sep 21  2015 anaconda-ks.cfg

-rw-r--r--.1 0 0  253 Sep 25 15:42 data.f

drwxr-xr-x.2 0 0 4096 Sep 20  2015 Desktop

drwxr-xr-x.2 0 0 4096 Sep 20  2015 Documents

drwxr-xr-x.2 0 0 4096 Sep 20  2015 Downloads

-rw-r--r--.1 0 0    7 Sep 25 17:15 grepstrings

-rw-r--r--.1 0 0 50010 Sep 21 2015 install.log

-rw-r--r--.1 0 0 10033 Sep 21 2015install.log.syslog

drwxr-xr-x.2 0 0 4096 Sep 20  2015 Music

drwxr-xr-x.2 0 0 4096 Sep 20  2015 Pictures

drwxr-xr-x.2 0 0 4096 Sep 20  2015 Public

drwxr-xr-x.2 0 0 4096 Oct  9  2015shell

drwxr-xr-x.2 0 0 4096 Sep 20  2015 Templates

-rw-r--r--.1 0 0   24 Sep 25 16:59 testfile

drwxr-xr-x.2 0 0 4096 Sep 20  2015 Videos

15. ls 命令和它的别名(Aliases)

我们可以为ls命令建立别名,例如输入ls命令就相当输入ls –l命令

#alias ls="ls -l"

注意:我们可以使用下面的命令查看系统中的可用别名。

# alias

alias cp='cp -i'

alias l.='ls -d .* --color=auto'

alias ll='ls -l --color=auto'

alias ls='ls -l'

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

I

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值