第3章 文件类型和目录结构
第3章 文件类型和目录结构
在这这章之前我们先学习一个前面使用过的命令 ls
查看文件ls
命令格式:
[root@bogon redhat]# ls --helpUsage: ls [OPTION]... [FILE]...List information about the FILEs (the current directory by default).Sort entries alphabetically if none of -cftuvSUX nor --sort is specified.
ls 命令是 List 的简写,它的作用是显示指定路径下的所有的文件信息。它常用的选项有如下:
-a 用于显示所有文件包括隐藏文件及目录-d 显示目录本身而不是目录下的内容-l 以长列表的方式来显示内容-h 以人的读取习惯显示内容-r 递归显示
使用命令:
1)显示根下所有内容
[root@bogon redhat]# ls // 它是 linux 系统的根目录,所有的文件或目录都是挂载到它之下的afsboot 它是系统启动时所需要的文件etc 它是系统中所有的配置文件所在目录,它是非常重要的一个目录,这个目录大家一定要记住,后续学到高级课程时就需要修改它里面的文件lib 库文件,在系统运行过程中所需要的一个库文件media 媒体,它是用户挂载的,一般我们光盘就可以挂载到它下面opt 它是第三方软件安装目录root 它是 root 用户有家目录sbin 超级用户可以执行的命令所在目录sys 这是用户存储虚拟的文件路径usr unix software resource 它是软件资源所在目录bin 普通用户可以执行的命令所在目录dev 设备文件所在目录home 它是普通用户所在的家目录lib64 它也是库文件所在目录mnt 它是挂载目录proc 进程所在目录run 程序运行时相关的文件所在路径srv 网络服务数据tmp 运行程序时产生的临时文件,在这个目录中不要存储重要信息,因为它可能会被系统删除var 用于存放一些变化的数据,例如日志
-a 选项
[root@bogon redhat]# ls -a / . afs bin dev home lib64 mnt proc run srv tmp var .. .bash_history boot etc lib media opt root sbin sys usr
当指定了 -a 选项时,发现多了一些内容:
. 它表示当前目录 .. 它表示上一级目录 .bash_history 表示它是一个隐藏文件或目录
注意:
在 linux 中以点开头的文件或目录是隐藏文件或目录
任何一个目录,当执行了 ls 命令后,都可以看到 . 和 ..
-d 选择
只显示目录本身,而不会显示目录内容
[root@bogon redhat]# ls /usr bin games include lib lib64 libexec local sbin share src tmp [root@bogon redhat]# ls -d /usr /usr
-l 选项
它是以长列表的方式来显示内容
[root@bogon redhat]# ls /usr bin games include lib lib64 libexec local sbin share src tmp # 下面是带了 -l 选项的 [root@bogon redhat]# ls -l /usr total 240 dr-xr-xr-x. 2 root root 45056 Oct 15 19:40 bin drwxr-xr-x. 2 root root 6 Aug 10 2021 games drwxr-xr-x. 3 root root 23 Oct 15 19:38 include dr-xr-xr-x. 38 root root 4096 Oct 15 19:42 lib dr-xr-xr-x. 123 root root 73728 Oct 15 19:42 lib64 drwxr-xr-x. 46 root root 12288 Oct 15 19:40 libexec drwxr-xr-x. 12 root root 131 Oct 15 19:36 local dr-xr-xr-x. 2 root root 20480 Oct 15 19:40 sbin drwxr-xr-x. 213 root root 8192 Oct 15 19:40 share drwxr-xr-x. 4 root root 34 Oct 15 19:36 src lrwxrwxrwx. 1 root root 10 Aug 10 2021 tmp -> ../var/tmp [root@bogon redhat]#
内容格式说明:
d r-xr-xr-x. 2 root root 45056 Oct 15 19:40 bin 1 2 3 4 5 6 7 81. 文件的类型 2. 文件的操作权限 3. 硬连接数 4. 文件所有者(创建文件的人) 5. 文件所发属组名称 6. 文件大小,单位为字节 7. 文件最后一次修改时间 8. 文件名称在 Linux 中一切皆文件。
-h 选项
[root@bogon redhat]# ls -l -h /usr total 240K dr-xr-xr-x. 2 root root 44K Oct 15 19:40 bin drwxr-xr-x. 2 root root 6 Aug 10 2021 games drwxr-xr-x. 3 root root 23 Oct 15 19:38 include dr-xr-xr-x. 38 root root 4.0K Oct 15 19:42 lib dr-xr-xr-x. 123 root root 72K Oct 15 19:42 lib64 drwxr-xr-x. 46 root root 12K Oct 15 19:40 libexec drwxr-xr-x. 12 root root 131 Oct 15 19:36 local dr-xr-xr-x. 2 root root 20K Oct 15 19:40 sbin drwxr-xr-x. 213 root root 8.0K Oct 15 19:40 share drwxr-xr-x. 4 root root 34 Oct 15 19:36 src lrwxrwxrwx. 1 root root 10 Aug 10 2021 tmp -> ../var/tmp# 我们可以在命令后面跟多个选项,多个选项之间用空格分隔,当然也可以把选项进行组全,例如: [root@bogon redhat]# ls -lh /usr total 240K dr-xr-xr-x. 2 root root 44K Oct 15 19:40 bin drwxr-xr-x. 2 root root 6 Aug 10 2021 games drwxr-xr-x. 3 root root 23 Oct 15 19:38 include dr-xr-xr-x. 38 root root 4.0K Oct 15 19:42 lib dr-xr-xr-x. 123 root root 72K Oct 15 19:42 lib64 drwxr-xr-x. 46 root root 12K Oct 15 19:40 libexec drwxr-xr-x. 12 root root 131 Oct 15 19:36 local dr-xr-xr-x. 2 root root 20K Oct 15 19:40 sbin drwxr-xr-x. 213 root root 8.0K Oct 15 19:40 share drwxr-xr-x. 4 root root 34 Oct 15 19:36 src lrwxrwxrwx. 1 root root 10 Aug 10 2021 tmp -> ../var/tmp
文件类型
在 linux 中一共有 七 种文件类,分别是如下:
- 表示普通文件 d 表示目录 l 链接文件(分为软链接和硬链接) c 表示字符设备文件 b 块设备文件 p 管道文件 s 套接字文件
[root@bogon ~]# ll /usr/bin/ls -rwxr-xr-x. 1 root root 140760 Jan 6 2023 /usr/bin/ls [root@bogon ~]# ll -d /root dr-xr-x---. 14 root root 4096 Oct 29 16:29 /root [root@bogon ~]# ll /dev/tty crw-rw-rw-. 1 root tty 5, 0 Oct 29 16:29 /dev/tty [root@bogon ~]# ll /dev/sr0 brw-rw----+ 1 root cdrom 11, 0 Oct 29 16:29 /dev/sr0 [root@bogon ~]# ll /usr/bin/yum lrwxrwxrwx. 1 root root 5 Mar 15 2023 /usr/bin/yum -> dnf-3
路径切换cd
这个命令的作用是用来切换用户的工作路径。cd 是 change directory 的简写。
它的语法格式:
[root@bogon ~]# cd --help cd: cd [-L|[-P [-e]] [-@]] [dir]Change the shell working directory.Change the current directory to DIR. The default DIR is the value of theHOME shell variable.
命令使用:
[root@bogon ~]# pwd /root [root@bogon ~]# cd /var/log [root@bogon log]# pwd /var/log [root@bogon log]# cd .. # 切换到上级目录 [root@bogon var]# pwd /var [root@bogon var]# cd /usr/local/etc [root@bogon etc]# ls [root@bogon etc]# pwd /usr/local/etc # 希望切换到 /usr 目录下 [root@bogon etc]# cd ../../ [root@bogon usr]# pwd /usr [root@bogon usr]# cd /usr# cd - 会在最近两目录之间来回切换 [root@bogon home]# cd - /root [root@bogon ~]# cd - /home [root@bogon home]# cd - /root [root@bogon ~]# cd - /home# 如果只写 cd 不带参数表示切换到 root 用户的家目录 [root@bogon home]# cd [root@bogon ~]# # cd ~ 也会切换到当前用户所在的家目录 [root@bogon home]# cd ~ [root@bogon ~]#