Linux基础命令 ls

Posted 刘合栋

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Linux基础命令 ls相关的知识,希望对你有一定的参考价值。

1. ls 列出目录的内容



常用选项如下:


-a --all

显示目录下所有的文件或子目录,包括隐藏文件、当前目录"."和上级目录".."

[[email protected] ~]# ls -a
.  ..  .bash_history  .bash_logout  .bash_profile  .bashrc  .cshrc  .pki  .tcshrc  .viminfo


-A --almost-all

显示目录下所有的文件或子目录,包括隐藏文件,但不包括"."和".."

[[email protected] ~]# ls -A
.bash_history  .bash_logout  .bash_profile  .bashrc  .cshrc  .pki  .tcshrc  .viminfo


-b --escape

打印 C 风格的转义非图形字符

[[email protected] tmp]# ls
  test_dir  test_txt
[[email protected] tmp]# ls -b
\  test_dir  test_txt


--block-size=SIZE

配合"-l"选项使用,输出的文件或目录的大小由SIZE决定(值有,K, M, G, T, P...)

[[email protected] tmp]# ls -l --block-size=M
total 0M
drwxr-xr-x 2 root root 1M Apr 19 21:46 drwxr-xr-x 2 root root 1M Apr 19 21:44 test_dir
-rw-r--r-- 1 root root 0M Apr 19 21:44 test_txt
[[email protected] tmp]# ls -l --block-size=P
total 0P
drwxr-xr-x 2 root root 1P Apr 19 21:46 drwxr-xr-x 2 root root 1P Apr 19 21:44 test_dir
-rw-r--r-- 1 root root 0P Apr 19 21:44 test_txt


--color

是否显示颜色,有三个选项:never‘, ‘auto‘ 或 ‘always‘

[[email protected] tmp]# ls --color=auto
  test_dir  test_txt


--d --directory

显示目录本身,而不会显示目录下的内容

[[email protected] tmp]# ls -d
.
[[email protected] tmp]# ls -ld
drwxrwxrwt. 9 root root 152 Apr 19 21:46 .


-F

向条目追加指示器(其中之一* / = > @

[[email protected] tmp]# ls -F
/  test_dir/  [email protected]  test_txt  test_txt_link


--file-type

于-F相同,但不显示*

[[email protected] tmp]# ls --file-type
/  test_dir/  [email protected]  test_txt  test_txt_link


--format=WORD

显示格式-x-横向排序(默认),-m横向排序,逗号分隔,-1一行显示一个数据

[[email protected] tmp]# ls -x
  test_dir  test_dir_link  test_txt  test_txt_link
[[email protected] tmp]# ls -m
, test_dir, test_dir_link, test_txt, test_txt_link
[[email protected] tmp]# ls -1
test_dir
test_dir_link
test_txt
test_txt_link


--full-time

长格式显示,显示详细时间,精确到后9位

[[email protected] tmp]# ls --full-time
total 8
drwxr-xr-x 2 root root 6 2019-04-19 21:46:15.623050216 +0800 drwxr-xr-x 2 root root 6 2019-04-19 21:44:19.657044687 +0800 test_dir
lrwxrwxrwx 1 root root 9 2019-04-19 22:21:43.109151663 +0800 test_dir_link -> test_dir/
-rw-r--r-- 2 root root 2 2019-04-19 22:22:46.067154665 +0800 test_txt
-rw-r--r-- 2 root root 2 2019-04-19 22:22:46.067154665 +0800 test_txt_link


-g

-l相似,但不输出属主

[[email protected] tmp]# ls -g
total 8
drwxr-xr-x 2 root 6 Apr 19 21:46 drwxr-xr-x 2 root 6 Apr 19 21:44 test_dir
lrwxrwxrwx 1 root 9 Apr 19 22:21 test_dir_link -> test_dir/
-rw-r--r-- 2 root 2 Apr 19 22:22 test_txt
-rw-r--r-- 2 root 2 Apr 19 22:22 test_txt_link


-G --no-group

-l相似,但不输出数组

[[email protected] tmp]# ls -lG
total 8
drwxr-xr-x 2 root 6 Apr 19 21:46 drwxr-xr-x 2 root 6 Apr 19 21:44 test_dir
lrwxrwxrwx 1 root 9 Apr 19 22:21 test_dir_link -> test_dir/
-rw-r--r-- 2 root 2 Apr 19 22:22 test_txt
-rw-r--r-- 2 root 2 Apr 19 22:22 test_txt_link


-h --human-readable

-l一起使用,打印人性化格式(1K, 1M, 1G)

[[email protected] tmp]# ls -lh /etc/rc.d/init.d/functions
-rw-r--r-- 1 root root 18K Aug 24  2018 /etc/rc.d/init.d/functions


--si

使用1000位单位,而不是1024

[[email protected] tmp]# ls -l /etc/wgetrc
-rw-r--r--. 1 root root 4479 Oct 31 00:54 /etc/wgetrc
[[email protected] tmp]# ls -l --si /etc/wgetrc
-rw-r--r--. 1 root root 4.5k Oct 31 00:54 /etc/wgetrc


--hide=

不要列出匹配shell模式的隐含条目(被 -a 或 -A 覆盖)

[[email protected] tmp]# ls
  test_dir  test_dir_link  test_txt  test_txt_link
[[email protected] tmp]# ls --hide=test*


-i --inode

打印每个文件的索引号

[[email protected] tmp]# ls -i
50812413          16777295 test_dir_link  16777289 test_txt_link
33689716 test_dir  16777289 test_txt


-Q --quote-name

用双引号将名称括起来

[[email protected] tmp]# ls -Q
"\"  "test_dir"  "test_dir_link"  "test_txt"  "test_txt_link"


-r --reverse

倒序

[[email protected] tmp]# 
[[email protected] tmp]# ls
  test_dir  test_dir_link  test_txt  test_txt_link
[[email protected] tmp]# ls -r
test_txt_link  test_txt  test_dir_link  test_dir  


-R --recursive

递归地列出子目录

[[email protected] tmp]# ls -R
.:
  test_dir  test_dir_link  test_txt  test_txt_link

./:

./test_dir:


-s --size

打印每个文件的分配大小,以块为单位

[[email protected] tmp]# ls -ls
total 2048
   0 drwxr-xr-x 2 root root       6 Apr 19 21:46    0 drwxr-xr-x 2 root root       6 Apr 19 21:44 test_dir
   0 lrwxrwxrwx 1 root root       9 Apr 19 22:21 test_dir_link -> test_dir/
1024 -rw-r--r-- 2 root root 1048576 Apr 19 23:28 test_txt
1024 -rw-r--r-- 2 root root 1048576 Apr 19 23:28 test_txt_link


-S

按文件大小进行排序

[[email protected] tmp]# ls -lhaS
total 16K
-rw-r--r--   2 root root 512 Apr 19 23:30 test_txt
-rw-r--r--   1 root root 512 Apr 19 23:30 test_txt_a
-rw-r--r--   2 root root 512 Apr 19 23:30 test_txt_link
-rw-------.  1 root root 247 Apr 17 06:35 .ifstat.u0
dr-xr-xr-x. 17 root root 244 Apr 17 09:29 ..
drwxrwxrwt.  9 root root 212 Apr 19 23:30 .
drwxr-xr-x   2 root root  21 Apr 19 23:30 test_dir
lrwxrwxrwx   1 root root   9 Apr 19 22:21 test_dir_link -> test_dir/
drwxr-xr-x   2 root root   6 Apr 19 21:46 drwxrwxrwt.  2 root root   6 Apr 16 22:49 .font-unix
drwxrwxrwt.  2 root root   6 Apr 16 22:49 .ICE-unix
drwxrwxrwt.  2 root root   6 Apr 16 22:49 .Test-unix
drwxrwxrwt.  2 root root   6 Apr 16 22:49 .X11-unix
drwxrwxrwt.  2 root root   6 Apr 16 22:49 .XIM-unix


-U

不进行排序,按目录顺序列出条目


-t

按修改时间排序,即最近更改的时间进行排序

[[email protected] tmp]# ls -lht
total 12K
-rw-r--r-- 2 root root 519 Apr 19 23:46 test_txt
-rw-r--r-- 2 root root 519 Apr 19 23:46 test_txt_link
-rw-r--r-- 1 root root 512 Apr 19 23:30 test_txt_a
drwxr-xr-x 2 root root  21 Apr 19 23:30 test_dir
lrwxrwxrwx 1 root root   9 Apr 19 22:21 test_dir_link -> test_dir/
drwxr-xr-x 2 root root   6 Apr 19 21:46 


-X

按条目扩展按字母顺序排序


-u

显示最后一次访问的时间,而不是最近修改时间


--time-style=

显示时间的格式,可选参数有:full-iso,long-iso,iso,locale,或者+FORMAT

[[email protected] tmp]# ls -l test_txt --time-style=full-iso
-rw-r--r-- 2 root root 519 2019-04-19 23:46:52.748395309 +0800 test_txt
[[email protected] tmp]# ls -l test_txt --time-style=long-iso
-rw-r--r-- 2 root root 519 2019-04-19 23:46 test_txt
[[email protected] tmp]# ls -l test_txt --time-style=iso
-rw-r--r-- 2 root root 519 04-19 23:46 test_txt
[[email protected] tmp]# ls -l test_txt --time-style=locale
-rw-r--r-- 2 root root 519 Apr 19 23:46 test_txt
[[email protected] tmp]# ls -l test_txt --time-style=+"%F %T"
-rw-r--r-- 2 root root 519 2019-04-19 23:46:52 test_txt































以上是关于Linux基础命令 ls的主要内容,如果未能解决你的问题,请参考以下文章

Linux基础命令

Linux基础命令之ls

linux基础命令

linux基础命令:ls

Linux核心基础命令

基本linux命令