ls应该是我们最熟悉的指令之一,通常进入命令行,少不了就要ls一下。虽然它是一个很基本很常用的命令,不过它的功能也很丰富,熟练使用它可以更加便捷我们看我们想要看到的文件信息。本文ls介绍基于CentOS7.2环境,后面的实验均在此环境下实现。
ls命令介绍| 123456789101112131415161718192021222324252627282930313233343536 | [root@localhost ~]LS(1) User Commands LS(1) NAME ls - list directory contents SYNOPSIS ls [OPTION]... [FILE]... DESCRIPTION List information about the FILEs (the current directory by default). Sort entries alphabetically if none of -cftuvSUX nor --sort is speci‐ fied. Mandatory arguments to long options are mandatory for short options too. -a, --all do not ignore entries starting with . -A, --almost-all do not list implied . and .. -d, --directory list directories themselves, not their contents -h, --human-readable with -l, print sizes in human readable format (e.g., 1K 234M 2G) -i, --inode print the index number of each file -l use a long listing format -r, --reverse reverse order while sorting -R, --recursive list subdirectories recursively -S sort by file size -t sort by modification time, newest first -u with -lt: sort by, and show, access time; with -l: show access time and sort by name; otherwise: sort by access time |
以上的内容有所省略,本文只截取常用到的11个选项做详细说明。
-a 显示所有内容,包括以.开头的隐藏文件
| 12345 | [root@localhost ~]. .bash_logout .config .esd_auth .local 公共 文档 .. .bash_profile .cshrc .ICEauthority .mozilla 模板 下载 anaconda-ks.cfg .bashrc CST initial-setup-ks.cfg .tcshrc 视频 音乐 .bash_history .cache .dbus .lesshst .Xauthority 图片 桌面 |
-A 显示所有内容,但不包括.和..文件,显示以.开头的隐藏文件
| 12345 | [root@localhost ~]anaconda-ks.cfg .bashrc CST initial-setup-ks.cfg .tcshrc 视频 音乐 .bash_history .cache .dbus .lesshst .Xauthority 图片 桌面 .bash_logout .config .esd_auth .local 公共 文档 .bash_profile .cshrc .ICEauthority .mozilla 模板 下载 |
-d 显示目录本身但不显示目录下的内容
| 1234 | [root@localhost ~]/root[root@localhost ~]anaconda-ks.cfg CST initial-setup-ks.cfg 公共 模板 视频 图片 文档 下载 音乐 桌面 |
-h 以人易识别的方式展示文件大小必须与-l一起使用
-l 文件长格式,也就是展示文件详细信息
| 123456789101112131415 | [root@localhost ~]anaconda-ks.cfg CST initial-setup-ks.cfg 公共 模板 视频 图片 文档 下载 音乐 桌面 [root@localhost ~]总用量 8 -rw-------. 1 root root 1172 7月 20 00:38 anaconda-ks.cfg drwxr-xr-x. 2 root root 6 7月 21 18:52 CST -rw-------. 1 root root 1220 7月 19 16:46 initial-setup-ks.cfg drwxr-xr-x. 2 root root 6 7月 22 09:52 公共 drwxr-xr-x. 2 root root 6 7月 22 09:52 模板 drwxr-xr-x. 2 root root 6 7月 22 09:52 视频 drwxr-xr-x. 2 root root 6 7月 22 09:52 图片 drwxr-xr-x. 2 root root 6 7月 22 09:52 文档 drwxr-xr-x. 2 root root 6 7月 22 09:52 下载 drwxr-xr-x. 2 root root 6 7月 22 09:52 音乐 drwxr-xr-x. 2 root root 6 7月 22 09:52 桌面 |
上面显示了加-l与不加-l的却别,下面看看加-h的好处
| 12345678910111213 | [root@localhost ~]总用量 8.0K -rw-------. 1 root root 1.2K 7月 20 00:38 anaconda-ks.cfg drwxr-xr-x. 2 root root 6 7月 21 18:52 CST -rw-------. 1 root root 1.2K 7月 19 16:46 initial-setup-ks.cfg drwxr-xr-x. 2 root root 6 7月 22 09:52 公共 drwxr-xr-x. 2 root root 6 7月 22 09:52 模板 drwxr-xr-x. 2 root root 6 7月 22 09:52 视频 drwxr-xr-x. 2 root root 6 7月 22 09:52 图片 drwxr-xr-x. 2 root root 6 7月 22 09:52 文档 drwxr-xr-x. 2 root root 6 7月 22 09:52 下载 drwxr-xr-x. 2 root root 6 7月 22 09:52 音乐 drwxr-xr-x. 2 root root 6 7月 22 09:52 桌面 |
之前的字节都自动转换为kb格式,上面的更方便与我们对于文件大小有更直观的认识。
-i 显示文件inode号,关于inode我还有一些困惑,等了解足够清楚在后面会专门为inode写一篇文字。
| 12345678910111213 | [root@localhost ~]总用量 8 72664101 -rw-------. 1 root root 1172 7月 20 00:38 anaconda-ks.cfg 73207816 drwxr-xr-x. 2 root root 6 7月 21 18:52 CST 72699695 -rw-------. 1 root root 1220 7月 19 16:46 initial-setup-ks.cfg 2536844 drwxr-xr-x. 2 root root 6 7月 22 09:52 公共 101603105 drwxr-xr-x. 2 root root 6 7月 22 09:52 模板 2536850 drwxr-xr-x. 2 root root 6 7月 22 09:52 视频 101603106 drwxr-xr-x. 2 root root 6 7月 22 09:52 图片 35973547 drwxr-xr-x. 2 root root 6 7月 22 09:52 文档 73207845 drwxr-xr-x. 2 root root 6 7月 22 09:52 下载 73207846 drwxr-xr-x. 2 root root 6 7月 22 09:52 音乐 34020951 drwxr-xr-x. 2 root root 6 7月 22 09:52 桌面 |
现在??以简单的认为inode相当于大公司的员工号,大公司根据员工号精确定位某个人,而它则是准确定位文件存储的位置。
-r 逆序显示文件
| 12345678910111213 | [root@localhost ~]总用量 8 drwxr-xr-x. 2 root root 6 7月 22 09:52 桌面 drwxr-xr-x. 2 root root 6 7月 22 09:52 音乐 drwxr-xr-x. 2 root root 6 7月 22 09:52 下载 drwxr-xr-x. 2 root root 6 7月 22 09:52 文档 drwxr-xr-x. 2 root root 6 7月 22 09:52 图片 drwxr-xr-x. 2 root root 6 7月 22 09:52 视频 drwxr-xr-x. 2 root root 6 7月 22 09:52 模板 drwxr-xr-x. 2 root root 6 7月 22 09:52 公共 -rw-------. 1 root root 1220 7月 19 16:46 initial-setup-ks.cfg drwxr-xr-x. 2 root root 6 7月 21 18:52 CST -rw-------. 1 root root 1172 7月 20 00:38 anaconda-ks.cfg |
-R 递归显示目录内容
| 1234567891011121314151617181920212223242526272829303132333435363738394041 | [root@localhost ~]/root: 总用量 8 -rw-------. 1 root root 1172 7月 20 00:38 anaconda-ks.cfg drwxr-xr-x. 2 root root 6 7月 21 18:52 CST -rw-------. 1 root root 1220 7月 19 16:46 initial-setup-ks.cfg drwxr-xr-x. 2 root root 6 7月 22 09:52 公共 drwxr-xr-x. 2 root root 6 7月 22 09:52 模板 drwxr-xr-x. 2 root root 6 7月 22 09:52 视频 drwxr-xr-x. 2 root root 6 7月 22 09:52 图片 drwxr-xr-x. 2 root root 6 7月 22 09:52 文档 drwxr-xr-x. 2 root root 6 7月 22 09:52 下载 drwxr-xr-x. 2 root root 6 7月 22 09:52 音乐 drwxr-xr-x. 2 root root 6 7月 22 09:52 桌面 /root/CST: 总用量 0 /root/公共: 总用量 0 /root/模板: 总用量 0 /root/视频: 总用量 0 /root/图片: 总用量 0 /root/文档: 总用量 0 /root/下载: 总用量 0 /root/音乐: 总用量 0 /root/桌面: 总用量 0 |
加上递归后会在显示主目录下的内容后在分别显示各个子目录下的所有内容,如果子目录下还有子目录也依次展开。其目的与-d相反
-S 按文件大小排序
| 12345678910111213 | [root@localhost ~]总用量 8 -rw-------. 1 root root 1220 7月 19 16:46 initial-setup-ks.cfg -rw-------. 1 root root 1172 7月 20 00:38 anaconda-ks.cfg drwxr-xr-x. 2 root root 6 7月 21 18:52 CST drwxr-xr-x. 2 root root 6 7月 22 09:52 公共 drwxr-xr-x. 2 root root 6 7月 22 09:52 模板 drwxr-xr-x. 2 root root 6 7月 22 09:52 视频 drwxr-xr-x. 2 root root 6 7月 22 09:52 图片 drwxr-xr-x. 2 root root 6 7月 22 09:52 文档 drwxr-xr-x. 2 root root 6 7月 22 09:52 下载 drwxr-xr-x. 2 root root 6 7月 22 09:52 音乐 drwxr-xr-x. 2 root root 6 7月 22 09:52 桌面 |
-t 以mtime时间前后排序,mtime越新越靠前
| 12345678910111213 | [root@localhost ~]总用量 8 drwxr-xr-x. 2 root root 6 7月 22 09:52 公共 drwxr-xr-x. 2 root root 6 7月 22 09:52 模板 drwxr-xr-x. 2 root root 6 7月 22 09:52 视频 drwxr-xr-x. 2 root root 6 7月 22 09:52 图片 drwxr-xr-x. 2 root root 6 7月 22 09:52 文档 drwxr-xr-x. 2 root root 6 7月 22 09:52 下载 drwxr-xr-x. 2 root root 6 7月 22 09:52 音乐 drwxr-xr-x. 2 root root 6 7月 22 09:52 桌面 drwxr-xr-x. 2 root root 6 7月 21 18:52 CST -rw-------. 1 root root 1172 7月 20 00:38 anaconda-ks.cfg -rw-------. 1 root root 1220 7月 19 16:46 initial-setup-ks.cfg |
-u 必须与-t合用,表示以atime时间前后排序,atime越新越靠前
| 12345678910111213 | [root@localhost ~]总用量 8 drwxr-xr-x. 2 root root 6 7月 31 09:39 视频 drwxr-xr-x. 2 root root 6 7月 31 09:39 图片 drwxr-xr-x. 2 root root 6 7月 31 09:39 音乐 drwxr-xr-x. 2 root root 6 7月 31 09:39 下载 drwxr-xr-x. 2 root root 6 7月 31 09:39 文档 drwxr-xr-x. 2 root root 6 7月 31 09:39 公共 drwxr-xr-x. 2 root root 6 7月 31 09:39 CST drwxr-xr-x. 2 root root 6 7月 31 09:39 桌面 drwxr-xr-x. 2 root root 6 7月 31 09:39 模板 -rw-------. 1 root root 1220 7月 19 16:46 initial-setup-ks.cfg -rw-------. 1 root root 1172 7月 19 16:46 anaconda-ks.cfg |
文件名通配下面简要介绍下,文件名的通配符*匹配零个或多个字符 ?匹配任何单个字符 ~ 当前用户家目录 ~username 用户家目录~-前一个工作目录 ~+ 当前工作目录[0-9]匹配一个数字范围 [a-z]:大写和小写字母[A-Z]:大写和小写字母[]匹配列表中的任何的一个字符 [^]匹配列表中的所有字符以外的字符[:digit:]:任意数字,相当于0-9 [:lower:]:任意小写字母[:upper:]: 任意大写字母 [:alpha:]: 任意大小写字母 [:alnum:]:任意数字或字母 [:space:]:空格[:punct:]:标点符号
下面通过一些实际例子,感受下文件名通配给我们带来的一些好处。1、显示/var目录下所有以l开头,以一个小写字母结尾,且中间出现至少一位数字的文件或目录
| 12 | [root@localhost ~]ls: 无法访问/var/l*[0-9]*[[:lower:]]: 没有那个文件或目录 |
找不到,那我们自己创建一个符合条件的文件
| 123 | [root@localhost ~][root@localhost ~]-rw-r--r--. 1 root root 0 7月 31 14:17 /var/lq2e |
成功找到2、显示/etc目录下以任意一位数字开头,且以非数字结尾的文件或目录
| 12345 | [root@localhost ~]ls: 无法访问/etc/[0-9]*[^0-9]: 没有那个文件或目录 [root@localhost ~][root@localhost ~]/etc/3q |
初始的/etc/下没有符合条件的,通过创建一个符合条件的文件,成功检索并显示出来3、显示/etc/目录下以非字母开头,后面跟了一个字母及其它任意长度任意字符的文件或目录
| 12 | [root@localhost ~]/etc/3q |
成功显示4、显示/etc目录下所有以m开头以非数字结尾的文件或目录
| 12345678910111213141516 | [root@localhost ~]/etc/machine-id /etc/makedumpfile.conf.sample /etc/motd /etc/my.cnf /etc/magic /etc/man_db.conf /etc/mtab/etc/mail.rc /etc/mke2fs.conf /etc/mtools.conf /etc/maven: /etc/modprobe.d: mlx4.conf /etc/modules-load.d: /etc/multipath: /etc/my.cnf.d: mysql-clients.cnf |
5、显示/etc目录下,所有以.d结尾的文件或目录
| 123456789101112 | [root@localhost ~]/etc/bash_completion.d /etc/ipsec.d /etc/profile.d /etc/rwtab.d /etc/binfmt.d /etc/ld.so.conf.d /etc/rc0.d /etc/sane.d /etc/chkconfig.d /etc/libibverbs.d /etc/rc1.d /etc/setuptool.d /etc/cron.d /etc/logrotate.d /etc/rc2.d /etc/statetab.d /etc/depmod.d /etc/modprobe.d /etc/rc3.d /etc/sudoers.d /etc/dnsmasq.d /etc/modules-load.d /etc/rc4.d /etc/sysctl.d /etc/dracut.conf.d /etc/my.cnf.d /etc/rc5.d /etc/tmpfiles.d /etc/exports.d /etc/oddjobd.conf.d /etc/rc6.d /etc/usb_modeswitch.d /etc/gdbinit.d /etc/pam.d /etc/rc.d /etc/xinetd.d /etc/grub.d /etc/popt.d /etc/request-key.d /etc/yum.repos.d /etc/init.d /etc/prelink.conf.d /etc/rsyslog.d |
这里我们得加上-d不显示目录内的内容,否则列出来的东西会非常多。因为我们想要的只是上述结果,所以不相关的内容还是不显示的好。6、显示/etc目录下,所有.conf结尾,且以m,n,r,p开头的文件或目录
| 12345 | [root@localhost ~]/etc/man_db.conf /etc/nsswitch.conf /etc/radvd.conf /etc/rsyslog.conf /etc/mke2fs.conf /etc/numad.conf /etc/request-key.conf /etc/mtools.conf /etc/pbm2ppa.conf /etc/resolv.conf /etc/nfsmount.conf /etc/pnm2ppa.conf /etc/rsyncd.conf |
提示:我们在使用文件通配符查找自己想看到的内容是最好加上-d因为不加的话,你会看到很多非相关信息。看到这相信大家对于ls及文件名通配内容已经有了初步的理解。在大家看到这时不知有没有疑惑,在上面的6个例子里面,显示字母我用的是[:alpha:]而不是[a-z]、[A-Z]为什么呢,[a-z]、[A-Z]有什么区别呢?下面对这个问题做下说明我们先创建一个实验用的目录及4个文件
/test1目录/test1/a文件 /test1/A文件 /test1/Z文件 /test1/z文件
| 12 | [root@localhost ~]a A z Z |
我们先分别ls /test1目录下的[:alpha:]、[a-z]、[A-Z]看看效果
| 123456 | [root@localhost ~]/test1/a /test1/A /test1/z /test1/Z[root@localhost ~]/test1/a /test1/A /test1/z[root@localhost ~]/test1/A /test1/z /test1/Z |
我们发现[:alpha:]包含所有大小写字母,[a-z]则不包含Z,[A-Z]不包含a。虽然[a-z][A-Z]这两个文件通配符不区分带小写,但是其所表示的范围还是有细微差别,所以在我们不了解其差别的情况下,稳妥起见还是选择使用[:alpha:]比较好。
本文永久更新链接地址