列出文件名和目录(ls)
- 语法格式
ls [选项] [文件]
ls 默认进行全加载 且sort排序展示
- 【 -f 】不排序目录内容;按它们在磁盘上存储的顺序列出。同时启 动“ -a ”选项
- 【 -1 】每行显示一条记录,即单列展示数据
[root@test ~]# ls -1
anaconda-ks.cfg
Desktop
Documents
Downloads
Music
Pictures
Public
Templates
Videos
- 【 -l 】长列表显示文件和目录,包括文件类型、大小、修改日期和时间、权限等信息
[root@test ~]# ls -l
total 12
-rw-------. 1 root root 8619 May 6 2014 anaconda-ks.cfg
drwxr-xr-x. 3 root root 19 May 14 05:20 Desktop
drwxr-xr-x. 2 root root 6 May 8 13:56 Documents
drwxr-xr-x. 2 root root 6 May 8 13:56 Downloads
drwxr-xr-x. 2 root root 6 May 8 13:56 Music
drwxr-xr-x. 2 root root 6 May 8 13:56 Pictures
drwxr-xr-x. 2 root root 6 May 8 13:56 Public
drwxr-xr-x. 2 root root 6 May 8 13:56 Templates
drwxr-xr-x. 2 root root 6 May 8 13:56 Videos
_|_________|__|___|____|_____|____________|___________|
# $1 $2 $3 $4 $5 $6 $7
从上面输出可以看到7个字段,含义分别如下:
(#)第一个字符:文件类型
- :普通文件
d :目录
s :套接字文件
l :链接文件
($1)字段1: 文件权限
($2)字段2: 链接数
($3)字段3: 所有者
($4)字段4: 用户组
($5)字段5: 文件大小
($6)字段6: 文件最近一次被修改的日期时间
($7)字段7: 文件名
- 【 -lh 】人性化显示文件大小
[root@test ~]# ls -lh
total 12K
-rw-------. 1 root root 8.5K May 6 2014 anaconda-ks.cfg
drwxr-xr-x. 3 root root 19 May 14 05:20 Desktop
drwxr-xr-x. 2 root root 6 May 8 13:56 Documents
drwxr-xr-x. 2 root root 6 May 8 13:56 Downloads
drwxr-xr-x. 2 root root 6 May 8 13:56 Music
drwxr-xr-x. 2 root root 6 May 8 13:56 Pictures
drwxr-xr-x. 2 root root 6 May 8 13:56 Public
drwxr-xr-x. 2 root root 6 May 8 13:56 Templates
drwxr-xr-x. 2 root root 6 May 8 13:56 Videos
- 【 -F 】 使用不同特殊字符归类不同的文件类型
[root@test ~]# ls -F /mnt/
123/ date.sh* lib@ test
[root@test ~]#
##如上所示:
/ : 表示目录
无特殊字符: 表示普通文件
@ : 表示链接文件
* : 表示可执行文件
- 【 -ld 】长列表格式列出某个目录的信息
[root@test ~]# ls -ld /mnt/123/file2
drwxr-xr-x. 2 root root 6 May 14 09:04 /mnt/123/file2
- 【 -R 】递归地列出子目录的内容
[root@test ~]# ls -R /mnt/
/mnt/:
123 date.sh lib test
/mnt/123:
file1 file2
/mnt/123/file1:
test
/mnt/123/file2:
linux
- 【 -ltr 】将长列表格式按文件或目录的修改时间 倒序地 列出文件和目录
[root@test ~]# ls -ltr /mnt/
total 4
lrwxrwxrwx. 1 root root 7 May 14 08:58 lib -> usr/lib
-rwxr-xr-x. 1 root root 5 May 14 08:59 date.sh
-rw-r--r--. 1 root root 0 May 14 08:59 test
drwxr-xr-x. 4 root root 30 May 14 09:04 123
- 【 -ls 】将长列表格式按文件大小顺序列出文件和目录
[root@test ~]# ls -ls /mnt/
total 4
0 drwxr-xr-x. 4 root root 30 May 14 09:04 123
4 -rwxr-xr-x. 1 root root 5 May 14 08:59 date.sh
0 lrwxrwxrwx. 1 root root 7 May 14 08:58 lib -> usr/lib
0 -rw-r--r--. 1 root root 0 May 14 08:59 test
- 【 -a 】显示包括隐藏文件或目录在内的所有文件和目录,包括 “.“(当前目录),“…“(父目录)
[root@test ~]# ls -a
. .bash_profile Desktop
.. .bashrc Documents
.gnuplot_history Pictures test
- 【 -A 】显示包括隐藏文件或目录在内的所有文件和目录,但不列出 “.” (目前目录) 及 “…” (父目录)
[root@test ~]# ls -A
.bash_profile Desktop
.bashrc Documents
.gnuplot_history Pictures test
- 【 -i 】显示文件或目录的 inode 编号,可能会用在系统维护操作时
[root@test ~]# ls -i /mnt/
27914670 123 11753058 date.sh
11753056 lib 11753057 test
- 【 -n 】显示 uid 和 gid ,代替显示所有者和用户组
[root@test ~]# ls -n
total 16
-rw-------. 1 0 0 8619 May 6 2014 anaconda-ks.cfg
drwxr-xr-x. 3 0 0 19 May 14 05:20 Desktop
drwxr-xr-x. 2 0 0 6 May 8 13:56 Documents
drwxr-xr-x. 2 0 0 6 May 8 13:56 Downloads
drwxr-xr-x. 2 0 0 6 May 8 13:56 Music
drwxr-xr-x. 2 0 0 6 May 8 13:56 Pictures
drwxr-xr-x. 2 0 0 6 May 8 13:56 Public
drwxr-xr-x. 2 0 0 6 May 8 13:56 Templates
-rw-r--r--. 1 0 0 7 May 14 09:16 test
drwxr-xr-x. 2 0 0 6 May 8 13:56 Videos
进阶用法
1、当某个目录小文件数量过多,导致 ls 执行卡住,何解?
解题思路:
- 了解到 ls 机制是 默认全加载 且排序的:
- 解决全加载卡死: 使用 -1 仅加载 1列 输出
- 解决文件数太多排序卡死: 使用 -f 禁止其排序
- 指定输出个数,按批次 删除操作:使用 head -n
排查过程如下:
-
- 解决 ls hang住问题
- 解决 ls hang住问题
-
- 按列加载、指定数量、不排序删除
ls -1 -f | xargs -n 10000 rm -fr
输出所有的文件名(用空格分割) xargs就是将ls的输出,每10000个为一组(以空格为分隔符),作为rm -rf的参数也就是说将所有文件名10个为一组,由rm -rf删除\