ls命令
ls命令是linux下最常用的命令。ls命令就是list的缩写缺省下ls用来打印出当前目录的清单如果ls指定其他目录那么就会显示指定目录里的文件及文件夹清单。 通过ls 命令不仅可以查看linux文件夹包含的文件而且可以查看文件权限(包括目录、文件夹、文件权限)查看目录信息等等。
1.命令格式:
ls [选项] [目录名]
2.命令功能:
列出目标目录中所有的子目录和文件。
3.常用参数;
ls -l | 显示文件的长格式信息 |
---|---|
ls -d | 显示目录或文件自身属性,而不是它包含的内容,通常和-l 一起用ls-ld |
ls -d */ 显示当前路径下所有的目录 (不显示文件) | |
ls -d /etc/*/ 查看etc下所有目录 | |
ls -ld | 目录和符号链接信息 |
ls -A | 显示以 . 开头的隐藏文件 ,不显示 . 和 … |
ls -a | 显示以 . 开头的隐藏文件 . 显示当前目录 … 显示当前目录的父目录 |
ls -i | ndex node,inode号,索引节点号 |
ls -t | 按mtime排序 |
ls -p | 给目录名尾部加上/ |
ls -h | 做文件大小的单位转换,把单位byte改成MB/KB |
ls -r | 逆序显示文件,默认是顺序从a到z |
ls -rt | 按照修改时间逆序排列,可以看到新增加的文件 |
ls -1 | 数字1,将显示的文件按一列显示 |
ls -X | 按文件后缀排序 |
ls -S | 按从大到小排序 |
ll --full-time | 文件的时间显示完整 |
4.常用范例:
-
列出/home/peidachang文件夹下的所有文件和目录的详细资料
[root@localhost ~]# ls -l -R /home/peidachang/ /home/peidachang/: 总用量 0 -rw-r--r--. 1 root root 0 11月 22 14:32 10.txt -rw-r--r--. 1 root root 0 11月 22 14:32 1.txt -rw-r--r--. 1 root root 0 11月 22 14:32 2.txt -rw-r--r--. 1 root root 0 11月 22 14:32 3.txt -rw-r--r--. 1 root root 0 11月 22 14:32 4.txt -rw-r--r--. 1 root root 0 11月 22 14:32 5.txt -rw-r--r--. 1 root root 0 11月 22 14:32 6.txt -rw-r--r--. 1 root root 0 11月 22 14:32 7.txt -rw-r--r--. 1 root root 0 11月 22 14:32 8.txt -rw-r--r--. 1 root root 0 11月 22 14:32 9.txt 或者 [root@localhost ~]# ls -lR /home/peidachang/ /home/peidachang/: 总用量 0 -rw-r--r--. 1 root root 0 11月 22 14:32 10.txt -rw-r--r--. 1 root root 0 11月 22 14:32 1.txt -rw-r--r--. 1 root root 0 11月 22 14:32 2.txt -rw-r--r--. 1 root root 0 11月 22 14:32 3.txt -rw-r--r--. 1 root root 0 11月 22 14:32 4.txt -rw-r--r--. 1 root root 0 11月 22 14:32 5.txt -rw-r--r--. 1 root root 0 11月 22 14:32 6.txt -rw-r--r--. 1 root root 0 11月 22 14:32 7.txt -rw-r--r--. 1 root root 0 11月 22 14:32 8.txt -rw-r--r--. 1 root root 0 11月 22 14:32 9.txt 在使用 ls 命令时要注意命令的格式:在命令提示符后,首先是命令的关键字,接下来是命令参数,在命令参数之前要有一短横线“-”,所有的命令参数都有特定的作用,自己可以根据需要选用一个或者多个参数,在命令参数的后面是命令的操作对象。在以上这条命令“ ls -l -R /home/peidachang”中,“ls” 是命令关键字,“-l -R”是参数,“ /home/peidachang”是命令的操作对象当然,你也可以把他们放在一起使用这种形式和上面的命令形式执行的结果是完全一样的。另外,如果命令的操作对象位于当前目录中,可以直接对操作对象进行操作;如果不在当前目录则需要给出操作对象的完整路径,例如上面的例子中,我的当前文件夹是peidachang文件夹,我想对home文件夹下的peidachang文件进行操作,我可以直接输入 ls -lR peidachang,也可以用 ls -lR /home/peidachang。
-
列出当前目录中所有以“t”开头的目录的详细内容,可以使用如下命令:
[root@localhost ~]# ls -l t* -rw-r--r--. 1 root root 257 11月 17 10:06 test1.sh -rw-r--r--. 1 root root 0 11月 17 14:46 test1.txt -rw-r--r--. 1 root root 318 11月 17 11:00 test2.sh test: 总用量 0 -rw-r--r--. 1 root root 0 11月 19 11:34 log1.log lrwxrwxrwx. 1 root root 7 11月 19 11:38 log_link.log -> log.log -rw-r--r--. 1 root root 0 11月 19 11:34 log.log drwxr-xr-x. 2 root root 37 11月 19 11:56 test2 drwxr-xr-x. 3 root root 88 11月 17 16:13 test3 drwxr-xr-x. 4 root root 47 11月 22 11:04 test5
说明:
可以查看当前目录下文件名以“t”开头的所有文件的信息。其实,在命令格式中,方括号内的内容都是可以省略的,对于命令ls而言,如果省略命令参数和操作对象,直接输入“ ls ”,则将会列出当前工作目录的内容清单。
-
只列出文件下的子目录
[root@localhost opt]# ls -F /opt/soft |grep /$ jdk1.6.0_16/ subversion-1.6.1/ tomcat6.0.32/ #列出 /opt/soft 文件下面的子目录
[root@localhost opt]# ls -l /opt/soft | grep "^d" drwxr-xr-x 10 root root 4096 09-17 18:17 jdk1.6.0_16 drwxr-xr-x 16 1016 1016 4096 10-11 03:25 subversion-1.6.1 drwxr-xr-x 9 root root 4096 2011-11-01 tomcat6.0.32 列出 /opt/soft 文件下面的子目录详细情况
-
列出目前工作目录下所有名称是s 开头的档案,愈新的排愈后面,可以使用如下命令:
[root@localhost opt]# ls -ltr s* src: 总计 0 script: 总计 0 soft: 总计 350644 drwxr-xr-x 9 root root 4096 2011-11-01 tomcat6.0.32 -rwxr-xr-x 1 root root 81871260 09-17 18:15 jdk-6u16-linux-x64.bin drwxr-xr-x 10 root root 4096 09-17 18:17 jdk1.6.0_16 -rw-r--r-- 1 root root 205831281 09-17 18:33 apache-tomcat-6.0.32.tar.gz -rw-r--r-- 1 root root 5457684 09-21 00:23 tomcat6.0.32.tar.gz -rw-r--r-- 1 root root 4726179 10-10 11:08 subversion-deps-1.6.1.tar.gz -rw-r--r-- 1 root root 7501026 10-10 11:08 subversion-1.6.1.tar.gz drwxr-xr-x 16 1016 1016 4096 10-11 03:25 subversion-1.6.1
-
列出目前工作目录下所有档案及目录;目录于名称后加"/", 可执行档于名称后加"*"**
[root@localhost opt]# ls -AF log/ script/ soft/ src/ svndata/ web/
-
计算当前目录下的文件数和目录数
ls -l * |grep "^-"|wc -l # 查找文件个数 ls -l * |grep "^d"|wc -l #查找目录个数
-
在ls中列出文件的绝对路径
[root@localhost opt]# ls | sed "s:^:`pwd`/:" /opt/log /opt/script /opt/soft /opt/src /opt/svndata /opt/web
-
列出当前目录下的所有文件(包括隐藏文件)的绝对路径, 对目录不做递归
[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
-
指定文件时间输出格式
[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
-
扩展
显示彩色目录列表 打开/etc/bashrc, 加入如下一行: alias ls="ls --color" 下次启动bash时就可以像在Slackware里那样显示彩色的目录列表了, 其中颜色的含义如下: 1. 蓝色-->目录 2. 绿色-->可执行文件 3. 红色-->压缩文件 4. 浅蓝色-->链接文件 5. 灰色-->其他文件