Linux 中的文件和目录

瞬间移动

查看当前目录

[root@localhost ~]# pwd
/root

前往一个目录通过cd命令,例如去根目录

[root@localhost ~]# cd /
[root@localhost /]# pwd
/

去一个别的目录看看

[root@localhost /]# cd /var/log
[root@localhost log]# pwd
/var/log

迅速返回刚刚所在的目录,也就是根目录

[root@localhost log]# cd -
/

迅速返回当前用户的主目录

[root@localhost /]# cd ~
[root@localhost ~]# pwd
/root

火眼金睛

看看 /var/log 目录下都有什么内容

[root@localhost ~]# pwd
/root
[root@localhost ~]# ls /var/log
anaconda  btmp-20170706  dmesg      lastlog           messages-20170706  secure-20170706   tuned               yum.log
audit     chrony         dmesg.old  maillog           ppp                spooler           vmware-install.log
boot.log  cron           firewalld  maillog-20170706  rhsm               spooler-20170706  vmware-vmsvc.log
btmp      cron-20170706  grubby     messages          secure             tallylog          wtmp

这,内容虽多,但太简约了,稍微详细一点点

[root@localhost ~]# ls /var/log -l
总用量 948
drwxr-xr-x. 2 root   root      176 6月  30 19:48 anaconda
drwx------. 2 root   root       23 6月  30 19:49 audit
-rw-r--r--. 1 root   root     8420 7月   6 14:43 boot.log
-rw-------. 1 root   utmp        0 7月   6 13:43 btmp
-rw-------. 1 root   utmp        0 6月  30 19:46 btmp-20170706
drwxr-xr-x. 2 chrony chrony      6 11月 15 2016 chrony
-rw-------. 1 root   root     1181 7月   6 15:01 cron
-rw-------. 1 root   root     3670 7月   6 13:43 cron-20170706
-rw-r--r--. 1 root   root   120938 7月   6 14:43 dmesg
-rw-r--r--. 1 root   root   121383 7月   6 12:45 dmesg.old
-rw-r--r--. 1 root   root      113 7月   6 14:43 firewalld
-rw-------. 1 root   root     1282 7月   6 13:43 grubby
-rw-r--r--. 1 root   root   291708 7月   6 14:46 lastlog
-rw-------. 1 root   root      200 7月   6 14:44 maillog
-rw-------. 1 root   root      594 7月   6 12:45 maillog-20170706
-rw-------. 1 root   root   164427 7月   6 15:01 messages
-rw-------. 1 root   root   417907 7月   6 13:41 messages-20170706
drwx------. 2 root   root        6 6月  10 2014 ppp
drwxr-xr-x. 2 root   root        6 6月  30 19:48 rhsm
-rw-------. 1 root   root    12144 7月   6 14:46 secure
-rw-------. 1 root   root    10057 7月   6 13:41 secure-20170706
-rw-------. 1 root   root        0 7月   6 13:43 spooler
-rw-------. 1 root   root        0 6月  30 19:46 spooler-20170706
-rw-------. 1 root   root        0 6月  30 19:46 tallylog
drwxr-xr-x. 2 root   root       23 6月  30 19:50 tuned
-rw-r--r--. 1 root   root    24303 7月   6 13:47 vmware-install.log
-rw-r--r--. 1 root   root    10602 7月   6 14:44 vmware-vmsvc.log
-rw-rw-r--. 1 root   utmp    10752 7月   6 14:46 wtmp
-rw-------. 1 root   root     2838 7月   6 14:42 yum.log

会不会还有隐藏文件?

[root@localhost ~]# ls /var/log -la
总用量 952
drwxr-xr-x.  8 root   root     4096 7月   6 14:43 .
drwxr-xr-x. 19 root   root      267 7月   6 14:43 ..
drwxr-xr-x.  2 root   root      176 6月  30 19:48 anaconda
drwx------.  2 root   root       23 6月  30 19:49 audit
-rw-r--r--.  1 root   root     8420 7月   6 14:43 boot.log
-rw-------.  1 root   utmp        0 7月   6 13:43 btmp
-rw-------.  1 root   utmp        0 6月  30 19:46 btmp-20170706
drwxr-xr-x.  2 chrony chrony      6 11月 15 2016 chrony
-rw-------.  1 root   root     1181 7月   6 15:01 cron
-rw-------.  1 root   root     3670 7月   6 13:43 cron-20170706
-rw-r--r--.  1 root   root   120938 7月   6 14:43 dmesg
-rw-r--r--.  1 root   root   121383 7月   6 12:45 dmesg.old
-rw-r--r--.  1 root   root      113 7月   6 14:43 firewalld
-rw-------.  1 root   root     1282 7月   6 13:43 grubby
-rw-r--r--.  1 root   root   291708 7月   6 14:46 lastlog
-rw-------.  1 root   root      200 7月   6 14:44 maillog
-rw-------.  1 root   root      594 7月   6 12:45 maillog-20170706
-rw-------.  1 root   root   164427 7月   6 15:01 messages
-rw-------.  1 root   root   417907 7月   6 13:41 messages-20170706
drwx------.  2 root   root        6 6月  10 2014 ppp
drwxr-xr-x.  2 root   root        6 6月  30 19:48 rhsm
-rw-------.  1 root   root    12144 7月   6 14:46 secure
-rw-------.  1 root   root    10057 7月   6 13:41 secure-20170706
-rw-------.  1 root   root        0 7月   6 13:43 spooler
-rw-------.  1 root   root        0 6月  30 19:46 spooler-20170706
-rw-------.  1 root   root        0 6月  30 19:46 tallylog
drwxr-xr-x.  2 root   root       23 6月  30 19:50 tuned
-rw-r--r--.  1 root   root    24303 7月   6 13:47 vmware-install.log
-rw-r--r--.  1 root   root    10602 7月   6 14:44 vmware-vmsvc.log
-rw-rw-r--.  1 root   utmp    10752 7月   6 14:46 wtmp
-rw-------.  1 root   root     2838 7月   6 14:42 yum.log

6月、7月什么的有点乱,排个序

[root@localhost ~]# ls /var/log -lat
总用量 952
-rw-------.  1 root   root     1181 7月   6 15:01 cron
-rw-------.  1 root   root   164427 7月   6 15:01 messages
-rw-r--r--.  1 root   root   291708 7月   6 14:46 lastlog
-rw-rw-r--.  1 root   utmp    10752 7月   6 14:46 wtmp
-rw-------.  1 root   root    12144 7月   6 14:46 secure
-rw-r--r--.  1 root   root    10602 7月   6 14:44 vmware-vmsvc.log
-rw-------.  1 root   root      200 7月   6 14:44 maillog
-rw-r--r--.  1 root   root     8420 7月   6 14:43 boot.log
-rw-r--r--.  1 root   root   120938 7月   6 14:43 dmesg
drwxr-xr-x.  8 root   root     4096 7月   6 14:43 .
drwxr-xr-x. 19 root   root      267 7月   6 14:43 ..
-rw-r--r--.  1 root   root      113 7月   6 14:43 firewalld
-rw-------.  1 root   root     2838 7月   6 14:42 yum.log
-rw-r--r--.  1 root   root    24303 7月   6 13:47 vmware-install.log
-rw-------.  1 root   root     1282 7月   6 13:43 grubby
-rw-------.  1 root   utmp        0 7月   6 13:43 btmp
-rw-------.  1 root   root        0 7月   6 13:43 spooler
-rw-------.  1 root   root     3670 7月   6 13:43 cron-20170706
-rw-------.  1 root   root    10057 7月   6 13:41 secure-20170706
-rw-------.  1 root   root   417907 7月   6 13:41 messages-20170706
-rw-------.  1 root   root      594 7月   6 12:45 maillog-20170706
-rw-r--r--.  1 root   root   121383 7月   6 12:45 dmesg.old
drwxr-xr-x.  2 root   root       23 6月  30 19:50 tuned
drwx------.  2 root   root       23 6月  30 19:49 audit
drwxr-xr-x.  2 root   root        6 6月  30 19:48 rhsm
drwxr-xr-x.  2 root   root      176 6月  30 19:48 anaconda
-rw-------.  1 root   root        0 6月  30 19:46 spooler-20170706
-rw-------.  1 root   utmp        0 6月  30 19:46 btmp-20170706
-rw-------.  1 root   root        0 6月  30 19:46 tallylog
drwxr-xr-x.  2 chrony chrony      6 11月 15 2016 chrony
drwx------.  2 root   root        6 6月  10 2014 ppp

总用量952 是什么鬼?还有那一大堆的数字?

[root@localhost ~]# ls /var/log -lath
总用量 952K
-rw-------.  1 root   root   1.2K 7月   6 15:01 cron
-rw-------.  1 root   root   161K 7月   6 15:01 messages
-rw-r--r--.  1 root   root   285K 7月   6 14:46 lastlog
-rw-rw-r--.  1 root   utmp    11K 7月   6 14:46 wtmp
-rw-------.  1 root   root    12K 7月   6 14:46 secure
-rw-r--r--.  1 root   root    11K 7月   6 14:44 vmware-vmsvc.log
-rw-------.  1 root   root    200 7月   6 14:44 maillog
-rw-r--r--.  1 root   root   8.3K 7月   6 14:43 boot.log
-rw-r--r--.  1 root   root   119K 7月   6 14:43 dmesg
drwxr-xr-x.  8 root   root   4.0K 7月   6 14:43 .
drwxr-xr-x. 19 root   root    267 7月   6 14:43 ..
-rw-r--r--.  1 root   root    113 7月   6 14:43 firewalld
-rw-------.  1 root   root   2.8K 7月   6 14:42 yum.log
-rw-r--r--.  1 root   root    24K 7月   6 13:47 vmware-install.log
-rw-------.  1 root   root   1.3K 7月   6 13:43 grubby
-rw-------.  1 root   utmp      0 7月   6 13:43 btmp
-rw-------.  1 root   root      0 7月   6 13:43 spooler
-rw-------.  1 root   root   3.6K 7月   6 13:43 cron-20170706
-rw-------.  1 root   root   9.9K 7月   6 13:41 secure-20170706
-rw-------.  1 root   root   409K 7月   6 13:41 messages-20170706
-rw-------.  1 root   root    594 7月   6 12:45 maillog-20170706
-rw-r--r--.  1 root   root   119K 7月   6 12:45 dmesg.old
drwxr-xr-x.  2 root   root     23 6月  30 19:50 tuned
drwx------.  2 root   root     23 6月  30 19:49 audit
drwxr-xr-x.  2 root   root      6 6月  30 19:48 rhsm
drwxr-xr-x.  2 root   root    176 6月  30 19:48 anaconda
-rw-------.  1 root   root      0 6月  30 19:46 spooler-20170706
-rw-------.  1 root   utmp      0 6月  30 19:46 btmp-20170706
-rw-------.  1 root   root      0 6月  30 19:46 tallylog
drwxr-xr-x.  2 chrony chrony    6 11月 15 2016 chrony
drwx------.  2 root   root      6 6月  10 2014 ppp

闻香识人

当使用 ls -l 命令列出文件和文件夹的详细信息后,可看到很多按行按列来区分的信息。

例如:drwxr-xr-x. 2 root root 176 6月 30 19:48 anaconda

格式:类型权限 链接数量 文件所有者 文件所有者的用户组 文件大小 最后更新时间 文件名

类型权限,是一段类似 drwxr-xr-x. 这种的火星文,用于描述该文件或目录的类型和用户权限。

这一行中 drwxr-xr-x. 代表的是文件类型和用户权限,除去 . 小数点外,一共 10 个字符。

如果用 0-9 为索引来表述这 10 个字符的含义的话,可先将其分成 4 组,分别是

0:文件类型(d 为目录、-为文件、l为链接)
123:文件所有者的权限
456:与文件所有者同一个组的人的权限
789:其他用户的权限

后三者的权限字符永远会是 rwx ,表示是否可读、是否可写、是否可执行,如果其中有不可的地方,用 - 减号代替。

对照上例,也就是:

d:我是一个目录
rwx:所有者能读、能写、能执行
r-x:所有者同组的人能读、不能写、能执行
r-x:其他用户能读、不能写、能执行

链接数量,指的是有多少个硬链接指向该文件。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容