查找文件

find命令:查找文件

find命令:在linux系统中,按照我们的要求去查询文件。
    格式:
        find [查询的路径] [匹配模式] [匹配规则]

    匹配模式:
   		-name : 按照名字去匹配

    正则匹配:
        * : 匹配任意数量的任意字符(匹配零个或多个任意字符)
        ?: 匹配任意一个字符

    -type : 按照文件的类型匹配
        常见的文件类型:
            f : 普通文件
            d : 普通文件夹
            l : 链接文件
            c : 字符设备文件
            b : 块设备文件
            s : socket文件

    -perm : 按照文件的权限来查询
        常见的文件权限:
            755		: 文件夹的默认权限
            644		:  文件的默认权限

        -user : 按照文件的属主来查询
        -nouser :查询用户被删除了的文件

        -group : 按照文件的属组来查询
        -nogroup : 查询没有数组的文件

    知识储备:
        删除用户:userdel
        删除用户组:groupdel

    -mtime : 按照修改文件的时间来查询
        +      查询某个时间段之前的数据
        -      查询某个时间段之内的数据

    -size : 按照文件的大小来查询
        +	查询超过n的文件
        -	查询小于n的文件

    知识储备:
    	stat : 查看文件的各种时间
        -ctime : 按照文件的创建时间来查询
        -atime : 按照访问时间来查询文件

        -a(默认)	: 并且	
        -o			:或者
        -exec(xargs) 	: 处理匹配之后的内容

-name : 按照名字去查找

-name : 按照名字去匹配
    案例1:查询出/etc目录下的hosts文件
        [root@localhost ~]# find /etc/ -name "hosts"     # 按照名字查询
        /etc/hosts
    
    案例2:查询出/etc目录下的以ifcfg开头的文件
        [root@localhost ~]# find /etc/ -name "ifcfg*"    # 加上*号,可以匹配任意数量的字符,代表所有
        [root@localhost ~]# find /etc/ -name "ifcfg-eth?"
    
    案例3:查询出/etc目录下以.conf结尾的文件
    	[root@localhost ~]# find /etc/ -name "*.conf"
    
    案例4:查询出/etc目录下,文件名中包含host的文件有哪些
    	[root@localhost ~]# find /etc/ -name "*host*"

-type : 按照文件的类型查找

-type : 按照文件的类型匹配
    常见的文件类型:
    f : 普通文件
    d : 普通文件夹
    l : 链接文件
    c : 字符设备文件
    b : 块设备文件
    s : socket文件
    
    案例1:查询/etc/下的所有普通文件
		[root@localhost ~]# find /etc/ -type f
    
    案例2:查询出/etc目录下,所有的文件夹
		[root@localhost ~]# find /etc/ -type d
     
     案例3:查询出/dev/目录中的所有的块设备文件
		[root@localhost ~]# find /etc/ -type b
     
     案例4:查询出/dev/目录中的所有的字符设备文件
		[root@localhost ~]# find /etc/ -type c
		
	案例5:查询出/etc目录中文件名包含nginx的普通文件
		[root@localhost ~]# find /etc/ -name "*nginx*" -type f  # 中间有个-a,但是它是默认的

-perm : 按照文件的权限来查找

-perm : 按照文件的权限来查询
    常见的文件权限:
        755		: 文件夹的默认权限
        644		:  文件的默认权限
        
	案例1:查询出/root目录下,权限为755的文件
		[root@localhost tmp]# find /root/ -perm 755
		
	案例2:查询出/root目录下,权限为644的文件
        [root@localhost tmp]# find /tmp/ -perm 644
        /tmp/1.txt

-user : 按照文件的属主来查询

-user : 按照文件的属主来查询
-nouser :查询用户被删除了的文件

-group : 按照文件的属组来查询
-nogroup : 查询没有数组的文件

	案例1:查询出属主为test01的文件
		[root@localhost tmp]# mkdir test
		[root@localhost tmp]# chown test01 test
		[root@localhost tmp]# find /tmp/ -user test01
		
	案例2:查询属主被删除了的文件
		[root@localhost tmp]# userdel test01        # 删除test01数组的
		[root@localhost tmp]# find /tmp/ -nouser    # 查询没有用户的文件
         /tmp/test
         
     案例3:查询属组为test的文件
		[root@localhost tmp]# find /tmp/ -group test
         /tmp/test
         
     案例4:查询属组被删除了的文件
     	[root@localhost tmp]# find /tmp/ -nogroup-mtime : 按照修改文件的时间来查询

-ctime : 按照文件的创建时间来查询

-ctime : 按照文件的创建时间来查询
    +      查询某个时间段之前的数据
    -      查询某个时间段之内的数据
    
	案例1:查询2天以内创建的文件
        [root@localhost tmp]# find /tmp/ -ctime -2      # -2代表两天之内创建的文件
        /tmp/test12
        /tmp/1.txt
    
    案例2:查询3天之前创建的文件
    	[root@localhost tmp]# find /tmp/ -ctime +3 	   # +3代表三天之前创建的文件

-atime : 按照访问时间来查询文件

-atime : 按照访问时间来查询文件
    +      查询某个时间段之前的数据
    -      查询某个时间段之内的数据
    
	案例1:查询2天以内访问过的文件
		[root@localhost tmp]# find /tmp/ -atime -2
		
	案例2:查询3天之前访问过的文件
		[root@localhost tmp]# find /root/ -atime +3

-mtime : 按照修改文件的时间来查询

-mtime : 按照修改文件的时间来查询
    +      查询某个时间段之前的数据
    -      查询某个时间段之内的数据
    
	[root@localhost tmp]# stat 1.txt     # 查看文件的创建时间
    Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)     # 权限
    Access: 2021-09-28 14:34:53.175042121 +0800    		# 访问时间  
    Modify: 2021-09-28 14:34:53.175042121 +0800			# 修改时间
    Change: 2021-09-28 14:34:53.175042121 +0800			# 创建时间
    
    案例1:查询2天之内修改过的文件
    	[root@localhost tmp]# find /root/ -mtime -2
    	/tmp/
        /tmp/test12
        /tmp/1.txt
        /tmp/test1
        
	案例2:查询3天之前修改过的文件
		[root@localhost tmp]# find /root/ -mtime +3

-size : 按照文件的大小来查询

-size : 按照文件的大小来查询
    +	查询超过n的文件
    -	查询小于n的文件
    
    案例1:查询大于1M的文件
    	[root@localhost tmp]# find /tmp/ -size +1M
    	
    案例2:查询小于1M的文件
    	[root@localhost tmp]# find /tmp/ -size -1M

-exec(xargs) : 处理匹配之后的内容

-exec(xargs) : 处理匹配之后的内容
	知识储备:
            -exec	: 处理查询之后的内容
            {}		: 代表的是查询到的内容、
            \; 		:  固定搭配
            xargs    :将所有的内容格式化成一行
	
	案例1:查询在3天以内创建的文件,并删除
		[root@localhost tmp]# find /tmp/ -ctime -3 -type f -exec rm -rf {} \;       # 第一种方法
		
		[root@localhost tmp]# find /tmp/ -ctime -3 -type f | xargs -I {} rm -rf {}  # 第二种方法
		# 第一个{}:把查询到的内容放进去
		# 第二个{}:是把第一个{}内容拿进来再取出来
		
	案例2:要求将所有3天前创建的普通文件加上.bak后缀
		第一种方法:
			[root@localhost tmp]# find /tmp/ -ctime -3 -type f -exec mv {} {}.bak \;
			
		第二种方法:
			[root@localhost tmp]# find /tmp/ -ctime -3 -type f | xargs -I {} mv {} {}.bak

Linux系统压缩包

gzip压缩包

gzip压缩包:
    压缩软件,将文件做成一个压缩包,会删除原来的文件,生成一个新的压缩包文件。

    格式:
    	压缩:gzip [文件路径]

    解压:
    	gzip -d [压缩包路径]

    缺陷:
    	gzip不能压缩文件夹,只能压缩文件。
    	
    	[root@localhost tmp]# gzip init.sh
    	[root@localhost tmp]# gzip -d init.sh

bzip2压缩包

压缩软件,将文件做成一个压缩包,会删除原来的文件,生成一个新的压缩包文件。
    格式:
        压缩:bzip2 [文件路径]

    解压:
        bzip2 -d [压缩包路径]

    缺陷:
    	bzip2不能压缩文件夹,只能压缩文件。
    	
	gzip和bzip2区别:
        gzip 比 bzip2 压缩率更大

        [root@localhost tmp]# bzip2 init.sh
        [root@localhost tmp]# bzip2 -d init.sh.bz2

tar打包

tar:打包文件,不会删除原文件,也不会压缩文件;tar命令是可以跟gzip或者bzip2共同使用

	格式:
		tar [参数] [压缩包名称]

    参数:
        -f  : 指定压缩包名称

        -c  : 打包文件

        -z  : 指定使用gzip压缩,一般使用gzip压缩的文件都以.tar.gz作为扩展名

        -j  : 指定使用bzip2压缩,一般使用bzip2压缩的文件都以.tar.bz2作为扩展名

        -v  : 显示压缩包压缩的过程

        -x  : 解压,不需要指定压缩包的压缩类型,它会自动匹配压缩包的类型自行解压。

        -P  :当压缩包中存在根目录是,自动移除根目录

        -t  : 查看压缩包中的内容
        
	打包:
        [root@localhost tmp]# tar -cf init.sh.tar init.sh   # 打包
        
        [root@localhost tmp]# tar -czvf init.sh.tar.gz init.sh  # z:可以和gzip一起使用
        [root@localhost tmp]# tar -cjvf init.sh.tar.bz2 init.sh  # j:可以和bzip2一起使用
        
	解压: 
		[root@localhost tmp]# tar -xf init.sh.gz   # 不需要指定压缩包
		
    案例1:将/etc目录中的所有的普通目录文件压缩成/tmp/etc.tar.gz文件
    	[root@localhost tmp]# tar -czvPf /tmp/etc.tar.gz $(find /etc/ -type f | xargs) 
        [root@localhost tmp]# tar -tf etc.tar.gz  # 查看文件的内容
    	知识储备:
    		$()   :   相当于数学当中的(),优先级

Linux中的管道

管道:用于传输数据,可以将前一个命令的执行结果,交给管道之后的命令去处理

    格式:	
        | 

        案例1:删除/tmp目录下,一天以内创建的文件
        	[root@localhost tmp]# find /tmp/ -ctime -1 -type f | xargs -I {} rm -rf {}

        案例2:将etc中所有的普通文件,复制到/tmp目录中
        	[root@localhost tmp]# find /etc/ -type f | xargs -I {} cp -r {} /tmp/
posted @ 2021-09-28 21:29  迷恋~以成伤  阅读(307)  评论(0)    收藏  举报