ls 命令详解

1、ls基本语法及选项

用法:ls [选项]... [文件]...
List information about the FILEs (the current directory by default).
Sort entries alphabetically if none of -cftuvSUX nor --sort is specified.
长选项必须使用的参数对于短选项时也是必需使用的。
  -a, --all                     不隐藏任何以. 开始的项目
  -A, --almost-all              列出除. 及.. 以外的任何项目
      --author                  与-l 同时使用时列出每个文件的作者
  -b, --escape                  以八进制溢出序列表示不可打印的字符
      --block-size=SIZE      scale sizes by SIZE before printing them.  E.g.,
                               `--block-size=M' prints sizes in units of
                               1,048,576 bytes.  See SIZE format below.
  -B, --ignore-backups       do not list implied entries ending with ~
  -c                         with -lt: sort by, and show, ctime (time of last
                               modification of file status information)
                               with -l: show ctime and sort by name
                               otherwise: sort by ctime, newest first
  -C                            每栏由上至下列出项目
      --color[=WHEN]            控制是否使用色彩分辨文件。WHEN 可以是
                                "never"(默认)、"always""auto"其中之一
  -d, --directory               当遇到目录时列出目录本身而非目录内的文件
  -D, --dired                   产生适合Emacs 的dired 模式使用的结果
  -f                            不进行排序,-aU 选项生效,-lst 选项失效
  -F, --classify                加上文件类型的指示符号(*/=@| 其中一个)
      --format=关键字           交错-x,逗号分隔-m,水平-x,长-l,
                                单栏-1,详细-l,垂直-C
      --full-time               即-l --time-style=full-iso
  -g                            类似-l,但不列出所有者
      --group-directories-first
                        在文件前分组目录。此选项可与--sort 一起使用,
                        但是一旦使用--sort=none (-U)将禁用分组
  -G, --no-group                以一个长列表的形式,不输出组名
  -h, --human-readable          与-l 一起,以易于阅读的格式输出文件大小
                                (例如 1K 234M 2G)
      --si                      同上面类似,但是使用1000 为基底而非1024
  -H, --dereference-command-line
                                跟随命令行列出的符号链接
      --dereference-command-line-symlink-to-dir
                                跟随命令行列出的目录的符号链接
      --hide=PATTERN            隐藏符合PATTERN 模式的项目
                                (-a 或 -A 将覆盖此选项)
      --indicator-style=方式    指定在每个项目名称后加上指示符号方式:
                                none (默认),classify (-F),file-type (-p)
  -i, --inode                   显示每个文件的inode 号
  -I, --ignore=PATTERN          不显示任何符合指定shell PATTERN 的项目
  -k                            即--block-size=1K
  -l                            使用较长格式列出信息
  -L, --dereference             当显示符号链接的文件信息时,显示符号链接所指示
                                的对象而并非符号链接本身的信息
  -m                            所有项目以逗号分隔,并填满整行行宽
  -n, --numeric-uid-gid         类似 -l,但列出UID 及GID 号
  -N, --literal                 输出未经处理的项目名称 (如不特别处理控制字符)
  -o                            类似 -l,但不列出有关组的信息
  -p,  --indicator-style=slash  对目录加上表示符号"/"
  -q, --hide-control-chars      以"?"字符代替无法打印的字符
      --show-control-chars      直接显示无法打印的字符 (这是默认方式,除非调用
                                的程序名称是"ls"而且是在终端输出结果)
  -Q, --quote-name              将条目名称括上双引号
      --quoting-style=方式      使用指定的quoting 方式显示条目的名称:
                                literal、locale、shell、shell-always、c、escape
  -r, --reverse                 逆序排列
  -R, --recursive               递归显示子目录
  -s, --size                    以块数形式显示每个文件分配的尺寸
  -S                            根据文件大小排序
      --sort=WORD               以下是可选用的WORD 和它们代表的相应选项:
                                extension -X       status   -c
                                none      -U       time     -t
                                size      -S       atime    -u
                                time      -t       access   -u
                                version   -v       use      -u
      --time=WORD               和-l 同时使用时显示WORD 所代表的时间而非修改时
                                间:atime、access、use、ctime 或status;加上
                                --sort=time 选项时会以指定时间作为排序关键字
      --time-style=STYLE        和-l 同时使用时根据STYLE 代表的格式显示时间:
                                full-iso、iso、locale、posix-iso、+FORMAT。
                                FORMAT 即是"date"所用的时间格式;如果FORMAT
                                是FORMAT1<换行>FORMAT2,FORMAT1 适用于较旧
                                的文件而FORMAT2 适用于较新的文件;如果STYLE
                                以"posix-"开头,则STYLE 仅在POSIX 语系之外
                                生效。
  -t                            根据修改时间排序
  -T, --tabsize=宽度    指定制表符(Tab)的宽度,而非8 个字符
  -t                         sort by modification time, newest first
  -T, --tabsize=COLS         assume tab stops at each COLS instead of 8
  -u                    同-lt 一起使用:按照访问时间排序并显示
                        同-l一起使用:显示访问时间并按文件名排序
                        其他:按照访问时间排序
  -U                    不进行排序;按照目录顺序列出项目
  -v                    在文本中进行数字(版本)的自然排序
  -w, --width=COLS      自行指定萤幕宽度而不使用目前的数值
  -x                    逐行列出项目而不是逐栏列出
  -X                    根据扩展名排序
  -1                    每行只列出一个文件
      --help            显示此帮助信息并退出
      --version         显示版本信息并退出

2、常用ls 命令

[root@rusky /]# ls -a    #列出所有文件,包括隐藏文件,
.    123.txt       bin   etc   lib           media  net   root     share  test      testoutput    tmp
..   .autofsck     boot  fuck  lost+found    misc   opt   sbin     srv    testdir   .test.swp     usr
123  .autorelabel  dev   home  lsoutputtest  mnt    proc  selinux  sys    testmth4  testyear2014  var
[root@rusky /]# ls -A    #同上,不包括当前工作目录(.)和父目录(..)
123        .autorelabel  dev   home        lsoutputtest  mnt  proc  selinux  sys      testmth4    testyear2014  var
123.txt    bin           etc   lib         media         net  root  share    test     testoutput  tmp
.autofsck  boot          fuck  lost+found  misc          opt  sbin  srv      testdir  .test.swp   usr

[root@rusky iamldap]# ls
dps6  ds6  dscc6  dsee6  dsrk6  etc  jre  var
[root@rusky iamldap]# ls var
dscc6
[root@rusky iamldap]# ls var/dscc6
dcc  dscc.war  tmp
[root@rusky iamldap]# ls .  #显示当前工作目录内容
dps6  ds6  dscc6  dsee6  dsrk6  etc  jre  var
[root@rusky iamldap]# ls ..
iamldap
[root@rusky iamldap]# ls ../..     #列出当前目录的父目录内容
[root@rusky iamldap]# e-tomcat-6.0.37  jdk-6u20-linux-i586.bin  kng   Legal       rusky2   user1
[root@rusky iamldap]# 6.0_20           king 
[root@rusky dscc6]# ls -1    #数字1,以单列形式显示
bin
dccapp
etc
lib
[root@rusky dscc6]# ls -l     #经常用来查看文件的权限信息
total 16
drwxr-xr-x 2 root root 4096 Mar 10  2008 bin
drwxr-xr-x 8 root root 4096 Mar 10  2008 dccapp
drwxr-xr-x 2 root root 4096 Mar 10  2008 etc
drwxr-xr-x 5 root root 4096 Apr 22 08:23 lib
[root@rusky ds6]# ls -R bin    #列出当前目录的bin目录下的所有子目录及子目录里的文件
bin:
dsadm   dsmig     fildif  mmldif            ns-activate    password  repldisc     testdir
dsconf  entrycmp  insync  ns-accountstatus  ns-inactivate  pwdhash   schema_push

bin/testdir:
test  test.txt
[root@rusky /]# ls -F    #列出当前目录下的文件类型
123      boot/  fuck   lost+found/   misc/  opt/   sbin/     srv/   testdir/    testyear2014  var/
123.txt  dev/   home/  lsoutputtest  mnt/   proc/  selinux/  sys/   testmth4    tmp/
bin/     etc/   lib/   media/        net/   root/  share@    test/  testoutput  usr/
[root@rusky iamldap]# ls -1 -i     #列出文件的inode值,默认按首字母从a-z排序
1605288 dps6
1604079 ds6
1605250 dscc6
1538595 dsee6
1538692 dsrk6
1604193 etc
1538953 jre
1605267 var
[root@rusky iamldap]# ls -1ir     #r,逆序输出
1605267 var
1540291 test.txt
1540292 test2.txt
1538953 jre
1604193 etc
1538692 dsrk6
1538595 dsee6
1605250 dscc6
1604079 ds6
1605288 dps6
[root@rusky iamldap]# ls *.txt    #可使用通配符,如列出当前目录下的后缀为.txt的所有文件
test2.txt  test.txt

 其它:

[root@OracleTest testdb]# ls -lh  --输出文件大小
total 24G
-rw-r----- 1 oracle oinstall  11G Sep 12 17:29 test01.dbf
-rw-r----- 1 oracle oinstall  11G Sep 12 17:29 test02.dbf
-rw-r----- 1 oracle oinstall 9.3M Sep 12 17:29 control01.ctl
-rw-r----- 1 oracle oinstall 101M Sep 12 17:29 rusky_data_01.dbf
-rw-r----- 1 oracle oinstall  51M Sep 11 13:47 rusky_temp_01.dbf
-rw-r----- 1 oracle oinstall 346M Sep 12 17:29 example01.dbf
-rw-r----- 1 oracle oinstall 101M Sep 12 17:29 lxj_data_01.dbf
-rw-r----- 1 oracle oinstall  51M Sep 11 13:40 lxj_temp_01.dbf
-rw-r----- 1 oracle oinstall  51M Sep 12 17:27 redo01.log
-rw-r----- 1 oracle oinstall  51M Sep 12 17:29 redo02.log
-rw-r----- 1 oracle oinstall  51M Sep 12 17:29 redo03.log
-rw-r----- 1 oracle oinstall 1.1G Sep 12 17:29 sysaux01.dbf
-rw-r----- 1 oracle oinstall 1.5G Sep 12 17:29 system01.dbf
-rw-r----- 1 oracle oinstall 4.1G Sep 12 17:25 temp01.dbf
-rw-r----- 1 oracle oinstall 231M Sep 12 17:29 undotbs01.dbf
-rw-r----- 1 oracle oinstall 5.1M Sep 12 17:29 users01.dbf

[root@OracleTest testdb]# ls -sh   --同上,横排列,输出文件大小
total 24G
 11G test01.dbf      101M rusky_data_01.dbf  101M lxj_data_01.dbf   51M redo02.log    1.5G system01.dbf   5.1M users01.dbf
 11G test02.dbf      1.1M rusky_temp_01.dbf  1.1M lxj_temp_01.dbf   51M redo03.log    4.1M temp01.dbf
9.4M control01.ctl  346M example01.dbf           51M redo01.log          1.1G sysaux01.dbf  231M undotbs01.dbf

[root@OracleTest testdb]# ls -m  --以逗号分隔
test01.dbf, test02.dbf, control01.ctl, rusky_data_01.dbf, rusky_temp_01.dbf, example01.dbf, lxj_data_01.dbf,
lxj_temp_01.dbf, redo01.log, redo02.log, redo03.log, sysaux01.dbf, system01.dbf, temp01.dbf, undotbs01.dbf, users01.dbf

[root@OracleTest testdb]# ls -rm  --按首字母逆序排序, -r 逆序
users01.dbf, undotbs01.dbf, temp01.dbf, system01.dbf, sysaux01.dbf, redo03.log, redo02.log, redo01.log, lxj_temp_01.dbf,
lxj_data_01.dbf, example01.dbf, rusky_temp_01.dbf, rusky_data_01.dbf, control01.ctl, test02.dbf, test01.dbf

[root@OracleTest testdb]# ls -Q   -- -Q文件名加上双引号
"test01.dbf"      "rusky_data_01.dbf"  "lxj_data_01.dbf"  "redo02.log"    "system01.dbf"   "users01.dbf"
"test02.dbf"      "rusky_temp_01.dbf"  "lxj_temp_01.dbf"  "redo03.log"    "temp01.dbf"
"control01.ctl"  "example01.dbf"          "redo01.log"          "sysaux01.dbf"  "undotbs01.dbf"

[root@OracleTest testdb]# ls -sSh  -- -S按照文件大小,从大到小排列
total 24G
 11G test01.dbf   1.5G system01.dbf   231M undotbs01.dbf          1.1M rusky_temp_01.dbf   51M redo02.log     5.1M users01.dbf
 11G test02.dbf   1.1G sysaux01.dbf   101M rusky_data_01.dbf  1.1M lxj_temp_01.dbf      51M redo03.log
4.1M temp01.dbf  346M example01.dbf  101M lxj_data_01.dbf      51M redo01.log             9.4M control01.ctl

[root@OracleTest testdb]# ls -srSh   -- -r逆序,按从小到大排列
total 24G
5.1M users01.dbf     51M redo02.log          1.1M rusky_temp_01.dbf  231M undotbs01.dbf  1.5G system01.dbf   11G test01.dbf
9.4M control01.ctl   51M redo01.log          101M lxj_data_01.dbf     346M example01.dbf  4.1M temp01.dbf
 51M redo03.log     1.1M lxj_temp_01.dbf  101M rusky_data_01.dbf  1.1G sysaux01.dbf    11G test02.dbf
 
[root@OracleTest testdb]# 
[root@OracleTest oraInventory]# ls -R  --递归输出目录内容
.:
ContentsXML  logs  oraInst.loc  orainstRoot.sh  oui

./ContentsXML:
comps.xml  inventory.xml  libs.xml

./logs:
installActions2016-09-10_12-52-24PM.log  oraInstall2016-09-10_12-52-24PM.err  oraInstall2016-09-10_12-52-24PM.out

./oui:
srcs.lst
[root@OracleTest oraInventory]# 

  

原文地址:https://www.cnblogs.com/rusking/p/3766690.html