列出目录 ls

一、命令详解


1.命令说明

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


2.语法格式

ls       [option]      [file]
ls       [选项]        [文件或目录]

3.选项描述

DESCRIPTION
 -a, --all                  do not ignore entries starting with .    
                           #等同于 –all, 列出目录下的所有文件,包括以 . 开头的隐藏文件。
 -A, --almost-all           do not list implied . and ..
      --author               with -l, print the author of each file
                           #显示指定目录下所有子目录与文件,包括隐藏文件。但不列出“.”和 “..”。
 -b, --escape               print C-style escapes for nongraphic characters  
                           #对文件名中的不可显示字符用八进制逃逸字符显示。
                               # 块以指定<大小>的字节为单位
  -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   
                               # 输出结果以文件的 ctime (文件状态最后更改的时间) 排序。
  -C                         list entries by columns  
                               #分成多列的顺序输出文件列表;
      --color[=WHEN]         colorize the output; WHEN can be 'never', 'auto', 
                               or 'always' (the default); more info below   
                               #是否使用不同的颜色区分文件类型,可能的值:
                                 never:从不;
                                 always:总是;
                                 auto:自动。
                                 例如:ls is aliased to ls `--color=auto`
  -d, --directory            list directories themselves, not their contents  
                               #显示目录名称而非其内容。
  -D, --dired                generate output designed for Emacs dired mode  
                               #用Emacs的模式设计产生文件和目录列表。
  -f                         do not sort, enable -aU, disable -ls --color      
                               #对输出的文件不进行排序,-aU 选项生效,-lst 选项失效。
  -F, --classify             append indicator (one of */=>@|) to entries 
   #附加文件类型的指示符号 (*/=@| 其中一个) 在目录名后面标记“/”,可执行文件后面标记“*”,符号链接后面标记 “@”,管道(或FIFO)后面标记“|”,socket文件后面标记“=”。
  -g                         like -l, but do not list owner   
                              #类似 -l,但不列出所有者。
  -G, --no-group             in a long listing, don’t print group names 
                              #不列出任何有关组的信息
  -h, --human-readable       with -l, print sizes in human readable format  
  #与 -l 配合使用,如 ls -lh以容易理解的格式列出文件大小,大小加单位,如 10k、20M、100G。
  -H, --dereference-command-line
                             follow symbolic links listed on the command line 
                              #使用命令列中的符号链接指示的 真正目的地
  -i, --inode                print the index number of each file 
                              #打印出每个文件的 inode 号
  -I, --ignore=PATTERN       do not list implied entries matching shell PATTERN 
                              # 不列出符合范本样式的文件或目录名称。
  -k, --kibibytes            default to 1024-byte blocks for disk usage 
                              #此参数的效果和指定"block-size=1024"参数相同。
  -l                         use a long listing format
  #使用详细格式列表,除了文件名之外,还将文件的权限、所有者、文件大小等信息详细列出来。ls -l 等于 ll
  -L, --dereference          when showing file information for a symbolic 
                               link, show information for the file the link
# 当显示符号链接的文件信息时,显示符号链接所指示的对象而并非符号链接本身的信息 
  -m                         fill width with a comma separated list of entries 
                              #用","号区隔每个文件和目录的名称。
  -n, --numeric-uid-gid      like -l, but list numeric user and group IDs  
                              #以用户识别码和群组识别码替代其名称。
  -N, --literal              print raw entry names (don’t treat e.g. control  
                              #接列出文件和目录名称,包括控制字符。
                               characters specially)
  -o                         like -l, but do not list group information
                             # 类似 -l,显示文件的除组信息外的详细信息。
  -p, --indicator-style=slash
                             append / indicator to directories 
                             #向目录追加/指示器
  -q, --hide-control-chars   print ? instead of nongraphic characters
      --show-control-chars   show nongraphic characters as-is (the default,
                               unless program is 'ls' and output is a terminal)
                               #用"?"号取代控制字符,列出文件和目录名称。
  -Q, --quote-name           enclose entry names in double quotes
      --quoting-style=WORD   use quoting style WORD for entry names:
                               literal, locale, shell, shell-always, c, escape
                               #把文件和目录名称以""号标示起来。
  -r, --reverse              reverse order while sorting 
                               #依相反次序排列-倒叙
  -R, --recursive            list subdirectories recursively
                               #递归处理,将指定目录下的所有文件及子目录一并处理。
  -s, --size                 print the allocated size of each file, in blocks   
                               # 以块大小为单位列出所有文件的大小
  -S                         sort by file size  #根据文件大小排序 
  -t                         sort by modification time, newest first 
                               #用文件和目录的更改时间排序
  -T, --tabsize=COLS         assume tab stops at each COLS instead of 8   
                               #显示我文件或目录最后被访问的时间
  -u                         with -lt: sort by, and show, access time;
                               with -l: show access time and sort by name;
                               #以最后存取时间排序,显示文件和目录。
                               #同-lt 一起使用:按照访问时间排序并显示。
	                           #同-l一起使用:显示访问时间并按文件名排序。
	                           #其他:按照访问时间排序。
                               otherwise: sort by access time
  -U                         do not sort; list entries in directory order  
                               #不进行排序;依文件系统原有的次序列出项目
  -v                         natural sort of (version) numbers within text  
                               #根据版本进行排序
  -w, --width=COLS           assume screen width instead of current value   
                              #设置每列的最大字符数。
  -x                         list entries by lines instead of by columns  
                              # 逐行列出项目而不是逐栏列出 
  -X                         sort alphabetically by entry extension 
                              #根据扩展名排序
  -1                         list one file per line。
                              #每行只列出一个文件 

二、命令示例


注意:
ls -l 的别名是ll,常用ll再加别的参数。

案例1:查看当前目录下的文件和目录#ls

[root@centos7 ~]#ls
d3       Documents  f[1-9][a-b]  f333   Pictures  Templates
Desktop  Downloads  f{1-9}{a-b}  Music  Public    Videos

案例2:查看文件列表#ll

[root@centos7 app]#ll
total 32
drwxr-xr-x. 2 root root    6 Dec  8 13:53 dir1
-rw-r--r--. 1 root root   19 Dec 12 17:32 f1
lrwxrwxrwx. 1 root root    2 Dec  7 21:57 f1_softlink -> f1
[root@centos7 app]#type ll
ll is aliased to `ls -l --color=auto'

案例3:按照修改时间排序#ll-t

[root@centos7 app]#ll -t
total 32
-rw-r--r--. 2 root root    4 Dec 13 10:47 f2
-rw-r--r--. 2 root root    4 Dec 13 10:47 f2_hardlink
-rw-r--r--. 1 root root   19 Dec 12 17:32 f1
-rwxrwxrwx. 1 root root 1703 Dec 11 21:14 merrys
[root@centos7 app]#ls -lt
total 32
-rw-r--r--. 2 root root    4 Dec 13 10:47 f2
-rw-r--r--. 2 root root    4 Dec 13 10:47 f2_hardlink
-rw-r--r--. 1 root root   19 Dec 12 17:32 f1
-rwxrwxrwx. 1 root root 1703 Dec 11 21:14 merrys

案例4:按照时间倒叙排列#ll -tr

[root@centos7 app]#ll -tr
total 32
-rw-r--r--. 1 root root  329 Nov 17 22:34 ls.log
drwx------. 3 tom  tom    78 Nov 20 18:24 tomhome
lrwxrwxrwx. 1 root root    2 Dec  7 21:57 f1_softlink -> f1
prw-r--r--. 1 root root    0 Dec  7 23:14 fifo_file
drwxr-xr-x. 2 root root    6 Dec  8 13:53 dir1
-rw-r--r--. 1 root root   88 Dec  8 22:25 fi
-rw-r--r--. 1 root root   88 Dec  8 22:39 fo
-rw-r--r--. 1 root root  114 Dec  9 14:54 f4
-rwxrwxrwx. 1 root root 1703 Dec 11 21:14 merrys
-rw-r--r--. 1 root root   19 Dec 12 17:32 f1
-rw-r--r--. 2 root root    4 Dec 13 10:47 f2_hardlink
-rw-r--r--. 2 root root    4 Dec 13 10:47 f2

案例5:按照文件大小排列#ll -S

[root@centos7 app]#ll -S
total 32
-rwxrwxrwx. 1 root root 1703 Dec 11 21:14 merrys
-rw-r--r--. 1 root root  329 Nov 17 22:34 ls.log
-rw-r--r--. 1 root root  114 Dec  9 14:54 f4
-rw-r--r--. 1 root root   88 Dec  8 22:25 fi
-rw-r--r--. 1 root root   88 Dec  8 22:39 fo
drwx------. 3 tom  tom    78 Nov 20 18:24 tomhome
-rw-r--r--. 1 root root   19 Dec 12 17:32 f1
drwxr-xr-x. 2 root root    6 Dec  8 13:53 dir1
-rw-r--r--. 2 root root    4 Dec 13 10:47 f2
-rw-r--r--. 2 root root    4 Dec 13 10:47 f2_hardlink
lrwxrwxrwx. 1 root root    2 Dec  7 21:57 f1_softlink -> f1
prw-r--r--. 1 root root    0 Dec  7 23:14 fifo_file

案例6:按照比较方便阅读方式显示文件大小#ll -hS

-h给大小加单位,S是按文件大小排列,更容易方便阅读。

[root@centos7 app]#ll -hS
total 32K
-rwxrwxrwx. 1 root root 1.7K Dec 11 21:14 merrys
-rw-r--r--. 1 root root  329 Nov 17 22:34 ls.log
-rw-r--r--. 1 root root  114 Dec  9 14:54 f4
-rw-r--r--. 1 root root   88 Dec  8 22:25 fi

案例7:结尾处显示文件类型#ll -F

给文件名末尾添加一个标识符(/=@|中的一个):
  “*”:可执行文件;
  “/”:目录;
  “=”:socket文件;
  “@”:符号链接(symbolic link)文件;
  “|”:FIFOs文件;

[root@centos7 app]#ll -F
total 32
drwxr-xr-x. 2 root root    6 Dec  8 13:53 dir1/
-rw-r--r--. 1 root root   19 Dec 12 17:32 f1
lrwxrwxrwx. 1 root root    2 Dec  7 21:57 f1_softlink -> f1
-rw-r--r--. 2 root root    4 Dec 13 10:47 f2
-rw-r--r--. 2 root root    4 Dec 13 10:47 f2_hardlink
-rw-r--r--. 1 root root  114 Dec  9 14:54 f4
-rw-r--r--. 1 root root   88 Dec  8 22:25 fi
prw-r--r--. 1 root root    0 Dec  7 23:14 fifo_file|
-rw-r--r--. 1 root root   88 Dec  8 22:39 fo
-rw-r--r--. 1 root root  329 Nov 17 22:34 ls.log
-rwxrwxrwx. 1 root root 1703 Dec 11 21:14 merrys*
drwx------. 3 tom  tom    78 Nov 20 18:24 tomhome/

案例8:递归列出指定文件路径#ls -lR dir1

ls -l -R dir1,在这条命令中,使用到了两个参数,分别为“l”和“R”,可以把他们放在一起使用,ls -lR dir1,最简单的为ll -R dir1

[root@centos7 app]#ls -lR dir1
dir1:
total 0
drwxr-xr-x. 3 root root 18 Dec 13 11:15 dir2

dir1/dir2:
total 0
drwxr-xr-x. 3 root root 18 Dec 13 11:15 dir3

dir1/dir2/dir3:
total 0
drwxr-xr-x. 3 root root 18 Dec 13 11:15 dir4

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

[root@centos7 app]#ls -l * |grep "^-"|wc -l
8
[root@centos7 app]#ls -l * |grep "^d"|wc -l
2

<think>好的,用户想知道如何用ls命令仅列出目录。首先,我得回忆一下ls的各种选项。记得ls本身不带参数的话,只会列出文件和目录,但不区分类型。用户需要的是仅显示子目录,而不是文件或其他类型的条目。 接下来,我应该考虑如何过滤出目录ls有一个-F选项,会在目录后面添加斜杠/,这样可能有用。然后结合grep来筛选出带有斜杠的行,这样就能只显示目录了。比如ls -F | grep '/$',不过这个方法可能在脚本中使用较多,用户可能更希望用ls本身的选项。 另一个方法是使用-l长格式选项,因为目录在权限字符串的第一个字符是d。比如ls -l | grep '^d',这样可以筛选出目录。不过这样会显示详细信息,用户可能只需要目录名,不需要其他信息。不过可以结合awk来提取目录名,例如ls -l | grep '^d' | awk '{print $9}',但这样可能有点麻烦。 还有没有更直接的选项呢?记得ls的-d选项可以防止递归列出目录内容,但这里用户需要的是列出当前目录下的子目录。可能还需要结合通配符,比如ls -d */,这里的*/会匹配所有目录,然后-d选项让ls不进入目录内部。这个方法应该更简洁,直接列出目录名,而不会显示文件。 另外,可能需要考虑隐藏目录的情况。如果用户希望包括以点开头的隐藏目录,可以结合-a选项,比如ls -ad .*/ */,不过这可能比较复杂,需要分步处理。 总结下来,最常用的方法应该是ls -d */,这个命令简单有效,直接列出所有子目录。另外,用户如果想显示详细信息,可以加上-l选项,比如ls -ld */,这样可以看到每个目录的详细信息,如权限、所有者、大小等。 还需要检查是否有其他选项或组合可能更合适。例如,使用find命令结合-type d,但用户可能更希望用ls本身的功能。此外,确保这些命令在不同环境下的兼容性,比如在bash或zsh中的表现是否一致。 最后,需要验证这些方法是否正确。比如在终端中测试ls -d */是否确实只显示目录,而不包含文件。同时,提醒用户注意通配符的处理,确保当前目录下有子目录存在,否则可能会报错。 可能用户还想知道如何递归列出目录,这时候可以结合-R选项,但用户的问题仅限当前目录下的子目录,所以暂时不需要涉及递归。不过作为扩展,可以提到如果需要递归的话,可以结合find命令。 总之,主要的解决方案是使用ls -d */来列出当前目录下的所有子目录,或者结合-l选项显示详细信息。同时,也可以使用grep过滤长格式输出来达到目的。</think>要使用`ls`命令仅列出当前目录下的子目录,可以通过以下方法实现: ### 方法一:使用通配符匹配目录标识 ```bash ls -d */ ``` - `-d`选项:仅显示目录本身(不展开目录内容) - `*/`通配符:匹配所有以斜杠结尾的条目(即目录)[^1][^3] ### 方法二:使用长格式过滤目录类型 ```bash ls -l | grep '^d' ``` - `-l`选项:显示详细信息 - `grep '^d'`:过滤以字母`d`开头的行(目录的权限标识)[^4] ### 方法三:结合查找命令递归显示 ```bash find . -maxdepth 1 -type d ``` - `-maxdepth 1`:仅搜索当前目录 - `-type d`:过滤目录类型[^2] ### 效果对比演示 普通`ls`命令输出: ``` file.txt dir1 dir2 image.jpg ``` 使用`ls -d */`输出: ``` dir1/ dir2/ ```
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Cyan_Jiang

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值