diff --git a/文件查找及压缩.md b/文件查找及压缩.md deleted file mode 100644 index aec4c5e..0000000 --- a/文件查找及压缩.md +++ /dev/null @@ -1,267 +0,0 @@ -

文件查找及压缩

- ------- - -## 一:文件查找 - -文件查找分为:命令文件查找和任意文件查找 - -### 1. 命令文件查找 - -#### whereis - -​ 用于查找与命令相关的二进制文件、源码和手册页的位置 - -```bash -语法格式:whereis 命令 - -[root@wxin ~]# whereis ls -ls: /usr/bin/ls /usr/share/man/man1/ls.1.gz /usr/share/man/man1p/ls.1p.gz -[root@wxin ~]# 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 -``` - -#### which - -​ 在 $PATH 环境变量中查找可执行文件的路径 - -```bash -语法格式:which 命令 - -[root@wxin ~]# which ls -alias ls='ls --color=auto' - /usr/bin/ls -[root@wxin ~]# which vim -/usr/bin/vim -``` - -### 2. 任意文件查找 - -#### locate - -​ 通过预建的数据库快速查找文件,速度更快,但数据库可能未实时更新。 - -```bash -语法格式:locate <文件> -# 更新数据库(需要 root 权限) -[root@wxin ~]# sudo updatedb - -[root@wxin ~]# locate nginx.conf -/etc/nginx/nginx.conf -# 限制搜索结果数量 -[root@wxin ~]# locate -n 10 "*.conf" -/etc/GeoIP.conf -/etc/asound.conf -/etc/brltty.conf -/etc/chrony.conf -/etc/dleyna-server-service.conf -/etc/dnsmasq.conf -/etc/dracut.conf -/etc/e2fsck.conf -/etc/extlinux.conf -/etc/fprintd.conf -``` - -#### find - -​ 是实时搜索文件的工具,支持通过多种条件(名称、类型、时间、大小等)查找文件。 - -```shell -语法:find [路径] [选项] [操作] -``` - -选项 - -| 选项 | 说明 | -| :-------------: | :--------------------------------------: | -| -name <文件名> | 按文件名查找(支持通配符* ?) | -| -iname <文件名> | 按文件名查找(忽略大小写) | -| -type <类型> | 按文件类型查找(f = 文件,d = 目录) | -| -itime <天数> | 按修改时间查找(+7 = 7天前,-7 = 7天内) | -| -size <大小> | 按文件大小查找(如 +10M = 大于10MB) | -| -exec <命令> | 对查找到的文件执行命令 | - -**按文件名:** - -```bash -[root@wxin ~]# find /etc -name nginx.conf -/etc/nginx/nginx.conf -[root@wxin ~]# find /etc -iname Nginx.conf -/etc/nginx/nginx.conf -[root@wxin ~]# find /etc -iname Nginx.* -/etc/yum.repos.d/nginx.repo -/etc/nginx/nginx.conf -``` - -**按文件大小:** - -```bash -[root@wxin ~]# find /etc/ -size +5M // 大于5MB -/etc/udev/hwdb.bin -[root@wxin ~]# find /etc/ -size 5M // 等于5MB -[root@wxin ~]# find /etc/ -size -5M // 小于5MB -/etc/ -/etc/fstab -... -``` - -**指定查找的目录深度:** - -```bash -可查找范围 -[root@wxin ~]# find / -maxdepth 4 -a -name "ifcfg-en*" -/etc/sysconfig/network-scripts/ifcfg-ens33 - -不可查找范围 -[root@wxin ~]# find / -maxdepth 3 -a -name "ifcfg-en*" - -``` - -**按文件属主、属组找:** - -```bash -按照属主 -[root@wxin ~]# find /home -user wxin -/home/wxin -/home/wxin/.mozilla -/home/wxin/.mozilla/extensions - -按照属组 -[root@wxin ~]# find /home -group wxin -/home/wxin -/home/wxin/.mozilla -/home/wxin/.mozilla/extensions -``` - -**按文件类型:** - -```bash -f 普通文件 -[root@wxin ~]# find /tmp -type f -/tmp/.X0-lock -/tmp/CentOS-Base.repo - -b 块设备文件 -[root@wxin ~]# find /dev -type b -/dev/dm-1 -/dev/dm-0 - -d 目录 -[root@wxin ~]# find /etc -type d -/etc -/etc/fonts - -p 管道 -[root@wxin ~]# find /run -type p -/run/dmeventd-client -/run/dmeventd-server - -l 链接 -[root@wxin ~]# find /usr/tmp -type l -/usr/tmp -``` - -**按文件权限:** - -```bash -[root@wxin ~]# find . -perm 644 -ls -35642065 4 -rw-r--r-- 1 root root 18 12月 29 2013 ./.bash_logout -35642066 4 -rw-r--r-- 1 root root 176 12月 29 2013 ./.bash_profile -``` - -**找到后处理的动作:** - -```bash -找到后默认是显示文件 -[root@wxin ~]# find /home -perm 755 -ls -50331763 0 drwxr-xr-x 3 root root 18 12月 27 13:24 /home -51871671 0 drwxr-xr-x 4 wxin wxin 39 12月 27 13:20 /home/wxin/.mozilla -[root@wxin ~]# find /home -perm 755 -print -/home -/home/wxin/.mozilla -/home/wxin/.mozilla/extensions -/home/wxin/.mozilla/plugins - -找到后删除 -[root@wxin ~]# find /etc -name "775*" -/etc/7750 -[root@wxin ~]# find /etc -name "775*" -delete -[root@wxin ~]# find /etc -name "775*" - -找到后复制 -[root@wxin ~]# find /etc -name "ifcfg-en*" -exec cp {} /tmp \; -[root@wxin ~]# find /tmp -name "ifcfg*" -/tmp/ifcfg-ens33 -``` - -## 二:文件打包及压缩 - -### 1. 简介 - -​ tar (Tape Archive)是 Linux 中用于 **打包、压缩、解包文件或目录** 的工具,支持多种压缩格式(如 .tar.gz,.tar.bz2,.tar.xz)。 - -### 2. 功能与参数 - -**核心功能** - -| 功能 | 说明 | -| :-------: | :--------------------------------------------------------: | -| 打包 | 将多个文件/目录合并为单个 .tar 文件(不压缩) | -| 压缩 | 结合压缩算法(如 gzip、bzip2)生成 .tar.gz、.tar.bz2等压缩 | -| 解包/解压 | 提取 .tar 或压缩包中的文件 | -| 查看内容 | 列出压缩包内的文件列表。 | - -**常用参数** - -| 参数 | 说明 | -| :--------------------: | :---------------------------------------------------: | -| -c | 创建新归档文件(Create)。 | -| -x | 解压/提取文件(eXtract)。 | -| -t | 列出归档文件内容(List)。 | -| -f <文件名> | 指定归档文件名(必选参数)。 | -| -z | 使用 **gzip** 压缩(生成 .tar.gz)。 | -| -j | 使用 **bzip2** 压缩(生成 .tar.bz2)。 | -| -J | 使用 **xz** 压缩(生成 .tar.xz ,压缩率高但速度慢)。 | -| -v | 显示操作过程的详细信息(Verbose)。 | -| -C <目录> | 解压到指定目录(Change directory)。 | -| --exclude | 排除指定文件或目录。 | -| --preserve-permissions | 保留文件权限(或用 -p ) | - - - -### 3. 用法 - -语法: - -```shell -tar [选项] [压缩包名称] [源文件] -``` - -打包与压缩: - -```bash -# 打包目录为 .tar 文件(不压缩) -[root@wxin ~]# tar -cf etc.tar /etc - -# 打包并压缩为 .tar.gz(gzip) -[root@wxin ~]# tar -czvf etc-gzip.tar.gz /etc - -# 打包并压缩为 .tar.bz2(bzip2) -[root@wxin ~]# tar -cjvf etc-bzip.tar.bz2 /etc - -# 打包并压缩为 .tar.xz(xzip) -[root@wxin ~]# tar -cJvf etc-xzip.tar.xz /etc -``` - -解压与查看: - -```bash -# 解压 .tar 文件到当前目录 -[root@wxin ~]# tar -xvf /root/etc.tar - -# 解压 .tar.gz 到指定目录 -[root@wxin ~]# tar -xzvf /root/etc-gzip.tar.gz -C /tmp - -# 仅列出 .tar.bz2 中的文件列表(不解压) -[root@wxin ~]# tar -tjvf /root/etc-bzip.tar.bz2 -``` \ No newline at end of file