十、文件查找,打包压缩

文件查找

grep: 文件内容过滤

[root@biudefor ~]# grep 'root' /etc/passwd   #从/etc/passwd文件中过滤root字段
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin
[root@biudefor ~]# grep '^root' /etc/passwd   #从/etc/passwd文件中过滤root开头的字段
root:x:0:0:root:/root:/bin/bash

查找命令

[root@biudefor ~]# which ls
alias ls='ls --color=auto'
        /usr/bin/ls
[root@biudefor ~]# which cd
/usr/bin/cd
[root@biudefor ~]# which rm
alias rm='rm -i'
        /usr/bin/rm

查询命令和配置文件的位置

[root@biudefor ~]# whereis rpm 
rpm: /usr/bin/rpm /usr/lib/rpm /etc/rpm /usr/share/man/man8/rpm.8.gz
[root@biudefor ~]# whereis passwd
passwd: /usr/bin/passwd /etc/passwd /usr/share/man/man1/passwd.1.gz
一、find详解: 文件查找,针对文件名
语法:
#find 路径 条件 跟条件相关的操作符   [-exec 动作]
路径:
1.默认不写路径时查找的是当前路径.
2.加路径。
条件:
1.指定的名称  -name
2.文件类型 - type

1.1.按文件名

从根开始找文件
[root@biudefor ~]# find / -name “file2” #从根开始找文件
/root/file2
/var/tmp/file2
[root@biudefor ~]# find /etc -name "ifcfg-ens33" #以名字的方式查找 
[root@biudefor ~]# find /etc -iname "ifcfg-ens33" #-i忽略大小写

熟用通配符*

[root@biudefor ~]# find /etc -iname "ifcfg-ens*"
参数解释:
*:表示所有字符

1.2.按文件大小 -size

[root@biudefor ~]# find /etc -size +5M      #大于5M
[root@biudefor ~]# find /etc -size 5M       #等于5M
[root@biudefor ~]# find /etc -size -5M      #小于5M
[root@biudefor ~]# find /etc -size +5M -ls  #-ls找到的处理动作,不是平时用的ls

1.3按时间查找

按时间找(atime,mtime,ctime)
-atime=  access访问时间
-mtime = modify改变时间  内容修改时间会改变
-ctime =change修改时间   属性修改时间会改变
[root@biudefor ~]# find /etc -mtime +5      #修改时间5天之前
[root@biudefor ~]# find /etc -atime +1      #访问时间1天之前
[root@biudefor ~]# find /etc -mtime -5      #修改时间5天之内

1.4按文件类型

[root@biudefor ~]# find /dev -type f    #f普通文件
[root@biudefor ~]# find /dev -type d    #d目录
[root@biudefor ~]# find /dev -type l    #l链接
[root@biudefor ~]# find /dev -type b    #b块设备
[root@biudefor ~]# find /dev -type c    #c字符设备
[root@biudefor ~]# find /dev -type s    #s套接字

1.5按文件权限

[root@biudefor ~]# find . -perm 644            #.是当前目录    精确查找644  
[root@biudefor ~]# find /usr/bin  -perm -4000  #包含set uid
[root@biudefor ~]# find /usr/bin  -perm -2000  #包含set gid
[root@biudefor ~]# find /usr/bin  -perm -1000  #包含sticky

1.6找到后处理的动作 actions

-name "ifcfg*" | xargs
-name "ifcfg*" -print   #打印
[root@biudefor ~]# find /etc -name "ifcfg*" -exec cp -rf {} /tmp \; #exec命令用于调用并执行指令的命令  查找带ifcfg开头的文件复制到tmp下
[root@biudefor ~]# touch /home/test{1..20}.txt
[root@biudefor ~]# find /home/ -name test* -exec rm -rf {} \; #exec为执行一条shell命令      {}为前面查找到的内容   \; 格式

find使用xargs

[root@biudefor ~]# find . -name "yang*.txt" |xargs rm -rf #重点 找到之后删除处理xargs 参数传递处理找出后删除
[root@biudefor ~]# touch /home/test{1..20}.txt
[root@biudefor ~]# find /home/ -name *test* | xargs -I {} cp -rf {} /var/tmp/

-exec和xargs的区别

-exec:命令会对每个匹配的文件执行一个单独的操作
xargs:是对参数进行处理的命令。它的任务就是将输入行转换成下一个命令的参数列表。
===============
1、exec 每处理一个文件或者目录,它都需要启动一次命令,效率不好; 
2、exec 格式麻烦,必须用 {} 做文件的代位符,必须用 \; 作为命令的结束符,书写不便。 
3、综上,如果要使用的命令支持一次处理多个文件, 那么使用 xargs比较方便; 
4、xargs不能操作文件名有空格的文件;

案例1: 分别找出test5 和除了test5的文件

[root@biudefor ~]# find /home/ -name *test5*
[root@biudefor ~]# find /home/ ! -name *test5*      # !--取反(会显示隐藏文件)

二、打包压缩

linux打包压缩工具:

结尾:.tar.gz      .tar.bz2     .zip
工具:gzip  bzip2  zip(只压缩) 和 tar(打包)

打包

#tar cvf file.tar 被打包的文件 ...
c :create  创建
v :verbose 详细信息
f :file  文件
# tar cvf 包名.tar 要打包的目标

解包

#tar xvf 打包文件 [-C /root/Desktop]
x: extract  加压缩  解包
-C: 指定解包路径

案例

[root@biudefor ~]# tar cvf dir1.tar /home/dir10/ #打包目录dir10,将包命名为dir1.tar
[root@biudefor ~]# tar xf dir1.tar -C /usr/local/ #将dir1包解压到指定目录

压缩

gzip bzip2  # 没有bzip2就下载bzip2
压缩:
    #gzip  源文件 ...   #格式  file.gz结尾
    #bzip2 源文件 ...   #格式  file.bz2结尾

解压缩

#gunzip 目标压缩文件
#bunzip2 目标压缩文件
#gzip -d 目标压缩文件
#bzip2 -d 目标压缩文件
-d:dicompress 解压缩

案例

[root@biudefor ~]# gzip file1  #压缩
[root@biudefor ~]# gzip -d file1.gz #解压缩
[root@biudefor ~]# gunzip file1.gz  #也是解压缩包
[root@biudefor ~]# gzip -c file1 > /usr/local/file1.gz  #压缩到指定位置(注意以.gz结尾)
[root@biudefor ~]# gunzip -c /usr/local/file1.gz > /opt/file1 #解压到指定位置(解压出的名字可以自定义)

打包压缩一起:

#tar cvzf file.tar.gz  源文件 ...
#tar cvjf file.tar.bz2 源文件 ...
z:表示gz压缩
j:表示bz2压缩

解压解包一起:

#tar xvzf 压缩文件 [-C 解压路径]
#tar xvjf 压缩文件 [-C 解压路径]

案例

[root@biudefor ~]# tar czf dir1.tar.gz dir1   #打包并压缩
[root@biudefor ~]# tar xzf dir1.tar.gz -C /usr/local/ #解压到指定位置

打包到指定路径

[root@biudefor ~]# tar czf /tmp/`date +%F-%T`etc.tar.gz /etc/  #将打包的文件放到/tmp目录下,并以当前时间开头命名         
                                        \\\  更好看

打包并远程发送到某主机

[root@biudefor ~]# tar -czvf dir2.tar.gz dir1  | xargs -I {} scp {} 192.168.0.2:/root/

测试

查找系统内所有.gz结尾的文件并备份到/tmp/backup目录下
# find / -name "*.gz" | xargs -i cp {} /tmp/backup
查找10天以内被修改过的.txt结尾的文件
# find / -mtime -10 -name "*.txt"

三.链接文件

软链接 或 符号链接 硬链接

4.1.符号链接 symbolic link

[root@biudefor ~]# echo 111 > /file1
[root@biudefor ~]# ln -s /file1 /file11     //将文件file11软链接到file1
[root@biudefor ~]# ll /file11 
lrwxrwxrwx 1 root root 6 Dec 20 17:58 /file11 -> /file1

[root@biudefor ~]# cat /file1 
111
[root@biudefor ~]# cat /file11 
111

[root@biudefor ~]# rm -rf /file1 
[root@biudefor ~]# ll /file11 
lrwxrwxrwx 1 root root 6 Dec 20 17:58 /home/file11 -> /file1   //已失效

4.2.硬链接

[root@biudefor ~]# echo 222 > /file2
[root@biudefor ~]# ln /file2 /file2-h1
[root@biudefor ~]# ln /file2 /etc/file-h3
[root@biudefor ~]# ll /file2 /file2-h1 /etc/file-h3
-rw-r--r--. 3 root root 4 Nov  9 15:01 /etc/file-h3
-rw-r--r--. 3 root root 4 Nov  9 15:01 /file2
-rw-r--r--. 3 root root 4 Nov  9 15:01 /file2-h1

把一些重要文件做多个链接

注:硬链接 
1. 不能跨文件系统(分区)
2. 不支持目录做硬链接
 [root@biudefor ~]# ln /home/ /mnt
 ln: “/home/”: 不允许将硬链接指向目录

软链接和硬链接的区别:

- 软链接可以跨文件系统,硬链接不可以;
- 软链接需要绝对路径
- 软链接可以对目录进行连接,硬链接不可以;
- 删除源文件之后,软链接失效,硬链接无影响;
- 两种链接都可以通过命令 ln 来创建;
- ln 默认创建的是硬链接;
- 使用 -s 参数可以创建软链接。
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容