每天一个Linux命令001——ls

联想记忆:

       ls - list directory

语法:

  ls [OPTION]... [FILE]...

  说明:ls是命令,OPTION是选项例如-a、-l,FILE则是参数例如文件或者目录(当然,在linux中一切皆文件)

ls是Linux系统下的最常用命令之一,而且也比较简单,下面我们就来看看最常用的用法

例1:

  在Linux系统中,以.(英文.)开头的文件被称为隐藏文件,那么使用ls命令时默认不会列出这些隐藏文件,那么怎么查看呢?

参数说明:

-a  --all    #列出所有文件,包括隐藏文件

  do not ignore entries starting with .

案例:

# ls -a
. .. .bash_history .bash_logout .bash_profile .bashrc .cshrc images .mysql_history .pki  .tcshrc .viminfo

例2:

  当使用ls命令查看一个目录时,系统输出的文件是此目录里面的内容,但是我只是想看看这个目录本身的属性,怎么办?

参数说明:

-d, --directory  #查看目录本身常与-l参数搭配

  list directory entries instead of contents, and do not dereference symbolic links

-l     use a long listing format  #以长格式形式输出

案例: 

# ls -d images/
images/

# ls -ld images/
drwxr-xr-x 2 root root 4096 Jul 4 22:16 images/

# ls -l .bashrc 
-rw-r--r--. 1 root root 176 Sep 23 2004 .bashrc

 

例3:

  如何直观地查看一个文件的大小?

-h, --human-readable  #以人类可读的形式打印输出
  with -l, print sizes in human readable format (e.g., 1K 234M 2G)

案例:

# ls -hl epel-release-latest-6.noarch.rpm 
-rw-r--r--. 1 root root 15K May 12 2016 epel-release-latest-6.noarch.rpm

 

例4:

  怎么查看/etc/目录下最新被改动的文件?

参数说明:

-t     sort by modification time  #按修改时间排序

-r, --reverse  #反向排序
  reverse order while sorting  

# ls -rt /etc
-rw-r--r--.  1 root root     46 Jun 15 19:08 adjtime
-rw-r--r--   1 root root    484 Jun 15 21:10 drbd.conf
-rw-r--r--   1 root root    251 Jun 15 22:04 mtab
-rw-r--r--   1 root root 173577 Jun 28 03:07 prelink.cache
-rw-r--r--.  2 root root     70 Jul  3 14:30 resolv.conf
-rw-r--r--   1 root root      0 Jul  3 14:30 wvdial.conf
-rw-r--r--   2 root root    641 Jul  3 14:31 hosts
drwxr-xr-x.  7 root root   4096 Jul  3 14:32 sysconfig
drwxr-xr-x   4 root root   4096 Jul  4 09:25 zabbix

 

 例5:

  如何直观地区分文件与目录?

-F, --classify   #为不同类型文件后面加上标识
append indicator (one of */=>@|) to entries

# ls -F
apache-tomcat-7.0.47/ drbd-8.4.4/ log tomcat@

 

例6:

  如何直观地查看文件最后修改的时间,例如yy-mm-dd形式?

--time-style=STYLE
  with -l, show times using style STYLE: full-iso, long-iso, iso, locale, +FORMAT. FORMAT is interpreted like
‘date’; if FORMAT is FORMAT1<newline>FORMAT2, FORMAT1 applies to non-recent files and FORMAT2 to recent files; if
STYLE is prefixed with ‘posix-’, STYLE takes effect only outside the POSIX locale

# ll --time-style=long-iso
total 4
drwxr-xr-x 2 root root 4096 2017-07-04 22:16 images

 

例7:

  如何让终端根据文件的不同输出不同的颜色?

--color[=WHEN]
  colorize the output. WHEN defaults to ‘always’ or can be ‘never’ or ‘auto’. More info below

案例:

博客园输出有点问题,但是在终端是可以清楚看到不同文件有不同的输出颜色,另外在CentOS系列操作系统中,默认就已经在别名里面写好了该颜色高亮功能

# alias |grep color
alias ls='ls --color=auto'

总结:-a打印所有内容、-l以长格式形式打印、-d仅打印目录本身、-h以人类可读格式输出、-r翻转排序、-t按修改时间排序

 

posted @ 2017-07-04 23:18  orclcast  阅读(295)  评论(0编辑  收藏  举报