对于使用Linux命令的同学来说,ls命令是最常用的命令之一。不过ls常用却不简单。
先看基本用法
[root@localhost ~]# ls
anaconda-ks.cfg Desktop Documents Downloads
可以看到ls会把当前目录下文件及目录(不包含隐藏目录)列出来并以空格隔开。
若想以逗号分隔,使用-m参数
[root@localhost ~]# ls -m
anaconda-ks.cfg, Desktop, Documents, Downloads
若想查看ls有哪些参数可用使用ls --help
若要以列表形式显示使用-l参数
[root@localhost ~]# ls -l
total 34236
-rw-------. 1 root root 1622 Mar 4 01:45 anaconda-ks.cfg
drwxr-xr-x. 2 root root 6 Mar 4 01:58 Desktop
drwxr-xr-x. 2 root root 6 Mar 4 01:58 Documents
使用ls -l pattern可以只显示指定模式的文件或目录,如ls -l *.log显示以.log结尾的文件或目录。若想显示所有除了以.log结尾的文件则使用—ignore选项,ls -l --ignore=*.log。
若要显示文件i节点使用-i选项
[root@localhost ~]# ls -li
total 34236
33574978 -rw-------. 1 root root 1622 Mar 4 01:45 anaconda-ks.cfg
51642637 drwxr-xr-x. 2 root root 6 Mar 4 01:58 Desktop
51642638 drwxr-xr-x. 2 root root 6 Mar 4 01:58 Documents
若要显示所有目录及文件(包括隐藏目录)使用-a参数
[root@localhost ~]# ls -al
total 34300
dr-xr-x---. 16 root root 4096 Jul 14 22:19 .
dr-xr-xr-x. 19 root root 249 Jun 9 08:57 ..
-rw-------. 1 root root 12479 Jul 14 14:04 .bash_history
drwxr-xr-x. 2 root root 6 Mar 4 01:58 Desktop
drwxr-xr-x. 2 root root 6 Mar 4 01:58 Documents
若要显示所有目录及文件(包括隐藏目录)但不想显示代表当前目录和父目录的.和..使用-A
[root@localhost ~]# ls -Al
total 34296
-rw-------. 1 root root 12479 Jul 14 14:04 .bash_history
drwxr-xr-x. 2 root root 6 Mar 4 01:58 Desktop
drwxr-xr-x. 2 root root 6 Mar 4 01:58 Documents
默认显示文件大小为字节,若想以可读方式如k,m显示使用-h选项
[root@localhost ~]# ls -hl
total 34M
-rw-------. 1 root root 1.6K Mar 4 01:45 anaconda-ks.cfg
-rw-r--r--. 1 root root 1.7K Mar 4 01:46 initial-setup-ks.cfg
-rwxr-xr-x. 1 root root 34M Apr 7 11:54 zookeeper-3.4.10.tar.gz
若想按时间排序,使用-t选项(日期从近及远,若想由远及近使用-r选项)
[root@localhost ~]# ls -ltr
total 34236
drwxr-xr-x. 10 weblogic weblogic 4096 Mar 23 2017 zookeeper-3.4.10
drwxr-xr-x. 2 root root 6 Mar 4 01:58 Desktop
-rwxr-xr-x. 1 root root 35042811 Apr 7 11:54 zookeeper-3.4.10.tar.gz
若想以文件大小排序,使用-s(默认从大到小,若想从小到大使用-r选项)
[root@localhost ~]# ls -lS
total 34236
-rwxr-xr-x. 1 root root 35042811 Apr 7 11:54 zookeeper-3.4.10.tar.gz
drwxr-xr-x. 10 weblogic weblogic 4096 Mar 23 2017 zookeeper-3.4.10
-rw-r--r--. 1 root root 1650 Mar 4 01:46 initial-setup-ks.cfg
文件大小与所占磁盘空间
不同的操作系统会使用不同的文件系统类型,文件系统类型决定了文件在磁盘上的存储格式,常见的文件系统有nfs、ext4、NTFS
Linux上如何查看文件系统类型呢?使用df -T。Windows下直接查看磁盘属性即可看出文件系统类型。
硬盘的最小读写单元为扇区(https://cloud.tencent.com/developer/article/1129947)。Linux上文件存储时是以Block大小进行读写的。
fdisk -l 查看扇区大小,512Byte
stat -f . 查看Block大小,一般为4096,即4k
[root@localhost mydir]# pwd
/root/mydir
[root@localhost mydir]# ls -l
total 4
-rw-r--r--. 1 root root 2 Jul 15 01:51 1.txt
有了以上知识,问题是在mydir中执行ls -l第一行total 4代表什么意思?
在讨论大小之前我们先说一个创建一个指定大小文件的命令
for i in `seq 1 1000`; do echo -n 1 >> 1.txt ; done
假如我们使用> 1.txt创建了一个空文件,使用上述命令即可向1.txt中写入1000个字节,echo中的-n参数指的是不要向文件中输出换行。
创建一个4096(4K的文件)我们看下,
[root@localhost mydir]# ll
total 4
-rw-r--r--. 1 root root 4096 Jul 15 03:08 1.txt
当文件大小为4K时,目录大小为4K。
再次向文件中写入一字节,4097已超出4K,这时可以看出目录大小为8K。
[root@localhost mydir]# ll
total 8
-rw-r--r--. 1 root root 4097 Jul 15 03:11 1.txt
文件的硬链接、软连接
ls –l输出中文件权限位后面跟着的数字为文件硬链接的个数,一般情况下这个数字为1。下面我们为1.txt创建一个硬连接。
[root@localhost mydir]# ln 1.txt 2.txt
[root@localhost mydir]# ls -li
total 16
74828 -rw-r--r--. 2 root root 4097 Jul 15 03:11 1.txt
74828 -rw-r--r--. 2 root root 4097 Jul 15 03:11 2.txt
通过上面示例可以看出创建硬链接后,硬链接个数变为2,1.txt和2.txt文件的i节点相同,即二者是同一个文件,修改其中一个另外一个会跟着修改。
除了硬链接还有软链接,软连接可以理解为windows中的快捷方式,其建立命令为ln –s src dest。