# 第五节课 管理文件与目录
# 一、文件与目录概念
Linux 系统中一切皆文件,即目录、字符设备、块设备、打印机、套辭等都以抽象的文件形式存在。这与 Windows 系统中的思维方式不同。
同样是査找文件,在 Windows 系统中,我们需要依次进入这个文件所在的磁盘分区(比如 E 盘),然后从中査找具体的文件路径,比如 E:\Linux_study。但是 Linux 系统中不存在 C 盘、D 盘等盘符的概念,査找文件需要从根目录 /
开始,然后依次进入文件所在的目录,比如/home/user01/。
项目 | windows | linux |
---|---|---|
文件夹区分大小写 | × | ✔ |
文件扩展名区分文件类型 | ✔ | × |
# 1.Linux 目录结构
目录 | 说明 |
---|---|
/ | 根目录 |
/boot | 用于存放内核和系统启动时需要的文件 |
/dev | 用于存放系统中的设备文件,比如 /dev/tty |
/etc | 用于存放系统中主要的配置文件,比如 /etc/passwd |
/home | 系统默认的用户家目录,比如 /home/jetlion |
/bin | 用于存放单用户模式下可以被用户使用的命令 |
/media | 挂载设备文件的目录 |
/mnt | 暂时挂载额外设备的目录 |
/opt | 用于存放第三方软件的目录 |
/proc | 是一个虚拟文件系统,用于存放内存中的数据,比如进程信息 |
/root | 系统管理员 root 的家目录 |
/run | 用于存放系统启动后产生的各种信息 |
/srv | 用于存放网络服务相关的数据 |
/sys | 与/proc 相似,是一个虚拟文件系统。用于存放内核与系统硬件信息相关的数据 |
/tmp | 用于存放临时文件的目录,任何用户都可以访问 |
/usr | 用于存放系统应用程序和与命令相关的系统数据 |
/var | 用于存放系统运行中经常会变化的文件,比如日志文件 |
# 2.相对路径和绝对路径
# 绝对路径
绝对路径就是从根目录 (/
) 开始以全路径的方式查找文件或目录,绝对路径一定要以 /
开头,比如/usr/local/share 这种方式就是绝对路径。
# 相对路径
相对路径指的是相对于用户当前所在目录的路径。.
代表当前目录, ..
代表上级目录。
假设当前用户的工作目录在/usr/local 下,那么它的上层目录(/usr 目录) 可以用 ../
表示
假设当前用户的工作目录在/usr/lib/grub,需要到/usr/lib/udev,可以写成:cd ../udev
# 二、文件和目录的进本操作
# mkdir 命令 (make directory)
mkdir 命令可以创建一个或多个新目录。默认情况下,目录需要一层一层地建立。如果想一次性创建多层目录,需要加上指定的选项才可以。
选项说明:
• -p
递归创建目录,一次可创建多层目录。
示例
[root@localhost ~] mkdir test #创建单个文件夹
[root@localhost ~] ls
test
[root@localhost ~] mkdir -p test1/test #创建多层目录
[root@localhost ~] ls
test test1
[root@localhost ~] cd test1/
[root@localhost test1] ls
test
2
3
4
5
6
7
8
9
# touch 命令 创建空文件、修改文件时间
我们在使用linux系统的时候经常会创建文件,touch就是我们经常使用文件创建的命令,还可以用于修改文件或者目录的时间属性
示例
[root@localhost ~] touch helloworld #创建文件
[root@localhost ~] ls
helloworld test test1
[root@localhost ~] touch test/aaa #在文件夹中创建文件
[root@localhost ~] cd test
[root@localhost test] ls
aaa
2
3
4
5
6
7
# dirname (directory name),basename 命令
dirmame 命令可以获取目录的名称,basename 命令可以获取路径中的文件名或者路径名
示例
[root@localhost ~] basename test1/test/ #获取文件夹名
test
[root@localhost ~] basename test/aaa #获取文件名
aaa
[root@localhost ~] dirname test/aaa #获取文件的文件夹名
test
2
3
4
5
6
# mv 命令 (move)
mv 命令不仅可以移动文件或目录,还可以对它们进行重命名操作。
示例
[root@localhost ~] cd test
[root@localhost test] ls
aaa
[root@localhost test] mv aaa bbb #重命名文件
[root@localhost test] ls
bbb
[root@localhost test] mv bbb ../test1/aaa #移动文件到指定目录
[root@localhost test] cd ../test1 #切换目录
[root@localhost test1] ls
aaa test
2
3
4
5
6
7
8
9
10
# cp 命令 (copy)
cp 命令可以用来复制文件或目录。
示例
[root@localhost test1] cp aaa bbb #复制文件
[root@localhost test1] ls
aaa bbb test
[root@localhost test1] mkdir newdir #复制目录
[root@localhost test1] ls
aaa bbb newdir test
[root@localhost test1] cp bbb ../test #复制文件到指定目录
[root@localhost test1] cp -r newdir/ ../test #复制目录到指定目录
[root@localhost test1] cd ../test #切换目录
[root@localhost test] ls
bbb newdir
2
3
4
5
6
7
8
9
10
11
# rm 命令(remove)
rm 命令可以用来删除文件或者目录。在 Linux 系统中,使用 rm 命令删除文件时,默认 会出现询问是否删除的提示信息。
[root@localhost test] rm bbb #删除文件
rm:是否删除普通空文件 "bbb"?y
[root@localhost test] ls
newdir
[root@localhost test] rm -r newdir/ #删除目录
rm:是否删除目录 "newdir/"?y
[root@localhost test] ls
[root@localhost test] cd ~ #切换目录
[root@localhost ~] ls
helloworld test test1
[root@localhost ~] rm -rf test1 #递归删除目录
[root@localhost ~] ls
helloworld test
2
3
4
5
6
7
8
9
10
11
12
13
# 三、查找文件命令
# whereis、which 命令
whereis 命令可以在某些特定的目录中查找文件。
示例
[root@localhost /] whereis passwd
passwd: /usr/bin/passwd /etc/passwd /usr/share/man/man1/passwd.1.gz
2
which 命令可以在 PATH 变量指定的路径中搜索某个系统命令的位置,我们可以使用这个命令查看某个命令是否存在,或者执行的到底是什么位置的命令。which 命令査找的就是可执行文件。
示例
[root@bogon /] which passwd
/usr/bin/passwd
2
# find 命令
find命令可以按照指定条件搜索文件,用户可以使用文件名、文件大小、所属用户、修改时间、文件类型等条件进行搜索。
语法格式
find [查找路径] [选项] 匹配条件
选项说明:
• -name
匹配名称。
示例
[root@localhost /] find -name helloworld #查找 helloworld的文件
./root/helloworld
[root@localhost /] find / -name helloworld #在根目录下查找名称为 helloworld的文件
/root/helloworld
[root@localhost /] find / -name hello* #模糊搜索 以hello开头的文件
/boot/grub2/i386-pc/hello.mod
/root/helloworld
/usr/lib/grub/i386-pc/hello.mod
/usr/share/doc/cmake-2.8.12.2/Example/Hello/hello.cxx
/usr/share/doc/cmake-2.8.12.2/Example/Hello/hello.h
/usr/share/tk8.5/demos/hello
/usr/share/cmake/Modules/IntelVSImplicitPath/hello.f
/www/server/redis/deps/lua/test/hello.lua
/www/server/redis/src/modules/helloacl.c
/www/server/redis/src/modules/helloblock.c
/www/server/redis/src/modules/hellocluster.c
/www/server/redis/src/modules/hellodict.c
/www/server/redis/src/modules/hellohook.c
/www/server/redis/src/modules/hellotimer.c
/www/server/redis/src/modules/hellotype.c
/www/server/redis/src/modules/helloworld.c
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# 四、文件解压、文件夹压缩
在Windos系统中,最常见的压缩文件是.zip和.rar,Linux系统就不同了,它有.gz、.tar.gz、.tgz、.bz2、.Z、.tar 等众多的压缩文件。
# gzip、bzip2、xz 压缩命令
gzip 命令只能用来压缩文件,不能压缩目录
[root@localhost ~] ls
helloworld test
[root@localhost ~] gzip helloworld #压缩文件.gz格式
[root@localhost ~] ls
helloworld.gz test
[root@localhost ~] gzip -d helloworld.gz #解压文件
[root@localhost ~] ls
helloworld test
[root@localhost ~] bzip2 helloworld #压缩文件.bz2格式
[root@localhost ~] ls
helloworld.bz2 test
[root@localhost ~] bzip2 -d helloworld.bz2 #解压文件
[root@localhost ~] ls
helloworld test
[root@localhost ~] xz helloworld #压缩文件.xz格式
[root@localhost ~] ls
helloworld.xz test
[root@localhost ~] xz -d helloworld.xz #解压文件
[root@localhost ~] ls
helloworld test
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# tar 命令
Linux 系统中的很多压缩命令只能针对一个文件进行压缩,这样当需要压缩大量文件时,常常借助 tar 命令将这些文件先打成一个包,再使用压缩命令进行压缩。这种打包和压缩的操作在进行网络传输时是非常有必要的。
tar命令常用选项
选项 | 说明 |
---|---|
-c | 将多个文件或目录进行打包 |
-A | 追加 tar 包到归档文件 |
-f 包名 | 指定包的文件名 |
-v | 显示打包或解包的过程 |
-x | 对 tar 包做解包操作 |
-t | 只查看 tar 包中有哪些文件或目录,不对 tar 包做解包操作 |
-C 目录 | 指定解包位置 |
-v | 详细报告 tar 处理的信息 |
-z | 通过 gzip 命令过滤归档 |
-j | 通过 bzip2 命令过滤归档 |
-t | 列出归档文件中的内容,查看已经备份了哪些文件 |
# tar压缩、解压缩文件
# tar 打包,解压
[root@localhost ~] mkdir test #创建文件夹
[root@localhost ~] ls
test
[root@localhost ~] touch test/helloworld #文件夹内创建文件
[root@localhost ~] ls
test
[root@localhost ~] tar -cvf test.tar test/ #打包,不压缩文件夹
test/
test/helloworld
[root@localhost ~] ls
test test.tar
[root@localhost ~] tar -tvf test.tar #查看包内的内容
drwxr-xr-x root/root 0 2022-10-20 12:44 test/
-rw-r--r-- root/root 0 2022-10-20 12:44 test/helloworld
[root@localhost ~] tar -xvf test.tar -C /home/ #解压到指定文件夹
test/
test/helloworld
[root@localhost ~] cd /home/
[root@localhost home] ls
test
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# tar 调用 gzip
[root@localhost ~] tar -zcvf test.tar.gz test #调用gzip打包,压缩
test/
test/helloworld
[root@localhost ~] ls
test testaa.tar test.tar test.tar.gz
[root@localhost ~] tar -zxvf test.tar.gz #解压
test/
test/helloworld
2
3
4
5
6
7
8