CentOS下文件的查找与打包

文件的查找

find 文件查找 针对文件名

find [path...] [options] [expression] [action]

按文件名查找

# find 目录 指令 目标文件
find path -name FileName

root@CatdeXin:~# find /etc -name hosts
/etc/hosts
/etc/avahi/hosts

# 不区分大小写
find path -iname FileName

root@CatdeXin:~# find /etc -iname Hosts
/etc/hosts
/etc/avahi/hosts

# 万能匹配
find path -iname FileName*

root@CatdeXin:~# find /etc -iname Host*
/etc/hosts
/etc/host.conf
/etc/avahi/hosts
/etc/hostname

按文件大小来查找

find path -size size 
find path -size +size # 查找大于size的文件
find path -size -size # 查找小于size的文件


创建一个5M大小的文件
[root@Catdexin-PC ~]# dd if=/dev/zero of=/tmp/5M.txt bs=1M count=5
记录了5+0 的读入
记录了5+0 的写出
5242880字节(5.2 MB)已复制,0.00370124 秒,1.4 GB/秒

[root@Catdexin-PC ~]# find /tmp/ -size 5M
/tmp/5M.txt

指定查找的目录深度

find path -maxdepth 3 -a -name FileName # -a(and) 组合命令

[root@Catdexin-PC ~]# find / -maxdepth 3 -a -name '5M.txt'
/tmp/5M.txt
[root@Catdexin-PC ~]# find / -maxdepth 3 -a -size 5M
/var/log/vmware-vmsvc-root.log
/tmp/5M.txt

按文件属主、数组找

find path -user FileName
find path -group FileName

按文件类型

find path -type FileType

linux 文件类型 不以后缀作为区分 以长格式第一位做文件类型
+ f 普通文件

[root@Catdexin-PC ~]# ll
总用量 12
-rw-r--r--. 1 root root   22 2月   8 17:05 a.txt
[root@Catdexin-PC ~]# find ./ -type f
./a.txt

按文件权限

find path -perm 权限码
find path -perm 权限码 -ls # ls 是find的动作之一 精确权限

[root@Catdexin-PC dev]# find / -perm 644 -ls
1572929    4 -rw-r--r--   1 root     root           64 12月 22 10:46 /boot/grub2/device.map
......

找到后处理的动作ACTIONS

  • find默认自带动作 -print 打印

找到之后删除

find path -name FileName -delete

找到之后复制

# 回忆一下cp指令
cp -rvf 源文件 目标路径

find path -name FileName -ok cp -rvf {}  目标path \;
ok 外接其他指令
{} 替换符 
\; 结束符

[root@Catdexin-PC dev]# find /etc -name ifcfg* -ok cp -rvf {} /tmp \;
< cp ... /etc/sysconfig/network-scripts/ifcfg-lo > ? yes
"/etc/sysconfig/network-scripts/ifcfg-lo" -> "/tmp/ifcfg-lo"
< cp ... /etc/sysconfig/network-scripts/ifcfg-ens33 > ? yes
"/etc/sysconfig/network-scripts/ifcfg-ens33" -> "/tmp/ifcfg-ens33"
[root@Catdexin-PC dev]# ls /tmp/ | grep ifc*
ifcfg-ens33
ifcfg-lo

其他查找语句

which: 命令查找

# 查找一个命令所在的位置
root@CatdeXin:~# which ls
/usr/bin/ls
root@CatdeXin:~# which cp
/usr/bin/cp  

locate 文件查找 依赖数据库

root@CatdeXin:~# touch /home/nayue/temp/77665.txt

# 只有重启后才能找到文件 重新可以刷新数据库
root@CatdeXin:~# locate 77665.txt

# 我们手动刷新一下数据库
root@CatdeXin:~# updatedb

root@CatdeXin:~# locate 77665.txt
/home/nayue/temp/77665.txt

alias 定义命令(别名)

root@CatdeXin:~# alias ll='ls -l'
root@CatdeXin:~# ll
总用量 6208
drwxr-xr-x  3 root root    4096 2月  11 01:44  AuI18N
drwxr-xr-x  8 root root    4096 2月  12 12:42  Bin

文件的打包及压缩

    tar命令是Unix/Linux系统中备份文件的可靠方法,几乎可以工作在任何环境中,它的使用权限是所有用户,建议针对目录

打包 压缩

tar -选项 包名 源文件

[root@Catdexin-PC ~]# tar -cf etc.tar /etc
[root@Catdexin-PC ~]# tar -czf etc.tar.gz /etc
[root@Catdexin-PC ~]# tar -cjf etc.tar.bz /etc
[root@Catdexin-PC ~]# tar -cJf etc.tar.xz /etc
[root@Catdexin-PC ~]# ll -h
-rw-r--r--. 1 root root  38M 2月  19 17:15 etc.tar
-rw-r--r--. 1 root root  12M 2月  19 17:16 etc.tar.gz
-rw-r--r--. 1 root root  11M 2月  19 17:43 etc.tar.bz
-rw-r--r--. 1 root root 8.3M 2月  19 17:43 etc.tar.xz

携带z选项才是压缩,不携带只是打包(cf是创建文件CreateFile)

压缩的原理 - 去重法

  • 文件的体积变小 缺点无法直接使用 需要解压

解压 解包

tar -xf 压缩文件

# 对 就这么简单
posted @ 2021-02-19 17:54  CatdeXin  阅读(167)  评论(0)    收藏  举报