Shel l中 ls 使用参数多样化查看文件和目录

Shel l中 ls 使用参数多样化查看文件和目录

列出文件名和目录(ls)

  • 语法格式
ls   [选项]   [文件]
  • 每行显示一条记录
[root@test ~]# ls -1
anaconda-ks.cfg
Desktop
Documents
Downloads
Music
Pictures
Public
Templates
Videos
  • 长列表显示文件和目录,包括文件类型、大小、修改日期和时间、权限等信息
[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: 文件名
  • 人性化显示文件大小
[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
  • 使用不同特殊字符归类不同的文件类型
[root@test ~]# ls -F /mnt/
123/  date.sh*  lib@  test
[root@test ~]#
##如上所示:
/ :        表示目录
无特殊字符: 表示普通文件
@ :        表示链接文件
* :        表示可执行文件
  • 长列表格式列出某个目录的信息
[root@test ~]# ls -ld /mnt/123/file2
drwxr-xr-x. 2 root root 6 May 14 09:04 /mnt/123/file2
  • 递归地列出子目录的内容
[root@test ~]# ls -R /mnt/
/mnt/:
123  date.sh  lib  test

/mnt/123:
file1  file2

/mnt/123/file1:
test

/mnt/123/file2:
linux
  • 将长列表格式按文件或目录的修改时间 倒序地 列出文件和目录
[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
  • 将长列表格式按文件大小顺序列出文件和目录
[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
  • 显示包括隐藏文件或目录在内的所有文件和目录,包括 “.“(当前目录),“..“(父目录)
[root@test ~]# ls -a
.                .bash_profile  Desktop    
..               .bashrc        Documents  
.gnuplot_history  Pictures  test
  • 显示包括隐藏文件或目录在内的所有文件和目录,但不列出 “.” (目前目录) 及 “..” (父目录)
[root@test ~]# ls -A
.bash_profile  Desktop    
.bashrc        Documents  
.gnuplot_history  Pictures  test
  • 显示文件或目录的 inode 编号,可能会用在系统维护操作时
[root@test ~]# ls -i /mnt/
27914670 123            11753058 date.sh  
11753056 lib            11753057 test
  • 显示 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

猜你喜欢

转载自blog.csdn.net/sunny_future/article/details/80315525
今日推荐