小白入门Liunx之文件目录管理2

Linux 文件与目录管理2

哦豁,上次还没有写完文件管理哟,那么这次讲解完毕,还有f文件查找和文件压缩等。
在这里插入图片描述
一、Linux下文件查找命令
Linux下一切皆文件!
1.命令查找

which 命令:找出命令的绝对路径
whereis 命令:找出命令的路径以及文档手册信息

[root@localhost ~]# which mkdir
/usr/bin/mkdir
[root@localhost ~]# whereis mkdir
mkdir: /usr/bin/mkdir /usr/share/man/man1/mkdir.1.gz /usr/share/man/man1p/mkdir.1p.gz /usr/share/man/man2/mkdir.2.gz /usr/share/man/man3p/mkdir.3p.gz

2.文件查找(find)

用法:
find 查找路径 选项 关键字

常见选项 含义 备注
-name 根据文件名查找文件
-iname 根据文件名忽略大小写查找
-size 根据文件大小来查找 +1M 大于1M -1M 小于1M 1M 等于1M
-type 根据文件类型来查找
-mtime 根据文件修改时间来查找文件 -n指n天以内,+n指n天以前
-atime 根据文件访问时间来查
-ctime 根据文件创建时间来查找文件
-perm 根据文件权限来查找文件
1)根据文件名查找
[root@localhost ~]# mkdir /test
[root@localhost ~]# touch /test/file1
[root@localhost ~]# touch /test/FILE1
[root@localhost ~]# find /test -name "file1"
/test/file1
[root@localhost ~]# find /test -iname "file1"
/test/file1
/test/FILE1
[root@localhost ~]# find /etc -name "*.conf"
/etc/resolv.conf
/etc/dnf/dnf.conf
/etc/dnf/plugins/copr.conf
/etc/dnf/plugins/debuginfo-install.conf
/etc/dnf/plugins/spacewalk.conf
/etc/dnf/protected.d/dnf.conf
/etc/dnf/protected.d/systemd.conf
/etc/dnf/protected.d/sudo.conf
/etc/fonts/conf.d/66-sil-nuosu.conf
(简单复制几个,还有很多)...
2)根据文件类型查找
[root@localhost ~]# find /usr/bin/ -type l (根据软连接查找)
/usr/bin/bunzip2
/usr/bin/bzcat
/usr/bin/bashbug
/usr/bin/sh
/usr/bin/geqn
/usr/bin/gneqn
[root@localhost ~]# find /dev -type b (根据块设备查找)
/dev/dm-1
/dev/dm-0
/dev/sr0
/dev/sda2
/dev/sda1
/dev/sda
[root@localhost ~]# find . -type d (根据目录查找)
.
./公共
./模板
./视频
./图片
./文档
./下载
./音乐
./桌面
[root@localhost test]# find . -type f (根据普通文件查找)
./file2
./file1
./FILE1
3)根据文件大小查找
[root@localhost ~]# find . -type f -size +1M
./.cache/tracker/meta.db
./.cache/tracker/meta.db-wal

[root@localhost ~]# find . -type f -size -1M
./.cache/gnome-shell/update-check-3.28
./.local/share/flatpak/.changed
./.mysql_history

[root@localhost tmp]# find . -type f -size -1024k
./dir1/file1
./dir1/file2
./test1
./test2
./test3
4)根据文件属性(权限,创建者和所属组)
[root@localhost ~]# find . -type f -perm 644
./.bash_logout
./.bash_profile
./.cache/tracker/meta.db

[root@localhost ~]# find . -user user01 -group user01 -type f
5)根据时间来查找
[root@localhost yum.repos.d]# find ./ -type f -mtime +2
./CentOS-AppStream.repo
./CentOS-Base.repo

[root@localhost yum.repos.d]# find ./ -type f -mtime -2
./.local.repo.swp
./local.repo

[root@localhost yum.repos.d]# find ./ -type f -mtime 2

3.使用动作查找

用法:
find 路径 选项 关键字 动作

常见动作 说明
-exec 对查找到的文件直接执行该参数后的shell命令
-ok 对查找到的文件询问式执行该参数后的shell命令
-delete 删除查找到的文件
-ls 列出查找到的文件,详细信息
-print 打印出查找到的文件(默认选项)
注意:
1. -exec或者-ok后面写完命令必须以空格反斜杠\;结尾( \;)
2. {}表示find命令所找出来的内容


二、Linux下文件压缩工具
1、常见的压缩与解压缩工具

压缩工具 说明 解压缩工具
zip 兼容类unix与windows,可以压缩多个文件或目录 unzip
gzip 压缩单个文件,压缩率相对低,cpu开销相对低 gunzip
bzip2 压缩单个文件,压缩率相对高,cpu开销相对高 bunzip2
xz 压缩单个文件,压缩率高,压缩时间相对长,解压速度快,cpu开销高 unxz

2、工具的用法
①:zip

压缩: zip 压缩后的文件 需要压缩的文件

选项: -r 递归压缩,压缩目录

注意:zip压缩默认压缩后的格式就是.zip;当然也可以加后缀.zip, 一般都加上

解压缩: -d 指定解压缩路径

②:gzip

压缩: gzip 压缩的单个文件

选项:
-r 递归压缩,压缩目录
-d 解压缩

解压缩:
gunzip 需要解压的文件
或者
gzip -d 需要解压的文件

gunzip file* 一次解压多个文件,代表通配符;file表示以file开头所有文件

③:bzip2工具

压缩: bzip2 压缩的单个文件

选项:
-d 解压缩

解压缩:
bunzip2 需要解压的文件
或者
bzip2 -d 需要解压的文件

④:xz工具

压缩: xz 压缩的单个文件

选项:
-z 压缩,默认
-d 解压缩 或者 unxz

解压缩:
unxz 文件名
或者
xz -d 文件名

三、Linux下文件打包工具
tar 命令:可以将多个文件打包成一个并且压缩,不会改变文件的属性,很常用。

用法: tar 选项 打包后的文件 需要打包的文件

常用选项 说明
-c 创建tar包(打包)
-z 调用gzip工具压缩
-j 调用bzip2工具压缩
-J 调用xz工具压缩
-v 显示详细信息
-f 指定包名
-x 解压
-C 指定解压路径
-t 列出或查看tar包内容
-r 往tar包里追加文件
注意说明:
1. 以上选项前面的横杠"-"可以省略
2. 如果已经将文件压缩打包,那么就不能追加;如果只是打包就可以追加。
3. 参数顺序需要注意,最好把-f参数放到所有参数后面。
4. 当出现以下提示时,加一个大P参数解决。 tar: Removing leading `/' from member names
打包压缩:
[root@localhost tmp]# tar -cvf /tmp/d1.tar /tmp/*
[root@localhost tmp]# ll
总用量 20
-rw-r--r--. 1 root   root   20480 6月  20 20:55 d1.tar

解压:
[root@localhost tmp]# tar -xf d1.tar (当前路径)

[root@localhost tmp]# tar -xf d1.tar -C abc/  (指定路径)

后续还会继续更新其他本章博客难免有些不足,恳请各位大佬不吝赐教!(╹▽╹)

猜你喜欢

转载自blog.csdn.net/weixin_44422985/article/details/106876624