您的位置:首页 > 运维架构 > Linux

每天一个Linux之ls命令

2015-12-19 21:48 701 查看
ls命令是linux下最常用的命令。ls命令就是list的缩写缺省下ls用来打印出当前目录的清单如果ls指定其他目录那么就会显示指定目录里的文件及文件夹清单。 通过ls 命令不仅可以查看linux文件夹包含的文件而且可以查看文件权限(包括目录、文件夹、文件权限)查看目录信息等等。ls 命令在日常的linux操作中用的很多!
1. 命令格式:
ls [选项] [目录名]
2. 命令功能:
列出目标目录中所有的子目录和文件。
3. 常用参数:
-a, –all 列出目录下的所有文件,包括以 . 开头的隐含文件
-l,除了文件名之外,还将文件的权限、所有者、文件大小等信息详细列出来
-h, –human-readable,与-l一起使用,以容易理解的格式列出文件大小 (例如 1K 234M 2G)
-d, 显示目录自身的相关属性;通常要与-l一起使用
-r, –reverse 按字母逆序显示
-R, –recursive 列出所有当前目录的所有目录层及子文件

4. 范例:
例一:列出root目录下的所有目录及文件
[BEGIN] 2015/12/19 21:14:26
[root@localhost ~]# pwd
/root
[root@localhost ~]# ls -a
.  ..  anaconda-ks.cfg  .bash_history  .bash_logout  .bash_profile  .bashrc  .cshrc  install.log  install.log.syslog  sersync2.5.4_64bit_binary_stable_final.tar.gz  .tcshrc

[END] 2015/12/19 21:14:39
例二:列出root目录下的所有目录及文件的权限、所有者、文件大小等信息详细列出来
[BEGIN] 2015/12/19 21:17:39
[root@localhost ~]# pwd
/root
[root@localhost ~]# ls -l
total 772
-rw-------. 1 root root   3326 Dec  3 06:38 anaconda-ks.cfg
-rw-r--r--. 1 root root  39935 Dec  3 06:38 install.log
-rw-r--r--. 1 root root   9154 Dec  3 06:35 install.log.syslog
-rw-r--r--. 1 root root 727290 Oct 25  2011 sersync2.5.4_64bit_binary_stable_final.tar.gz

[END] 2015/12/19 21:17:56
例三:列出root目录下的所有目录及文件的权限、所有者、文件大小(例如 1K 234M 2G)等信息详细列出来
[BEGIN] 2015/12/19 21:19:45
[root@localhost ~]# pwd
/root
[root@localhost ~]# ls -hl
total 772K
-rw-------. 1 root root 3.3K Dec  3 06:38 anaconda-ks.cfg
-rw-r--r--. 1 root root  39K Dec  3 06:38 install.log
-rw-r--r--. 1 root root 9.0K Dec  3 06:35 install.log.syslog
-rw-r--r--. 1 root root 711K Oct 25  2011 sersync2.5.4_64bit_binary_stable_final.tar.gz

[END] 2015/12/19 21:20:01
例四:列出root目录权限、所有者、文件大小
[BEGIN] 2015/12/19 21:28:33
[root@localhost ~]# pwd
/root
[root@localhost ~]# ls -dl
dr-xr-x---. 18 root root 4096 Dec 17 22:08 .
[root@localhost ~]# cd ..
[root@localhost /]# pwd
/
[root@localhost /]# ls -dl|grep root
dr-xr-xr-x. 22 root root 4096 Dec 15 03:15 .

[END] 2015/12/19 21:30:59
例五:按字母逆序列出root目录下的子文件
[BEGIN] 2015/12/19 21:37:36
[root@localhost ~]# pwd
/root
[root@localhost ~]# ls
anaconda-ks.cfg        install.log         mysql-5.6.24  php-5.5.26             redis-3.0.5         rsync-3.0.7.tar.gz  rubygems-1.8.24                   svn_merge1.sh
download?source=files  install.log.syslog  nginx-1.8.0   postfix-2.2.10         redis-3.0.5.tar.gz  ruby-2.0.0-p353     sqlite-autoconf-3070900           yaml-0.1.4
index.html             jb                  node-v0.6.6   postfix-2.2.10.tar.gz  rsync-3.0.7         rubygame-2.3.0      sqlite-autoconf-3070900.tar.gz.1  yaml-0.1.4.tar.gz.1
[root@localhost ~]# ls -r
yaml-0.1.4.tar.gz.1  sqlite-autoconf-3070900.tar.gz.1  rubygame-2.3.0      rsync-3.0.7         postfix-2.2.10.tar.gz  node-v0.6.6   jb                  index.html
yaml-0.1.4           sqlite-autoconf-3070900           ruby-2.0.0-p353     redis-3.0.5.tar.gz  postfix-2.2.10         nginx-1.8.0   install.log.syslog  download?source=files
svn_merge1.sh        rubygems-1.8.24                   rsync-3.0.7.tar.gz  redis-3.0.5         php-5.5.26             mysql-5.6.24  install.log         anaconda-ks.cfg

[END] 2015/12/19 21:37:50
例六:按字母逆序列出root目录下的子文件
[root@localhost ~]# pwd
/root
[root@localhost ~]# tree
.
├── a
│?? └── b
│??     └── c
│??         └── d
│??             └── f
├── anaconda-ks.cfg
├── install.log
└── install.log.syslog
5 directories, 3 files
[root@localhost ~]#
[root@localhost ~]# tree
.
├── a
│?? └── b
│??     └── c
│??         └── d
│??             └── f
├── anaconda-ks.cfg
├── install.log
└── install.log.syslog

5 directories, 3 files
[root@localhost ~]# ls -R
.:
a  anaconda-ks.cfg  install.log  install.log.syslog

./a:
b

./a/b:
c

./a/b/c:
d

./a/b/c/d:
f

./a/b/c/d/f:
[root@localhost ~]# pwd
/root

[END] 2015/12/19 21:45:38
扩展:
1. 显示彩色目录列表
打开/etc/bashrc, 加入如下一行:
alias ls="ls --color"
下次启动bash时就可以像在Slackware里那样显示彩色的目录列表了, 其中颜色的含义如下:
1. 蓝色-->目录
2. 绿色-->可执行文件
3. 红色-->压缩文件
4. 浅蓝色-->链接文件
5. 灰色-->其他文件
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  Linux之ls命令