linux文件查找 第七章

查找命令

grep:文件内容过滤

grep 'root' /etc/passwd

which:查找命令

which ls
which cd
whichrm
which只能查找命令文件,对普通文件没有用

where:查找命令和配置文件的位置

whereis rpm
whereis passwd

find详解

语法:find 路径 条件 跟文件相关的操作符 [-exec 动作]

按文件名查找

find / -name 'file'  按文件名查找/下名字为file的文件
find /etc -name 'ifcig-ens33'  以名字方式查找
find /etc -iname 'ifciG-ens33'  -i忽略大小写

按文件大小查找

find /etc -size +5M   查找/etc中大于5M的文件
find /etc -size 5M    查找/etc中等于5M的文件
find /etc -size -5M   查找/etc中小于5M的文件
find /etc -size -5M -a -size +3M  查找小于5M并且大于3M的文件
find /etc -size -5M -o -size +3M  查找小于5M或者大于3M的文件
find /etc -size -3M -a -name "*.txt"  查找小于3M并且以.txt结尾的文件

按时间查找

-atime=access访问时间

-mtime=modify改变时间   内容修改时间会改变

-ctime=change修改时间   属性修改时间会改变

-amin     #分钟

-mmin

-cmin
find / -mtime +5   修改时间5天之前
find / -atime +1   访问时间1天之前
find / -mtime -2   修改时间2天之内
find / -amin +1    访问时间1分钟之内
find / -amin -4    访问时间4分钟之内
find / -mmin -2    修改时间2分钟之内

按文件类型查找

find / -type f     f普通文件
find / -type f -size -2M -o -name '*.txt' 查找小于2M或者以.txt结尾的的普通文件
find / -type d -name '*.conf.d'   查找以conf.d结尾的目录文件
find / -type l   查找/下的链接文件
find / -type b   查找/下的块设备文件

按文件权限查找

find / -perm 755   查找/下权限为755的文件
find / -perm 4000  查找/下权限为4000的文件  (包含set uid)
find / -perm 2000  查找/下权限为2000的文件  (包含set gid)
find / -perm 1000  查找/下权限为1000的文件  (包含sticky)

找到后的处理动作

-name 'ifcfg'  | xargs

-name 'ifcfg'  | exec

find使用xargs

find /etc -name 'file' | xargs -i cp {} /opt
find /etc -name 'file' | xargs -I {} cp {} /opt   

find使用exec

find / -type f -name 'file' -exec rm -rf {} \;  找到名字为file的普通文件后删除
-exec:参数是一个一个传递的,传递一个参数执行一次命令。
xargs:将前一个命令的标准输出传递给下一个命令,作为它的参数转换成下一个命令的参数列表。
=================================================================
1、exec 每处理一个文件或者目录,它都需要启动一次命令,效率不好; 
2、exec 格式麻烦,必须用 {} 做文件的代位符,必须用 \来转义; 作为命令的结束符,书写不便。 
3、xargs不能操作文件名有空格的文件;

综上,如果要使用的命令支持一次处理多个文件,并且也知道这些文件里没有带空格的文件,
那么使用 xargs比较方便; 否则,就要用 exec了。
一、xargs的使用
(1)将前一个命令的标准输出传递给下一个命令,作为它的参数,xargs的默认命令是echo,空格是默认定界符
(2)将多行输入转换为单行

二、exec的使用
exec参数是一个一个传递的,传递一个参数执行一次命令。

总结:xargs一次将参数传给命令,可以使用-n控制参数个数;exec参数是一个一个传递的,传递一个参数执行一次命令。

[root@youngfit tmp]# find ./ -type f -name 'file*' |xargs -n 10 -i  rm -rvf {} 

分别找出test5和除了test5电容文件

[root@localhost ~]# find /home/ -name *test5*
[root@localhost ~]# find /home/ ! -name "test5*" # !--取反

打包压缩

linux打包压缩工具

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

bzip2(只压缩)

打包语法:tar cvf xxx.tar 被打包的文件名

c:创建 create

v:详细信息verbose

f:文件file

[root@localhost ~]# tar cvf /opt/files.txt.tar files.txt 将当前文件打包到/opt下
files.txt
[root@localhost ~]# ls /opt/
dir1  files.txt.tar
注:打包时尽量不要用绝对路径,否则解包时会发现连路径都打包进去了

解包语法:tar xvf xxx.tar [-C /etc/dir]

x:extract 解包

-C:指定解包路径

[root@localhost ~]# tar xvf /opt/files.txt.tar -C .
files.txt
[root@localhost ~]# ls
files.txt                                   

压缩

gzip bzip2
gzip 命令只能用来压缩文件,不能压缩目录,即便指定了目录,也只能压缩目录内的所有文件
[root@localhost ~]# gzip files.txt 
[root@localhost ~]# ls
files.txt.gz    
[root@localhost ~]# gzip -c files.txt > /opt/files.txt.gz    #-c 压缩到指定位置
[root@localhost ~]# ls /opt/
files.txt.gz 
bzip2需要安装  用法与gzip一样

[root@localhost ~]#  yum -y install bzip2
[root@localhost ~]# bzip files.txt 
[root@localhost ~]# ls
files.txt.bz2   
[root@localhost ~]# bzip -c files.txt > /opt/files.txt.bz2    # -c 压缩到指定位置
[root@localhost ~]# ls /opt/
files.txt.bz2 

解压缩

gzip -d xxx.tar.gz           gunzip xxx.tar.gz

bzip2 xxx.tar.bz2           bunzip xxx.tar.bz2

-d 解压缩
[root@localhost ~]# gzip -d files.txt.gz 
[root@localhost ~]# ls
files.txt 
[root@localhost ~]# gzip -d /opt/files.txt.gz -c > ./files.txt 
[root@localhost ~]# ls
files.txt  
[root@localhost ~]# gunzip -c /opt/files.txt.gz  > ./files.txt 
[root@localhost ~]# ls
files.txt 
 
[root@localhost ~]# bzip -d files.txt.bz2 
[root@localhost ~]# ls
files.txt
[root@localhost ~]# bzip -d /opt/files.txt.bz2 -c > ./files.txt 
[root@localhost ~]# ls
files.txt  
[root@localhost ~]# bunzip -c /opt/files.txt.bz2  > ./files.txt 
[root@localhost ~]# ls
files.txt 

打包压缩一起

[root@localhost ~]# tar czvf files.txt.tar.gz files.txt 
files.txt
[root@localhost ~]# ls
files.txt        files.txt.tar.gz 
z:表示gz压缩
j:表示bz2压缩

解压解包一起

[root@localhost ~]# tar xzvf files.txt.tar.gz -C ./
files.txt
[root@localhost ~]# ls
files.txt 

打包到指定路径

[root@youngfit.com ~]# tar czf /tmp/`date +%F-%T`-etc.tar.gz /etc/  #将打包的文件放到/tmp目录下,并以当前时间开头命名
 
按时间创建目录或者文件
# mkdir `date +%F`-upload
# touch file-`date +%F`.txt

链接文件

硬链接

一般情况下,文件名和inode号码是"一一对应"关系,每个inode号码对应一个文件名。但是,Unix/Linux系统允许,多个文件名指向同一个inode号码。
这意味着,可以用不同的文件名访问同样的内容;对文件内容进行修改,会影响到所有文件;但是,删除一个文件名,不影响另一个文件名的访问。这种情况就被称为"硬链接"(hard link)。

[root@localhost ~]# echo 222 > files.txt
[root@localhost ~]# ll -i files.txt     # -i 显示inode号
33637270 -rw-r--r--. 1 root root 4 3月  12 19:45 files.txt
[root@localhost ~]# ln files.txt files.txt-h1   #硬链接
[root@localhost ~]# ll -i files.txt files.txt-h1  #查看inode号
33637270 -rw-r--r--. 2 root root 4 3月  12 19:45 files.txt
33637270 -rw-r--r--. 2 root root 4 3月  12 19:45 files.txt-h1
[root@localhost ~]# rm -rf files.txt             #删除源文件
[root@localhost ~]# ll -i files.txt-h1           #查看链接文件
33637270 -rw-r--r--. 1 root root 4 3月  12 19:45 files.txt-h1
[root@localhost ~]# cat files.txt-h1 
222

运行上面这条命令以后,源文件与目标文件的inode号码相同,都指向同一个inode。inode信息中有一项叫做"链接数",记录指向该inode的文件名总数,这时就会增加1。

反过来,删除一个文件名,就会使得inode节点中的"链接数"减1。当这个值减到0,表明没有文件名指向这个inode,系统就会回收这个inode号码,以及其所对应block区域。
1、原文件与链接文件的inode号相同
2、删除原文件对链接文件没有影响
3、目录不支持硬链接

软链接

除了硬链接以外,还有一种特殊情况

文件A和文件B的inode号码虽然不一样,但是文件A的内容是文件B的路径。读取文件A时,系统会自动将访问者导向文件B。因此,无论打开哪一个文件,最终读取的都是文件B。这时,文件A就称为文件B的"软链接"(soft link)或者"符号链接(symbolic link)。

这意味着,文件A依赖于文件B而存在,如果删除了文件B,打开文件A就会报错:"No such file or directory"。这是软链接与硬链接最大的不同:文件A指向文件B的文件名,而不是文件B的inode号码,文件B的inode"链接数"不会因此发生变化。

ln -s 创建软链接

[root@localhost ~]# echo 111 >files.txt
[root@localhost ~]# ll -i files.txt
37181005 -rw-r--r--. 1 root root 4 3月  12 19:50 files.txt
[root@localhost ~]# ln -s files.txt files.txt-h2  #将文件file.txt软链接到files.txt-h2
[root@localhost ~]# ll files.txt-h2 
lrwxrwxrwx. 1 root root 9 3月  12 19:51 files.txt-h2 -> files.txt
[root@localhost ~]# ll -i files.txt files.txt-h2   #查看inode号
37181005 -rw-r--r--. 1 root root 4 3月  12 19:50 files.txt
33637270 lrwxrwxrwx. 1 root root 9 3月  12 19:51 files.txt-h2 -> files.txt
[root@localhost ~]# cat files.txt
111
[root@localhost ~]# cat files.txt-h2 
111
[root@localhost ~]# rm -rf files.txt-h2    #取消软链接
[root@localhost ~]# ln -s files.txt files.txt-h2
[root@localhost ~]# rm -rf files.txt       #删除源文件
[root@localhost ~]# ll files.txt-h2 
lrwxrwxrwx. 1 root root 9 3月  12 19:53 files.txt-h2 -> files.txt   #软链接已失效
 
#给目录设置软链接必须是绝对路劲
[root@localhost ~]# ln -s /root/aaa/ /usr/bbb
[root@localhost ~]# ll /usr/bbb
lrwxrwxrwx 1 root root 10 Dec 29 21:08 /usr/bbb -> /root/aaa/
[root@localhost ~]# rm -rf /usr/bbb  #取消链接,注意:删除目录链接时目录后面加“/”是删除目录下的内容,不加是删除链接
1、原文件与软链接文件inode号不一样
2、删除原文件链接文件也会失效
3、目录支持软连接文件

注:硬链接 
1. 不能跨文件系统
2. 不支持目录做硬链接

[root@localhost ~]# ln /root/aaa/ /mnt/bbb
ln: "/root/aaa/": 不允许将硬链接指向目录

面试题

- 软链接可以跨文件系统,硬链接不可以;
- 软链接可以对目录进行连接,硬链接不可以;
- 删除源文件之后,软链接失效,硬链接无影响;
- 两种链接都可以通过命令 ln 来创建;
- ln 默认创建的是硬链接;
- 使用 -s 参数可以创建软链接。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值