liunx文件查找
文件查找主要有三种查找类型,which,locate,find!
which:命令查找,能且只能够查找命令的位置,例如:which cat,查找cat命令所在的命令。
[root@localhost ~]# which cat //查找命令cat所在的位置
/usr/bin/cat
locate:文件查找,该查找依赖数据库,使用该查找只能够查找开机以前就存在的文件,开机以后创建的文件查不到,因为数据库只有开机的时候会对全机进行扫描,录入数据库,开机后创建的文件没有录入数据库,在这里要想查找到开机后创建的文件需要使用:updatedb,命令来更新数据库。
[root@localhost ~]# touch 19941109 //创建一个文件名为19941109
[root@localhost ~]# locate 19941109 //查找19941109文件,没有查到
[root@localhost ~]# updatedb //更新数据库
[root@localhost ~]# locate 19941109 //查找19941109文件,结果在下面显示
/root/19941109
find:文件查找,这是我们使用最多的文件查找命令,因为find查找功能众多,且方便好用,按照查找方式可以分为6种,后面还有动作,即移动,复制等。
其命令格式: find [path...] [options] [expression] [action]
命令 路径 选项 表达式 动作
1:按文件名查找:find 路径 -iname(不区分大小写,-name区分大小写) "ens33"("这里面的根据需要想查什么输入什么")
2:按文件大小查找:find 路径 -size +5M(+5M是大于5M,5M等于5M,-5M小于5M)
3:指定查找的目录深度:find 路径 -maxdepth 4(最大查找深度) -a(and,和,执行前面的条件后执行后面的条件) -name "ifcfg-en*" (同,1按文件名查找)
4:按文件的属主,属组来查找:find 路径 -user 用户名 ;find 路径 -group 用户组名
5:按文件类型来查找:find 路径 type 文件类型 文件类型:f普通文件,b块设备文件,d目录,p管道,l连接
6:按文件权限查找:find 路径 -perm 权限
7:复制:find 路径 -name "ifcfg*" -ok(固定语法) cp {}(代指前面查找出来的内容) /tmp(目录路径) \;(固定语法)
[root@localhost ~]# find /etc -name "hosts" //在etc下查找名为hosts的文件
/etc/hosts //查找结果
[root@localhost ~]# find /etc -size +5M //查找文件大于5M的文件
/etc/udev/hwdb.bin //查找结果
[root@localhost ~]# ll /etc/udev/hwdb.bin -h //检查查找结果,显示其大于5M
-r--r--r--. 1 root root 8.2M 8月 4 16:33 /etc/udev/hwdb.bin
[root@localhost ~]# find / -maxdepth 4 -a -name "ifcfg-en*" //查找最大目录深度为4的文件名为ifcfg-en的文件,如果把最大目录深度改为3,那么将会查不到任何东西,也就是查找失败
/etc/sysconfig/network-scripts/ifcfg-ens33
[root@localhost ~]# find / -user a //查找用户为a的文件
/var/spool/mail/a
[root@localhost ~]# find / -group jishuyuan //查找用户组为jishuyuan的文件
/tmp/bayi
[root@localhost ~]# find / -type d //查找文件类型为D的文件,下面有很多很多
/proc/14100/task/14103
[root@localhost ~]# find /home -perm 777 //查找文件权限为777的问加你
/home/1.txt
[root@localhost ~]# find /etc -name "ifcfg*" -ok cp {} /tmp \; //将搜到的文件复制到/tmp
< cp ... /etc/sysconfig/network-scripts/ifcfg-lo > ? y
< cp ... /etc/sysconfig/network-scripts/ifcfg-ens33 > ? y
文件打包和压缩
tar命令是Unix/Linux系统中备份文件的可靠方法,几乎可以工作于任何环境中,它的使用权限是所有用户。建议针对目录。
语法是:tar 选项 压缩包名称 源文件;解压缩语法:tar -xf etc.tar(解压到当前目录) -C 路径(解压到指定路径)
压缩方式主要有三种:gzip,bzip,xzip,还有一种不压缩直接打包,其占用空间最大,xzip压缩速度最慢,但是xzip压缩之后占据空间越小。
gzip语法:tar -czf 压缩包名 源文件
bzip语法:tar -cjf 压缩包名 源文件
xzip语法:tar -cJf 压缩包名 源文件
[root@localhost ~]# tar -cf etc.tar /etc //不压缩,直接进行打包
tar: 从成员名中删除开头的“/”
[root@localhost ~]# tar -czf etc.tar.gzip /etc //进行gzip压缩
tar: 从成员名中删除开头的“/”
[root@localhost ~]# tar -cjf etc.tar.bzip /etc //进行bzip压缩
tar: 从成员名中删除开头的“/”
[root@localhost ~]# tar -cJf etc.tar.xzip /etc //进行xzip压缩
tar: 从成员名中删除开头的“/”
[root@localhost ~]# ll -h etc.tar
etc.tar.bzip etc.tar.gzip etc.tar.xzip //查看四种方法的结果情况
-rw-r--r--. 1 root root 38M 8月 4 19:48 etc.tar
-rw-r--r--. 1 root root 11M 8月 4 19:48 etc.tar.bzip
-rw-r--r--. 1 root root 12M 8月 4 19:48 etc.tar.gzip
-rw-r--r--. 1 root root 8.3M 8月 4 19:50 etc.tar.xzip
[root@localhost ~]# tar -xf etc.tar.xzip -C /home //指定解压路径到/home
[root@localhost ~]# ls /home //查看解压到指定目录是否成功
1.txt a amu etc test1 test2 test3 test.txt user02 zhang
到这里关于liunx文件查找,打包和压缩就全部讲完了,您可以根据我的代码进行练习,适当的做出一些修改,能够有助于您的掌握。有任何不懂得您也可以加我创建的liunx系统技术交流群426841431;您也可以加我qq:984722674