linux命令总结-ls

linux命令总结-ls

编程入门访客2021-10-12 9:34:006943A+A-

  

  文章来源:http://johnnyr.blog.51cto.com/7130220/1607009

  相关命令:dir

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

  ——————————————————————————–

  1. 命令格式:

  ls [选项] [目录名]

  2. 命令功能:

  列出目标目录中所有的子目录和文件。

  3. 常用参数:

  长选项必须使用的参数对于短选项时也是必需使用的。

  -a, -all                      列出目录下的所有文件,包括以 . 开头的隐含文件

  -A,                          同-a,但不列出“.”(表示当前目录)和“..”

  (表示当前目录的父目录)。

  -c,                          配合 -lt:根据 ctime 排序及显示ctime

  (文件状态最后更改的时间)

  配合 -l:显示ctime但根据名称排序否则:根据 ctime 排序

  -C,                          每栏由上至下列出项目

  –color[=WHEN]             控制是否使用色彩分辨文件。

  WHEN可以是'never'、'always'或'auto'其中之一

  -d, –directory               将目录象文件一样显示,而不是显示其下的文件。

  -D, –dired                   产生适合 Emacs 的 dired 模式使用的结果

  -f,                          对输出的文件不进行排序,-aU 选项生效,-lst 选项失效

  -g,                          类似 -l,但不列出所有者

  -G, –no-group                不列出任何有关组的信息

  -h, –human-readable          以容易理解的格式列出文件大小 (例如 1K 234M 2G)

  -si,                         类似-h,但文件大小取 1000 的次方而不是 1024

  -H,

  –dereference-command-line 使用命令列中的符号链接指示的真正目的地

  –indicator-style=方式     指定在每个项目名称后加上指示符号<方式>:

  none (默认),classify (-F),file-type (-p)

  -i, –inode                   印出每个文件的 inode 号

  -I, –ignore=样式             不印出任何符合 shell 万用字符<样式>的项目

  -k,                          即 -block-size=1K,以 k 字节的形式表示文件的大小

  -l,                          除了文件名之外,还将文件的权限、所有者、文件大小

  等信息详细列出来

  -L, –dereference             当显示符号链接的文件信息时,

  显示符号链接所指示的对象而并非符号链接本身的信息

  -m,                          所有项目以逗号分隔,并填满整行行宽

  -o,                          类似 -l,显示文件的除组信息外的详细信息。

  -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

  -t,                          以文件修改时间排序

  -u,                          配合 -lt:显示访问时间而且依访问时间排序

  配合 -l:显示访问时间但根据名称排序

  否则:根据访问时间排序

  -U,                          不进行排序;依文件系统原有的次序列出项目

  -v,                          根据版本进行排序

  -w, -width=COLS               自行指定屏幕宽度而不使用目前的数值

  -x                            逐行列出项目而不是逐栏列出

  -X                            根据扩展名排序

  -1                            每行只列出一个文件

  –help                    显示此帮助信息并离开

  –version                 显示版本信息并离开

  注释:

  SIZE 可以是一个可选的整数,后面跟着以下单位中的一个:

  KB 1000,K 1024,MB 1000*1000,M 1024*1024,还有 G、T、P、E、Z、Y。

  使用色彩来区分文件类型的功能已被禁用,默认设置和 –color=never 同时禁用了它。

  使用 –color=auto 选项,ls 只在标准输出被连至终端时才生成颜色代码。

  LS_COLORS 环境变量可改变此设置,可使用 dircolors 命令来设置。

  退出状态:

  0  正常

  1  一般问题 (例如:无法访问子文件夹)

  2  严重问题 (例如:无法使用命令行参数)

  4. 常用范例:

  例一、列出/home/lucifer文件夹下的所有文件和目录的详细资料?

  命令:ls -l -R /home/lucifer 或 ls -lR /home/lucifer

  [root@lucifer ~]# ls -lR /home/lucifer

  /home/lucifer:

  total 4

  -rw-r–r–. 1 root root    0 Jan 22 16:20 1.txt

  drwxr-xr-x. 2 root root 4096 Jan 22 16:20 test

  /home/lucifer/test:

  total 0

  注:

  -l  使用长格式列出信息

  -R  递归显示子目录

  ls -al

  –a参数与l参数结合使用,以长列表形式显示所有目录及文件详细信息。包括文件类型、文件权限、硬连接数、文件拥有者、文件所属组、文件大小、更动时间、文件名等。其中

  第1列:drwxr-xr-x代表文件的类型及权限

  以d开头的为文件夹,以-开头的为文件

  以l开头的为连接文件

  第2列:文件的链接数

  第3、4列:拥有文件的用户及组

  第5列:文件的大小

  第6列:文件最后的修改时间

  第7列:文件名

  [root@lucifer var]# ls -al

  total 80

  drwxr-xr-x. 20 root root 4096 Jan 22 17:01 .

  dr-xr-xr-x. 22 root root 4096 Jan 22 17:14 ..

  -rw-r–r–.  1 root root    0 Jan 22 17:01 1

  -rw-r–r–.  1 root root    0 Jan 22 17:01 2

  -rw-r–r–.  1 root root    0 Jan 22 17:01 3

  -rw-r–r–.  1 root root    0 Jan 22 17:01 4

  -rw-r–r–.  1 root root    0 Jan 22 17:01 5

  drwxr-xr-x.  2 root root 4096 Jan 22 15:04 account

  drwxr-xr-x.  8 root root 4096 Jan 22 15:02 cache

  drwxr-xr-x.  2 root root 4096 Feb 22  2013 crash

  drwxr-xr-x.  2 root root 4096 Sep 21  2012 cvs

  drwxr-xr-x.  3 root root 4096 Jan 22 15:04 db

  例二、列出当前目录中所有以“t”开头的文件的详细内容?

  命令:ls -l t*

  [root@lucifer lucifer]# ls -l t*

  -rw-r–r–. 1 root root    0 Jan 22 16:31 t4

  -rw-r–r–. 1 root root    0 Jan 22 16:31 t5

  test:

  total 0

  可以查看当前目录下文件名以“t”开头的所有文件的信息。其实,在命令格式中,方括号内的内容都是可以省略的,对于命令ls而言,如果省略命令参数和操作对象,直接输入“ ls ”,则将会列出当前工作目录的内容清单。

  例三:只列出目录下的子目录

  命令:ls -F /var |grep /$

  [root@lucifer var]# ls

  1  3  5        cache  cvs  empty  lib    lock  mail  opt       run    tmp

  2  4  account  crash  db   games  local  log   nis   preserve  spool  yp

  [root@lucifer var]# ls -F |grep /$

  account/

  cache/

  crash/

  cvs/

  db/

  empty/

  games/

  lib/

  local/

  lock/

  log/

  nis/

  opt/

  preserve/

  run/

  spool/

  tmp/

  yp/

  命令:ls -l /var | grep "^d"

  列出 /var 文件下面的子目录详细情况

  [root@lucifer var]# ls -l /var |grep "^d"

  drwxr-xr-x.  2 root root 4096 Jan 22 15:04 account

  drwxr-xr-x.  8 root root 4096 Jan 22 15:02 cache

  drwxr-xr-x.  2 root root 4096 Feb 22  2013 crash

  drwxr-xr-x.  2 root root 4096 Sep 21  2012 cvs

  drwxr-xr-x.  3 root root 4096 Jan 22 15:04 db

  drwxr-xr-x.  3 root root 4096 Jan 22 15:04 empty

  例四:列出目前工作目录下所有名称是s开头的档案,以时间排序。

  命令:ls -ltr s*

  [root@lucifer lucifer]# ls -ltr t*

  -rw-r–r–. 1 root root    0 Jan 22 16:31 t5

  -rw-r–r–. 1 root root    0 Jan 22 16:31 t4

  -rw-r–r–. 1 root root    0 Jan 22 17:10 t3

  test:

  total 0

  -rw-r–r–. 1 root root 0 Jan 22 16:32 t9

  -rw-r–r–. 1 root root 0 Jan 22 16:32 t10

  -rw-r–r–. 1 root root 0 Jan 22 17:10 t11

  例五:列出目前工作目录下所有档案及目录;目录于名称后加"/", 可执行档于名称后加"*"

  命令:ls -AF

  [root@lucifer lucifer]# ls -AF

  1.txt  .bash_logout  .bash_profile  .bashrc  t3  t4  t5  test/  test.sh*

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

  命令:

  ls -l * |grep "^-"|wc -l    —文件个数

  ls -l * |grep "^d"|wc -l    —目录个数

  [root@lucifer etc]# ls -l * |grep "^-"|wc -l

  406

  [root@lucifer etc]# ls -l * |grep "^d"|wc -l

  72

  例七: 在ls中列出文件的绝对路径

  命令:ls | sed "s:^:`pwd`/:"

  输出:

  [root@localhost opt]# ls | sed "s:^:`pwd`/:"

  /opt/log

  /opt/script

  /opt/soft

  /opt/src

  /opt/svndata

  /opt/web

  例九:列出当前目录下的所有文件(包括隐藏文件)的绝对路径, 对目录不做递归

  命令:find $PWD -maxdepth 1 | xargs ls -ld

  输出:

  [root@localhost opt]# find $PWD -maxdepth 1 | xargs ls -ld

  drwxr-xr-x 8 root root 4096 10-11 03:43 /opt

  drwxr-xr-x 2 root root 4096 2012-03-08 /opt/log

  drwxr-xr-x 2 root root 4096 2012-03-08 /opt/script

  drwxr-xr-x 5 root root 4096 10-11 03:21 /opt/soft

  drwxr-xr-x 2 root root 4096 2012-03-08 /opt/src

  drwxr-xr-x 4 root root 4096 10-11 05:22 /opt/svndata

  drwxr-xr-x 4 root root 4096 10-09 00:45 /opt/web

  例十:递归列出当前目录下的所有文件(包括隐藏文件)的绝对路径

  命令: find $PWD | xargs ls -ld

  例十一:指定文件时间输出格式

  命令:

  ls -tl –time-style=full-iso

  输出:

  [root@localhost soft]# ls -tl –time-style=full-iso

  总计 350644

  drwxr-xr-x 16 1016 1016 4096 2012-10-11 03:25:58.000000000 +0800 subversion-1.6.1

  ls -ctl –time-style=long-iso

  输出:

  [root@localhost soft]# ls -ctl –time-style=long-iso

  总计 350644

  drwxr-xr-x 16 1016 1016      4096 2012-10-11 03:25 subversion-1.6.1

  扩展:

  1. 显示彩色目录列表

  打开/etc/bashrc, 加入如下一行:

  alias ls="ls –color"

  下次启动bash时就可以像在Slackware里那样显示彩色的目录列表了, 其中颜色的含义如下:

  蓝色–>目录

  绿色–>可执行文件

  红色–>压缩文件

  浅蓝色–>链接文件

  灰色–>其他文件

 

点击这里复制本文地址 以上内容由黑资讯整理呈现,请务必在转载分享时注明本文地址!如对内容有疑问,请联系我们,谢谢!
  • 3条评论
  • 鸽吻谨兮2022-05-28 06:13:31
  • wxr-xr-x.  2 root root 4096 Jan 22 15:04 account  drwxr-xr-x.  8 root root 4096 Jan 22 15:02 cache  drwxr-xr-x.  2 ro
  • 末屿唔猫2022-05-28 01:40:53
  • 31 t5  -rw-r–r–. 1 root root    0 Jan 22 16:31 t4  -rw-r–r–. 1 root root    0 Jan 22 17:10 t3  test:  total 0  -rw-r–r–. 1 root roo
  • 孤央橘亓2022-05-28 01:21:11
  • 的除组信息外的详细信息。  -r, –reverse                 依相反次序排列  -R, –recursive               同时列出所有子目录层  -s, –size                    以块大小为单位列出所有

支持Ctrl+Enter提交

黑资讯 © All Rights Reserved.  
Copyright Copyright 2015-2020 黑资讯
滇ICP备19002590号-1
Powered by 黑客资讯 Themes by 如有不合适之处联系我们
网站地图| 发展历程| 留言建议| 网站管理