文件查找

 目录

grep: 文件内容过滤

查找命令

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

一、find详解: 文件查找,针对文件名

        1.1 按文件名

        1.2 按文件大小size

        1.3 按时间查找

        1.4 按文件类型

        1.5 按文件权限

        1.6 找到后的处理动作

二、打包压缩

三.链接文件

        3.1 硬链接

        3.2 软链接

grep : 文件内容过滤

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

查找命令

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


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

[root@marve1ous ~]# whereis rpm 
rpm: /usr/bin/rpm /usr/lib/rpm /etc/rpm /usr/share/man/man8/rpm.8.gz
[root@marve1ous ~]# whereis passwd
passwd: /usr/bin/passwd /etc/passwd /usr/share/man/man1/passwd.1.gz

一、find详解: 文件查找,针对文件名

        1.1 按文件名

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

通配符
[root@marve1ous ~]# find /etc -iname "*.txt"
参数解释:
*:表示所有字符

        1.2 按文件大小

[root@marve1ous ~]# find /etc -size +5M		#大于5M
[root@marve1ous ~]# find /etc -size 5M		#等于5M
[root@marve1ous ~]# find /etc -size -5M      #小于5M
[root@marve1ous ~]# find / -size +3M -a -size -5M  #查找/下面大于3M而且小于5M的文件
-a:and
[root@marve1ous ~]# find / -size -1M -o -size +80M #查找/下面小于1M或者大于80M的文件
-o:or
[root@marve1ous ~]# find / -size -3M -a -name "*.txt" #查找/ 下面小于3M而且名字是.txt的文件

        1.3 按时间查找

按时间找(atime,mtime,ctime)
-atime = access访问时间
-mtime = modify改变时间  内容修改时间会改变
-ctime = change修改时间   属性修改时间会改变

-amin  #分钟
-mmin
-cmin

[root@marve1ous ~]# find /opt -mtime +5		#修改时间5天之前
[root@marve1ous ~]# find /opt -atime +1     #访问时间1天之前
[root@marve1ous ~]# find . -mtime -2		#修改时间2天之内

[root@marve1ous ~]# find . -amin +1         #访问时间在1分钟之前
[root@marve1ous ~]# find /opt -amin -4      #访问时间在4分钟之内
[root@marve1ous ~]# find /opt -mmin -2      #修改时间在2分钟之内

        1.4 按文件类型

[root@marve1ous ~]# find /dev -type f	#f普通文件
[root@marve1ous ~]# find / -type f -size -1M -o -name "*.txt"

[root@marve1ous ~]# find /dev -type d	#d目录
[root@marve1ous ~]# find /etc/ -type d -name "*.conf.d"

[root@marve1ous ~]# find /etc -type l	#l链接

[root@marve1ous ~]# find /dev -type b	#b块设备
[root@marve1ous ~]# find /dev/ -type b -name "sd*"

        1.5 按文件权限

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

        1.6 找到后的处理动作(ACTIONS)

[root@marve1ous ~]# find /etc -name "ifcfg*" -exec cp -rf {} /tmp \; #exec命令对之前查找出来的文件做进一步操作-----  查找带ifcfg开头的文件复制到tmp下

[root@marve1ous ~]# touch /home/test{1..20}.txt

[root@marve1ous ~]# find /home/ -name test* -exec rm -rf {} \; #{}为前面查找到的内容,\; 格式

                1.6.1 find 使用xargs

[root@marve1ous ~]# touch /home/test{1..20}.txt
[root@marve1ous ~]# # find /home/ -name "test*" | xargs -i cp {} /tmp/ #找到之后删除处理xargs 参数传递

                1.6.2 -exec和xargs的区别

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

综上,如果要使用的命令支持一次处理多个文件,并且也知道这些文件里没有带空格的文件,那么使用 xargs比较方便; 否则,就要用 exec了。

 二、打包压缩

         2.1 Linux打包压缩工具

结尾:.tar.gz      .tar.bz2     .zip

工具:gzip和tar(打包)
bzip2(只压缩)

  打包、解包

语法:
#tar cvf xxxx.tar filename   被打包的文件 ...
c :create  创建
v :verbose 详细信息
f :file  指定文件
#tar xvf filename.tar [-C /root/Desktop]
x: extract  解压缩  解包
-C: 指定解包路径

 压缩、解压缩

gzip bzip2 xzip zip

语法:
压缩:
    #gzip  源文件   #格式  file.gz结尾
    #bzip2 源文件   #格式  file.bz2结尾

语法:
#gunzip    压缩文件
#bunzip2   压缩文件

#gzip     -d 压缩文件  
#bzip2  -d 压缩文件
-d:dicompress 解压缩

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

打包压缩一起:

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

解压解包一起:

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

三、链接文件

软链接 或 符号链接 硬链接

        3.1 硬链接

一般情况下,文件名和inode号码是"一一对应"关系,每个inode号码对应一个文件名。但是,Unix/Linux系统允许,多个文件名指向同一个inode号码。
这意味着,可以用不同的文件名访问同样的内容;对文件内容进行修改,会影响到所有文件;但是,删除一个文件名,不影响另一个文件名的访问。这种情况就被称为"硬链接"(hard link)。
[root@marve1ous ~]# echo 222 > /file2
[root@marve1ous ~]# ll -i /file2                 #-i:显示inode编号
34045994 -rw-r--r-- 1 root root 4 Dec 29 20:52 file2
[root@marve1ous ~]# ln /file2 /file2-h1
[root@marve1ous ~]# ll -i /file2 /file2-h1       #查看inode号
34045994 -rw-r--r-- 2 root root 4 7月  30 22:25 /file2
34045994 -rw-r--r-- 2 root root 4 7月  30 22:25 /file2-h1

[root@marve1ous ~]# rm -rf /file2        #删除源文件
[root@marve1ous ~]# ll -i /file2-h1      #查看链接文件
34045994 -rw-r--r--. 3 root root 4 Nov  9 15:01 /file2-h1
查看:
[root@marve1ous ~]# cat /file2-h1
222

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

#反过来,删除一个文件名,就会使得inode节点中的"链接数"减1。当这个值减到0,表明没有文件名指向这个inode,系统就会回收这个inode号码,以及其所对应block区域。

        3.2 软链接

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

文件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"链接数"不会因此发生变化。

                3.2.1 ln -s 命令可以创建软链接  

                        语法:ln -s  源文件  链接文件



[root@marve1ous ~]# echo 111 > /file1
[root@marve1ous ~]# ll -i /file1 
545310 -rw-r--r-- 1 root root 4 7月  30 22:06 /file1
[root@marve1ous ~]# ln -s /file1 /file11		#将文件file1软链接到file11
[root@marve1ous ~]# ll /file11 
lrwxrwxrwx 1 root root 6 Dec 20 17:58 /file11 -> /file1

[root@marve1ous ~]# ll -i /file11 /file1    #查看inode号
545310 -rw-r--r-- 1 root root 4 7月  30 22:06 /file1
545343 lrwxrwxrwx 1 root root 6 7月  30 22:06 /file11 -> /file1

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

[root@marve1ous ~]# rm -rf /file11 #取消软连接。

[root@marve1ous ~]# ln -s /file1 /file11
[root@marve1ous ~]# rm -rf /file1  #删除源文件
[root@marve1ous ~]# ll /file11 
lrwxrwxrwx 1 root root 6 Dec 20 17:58  /file11 -> /file1   #已失效

#给目录设置软链接必须是绝对路径
[root@marve1ous ~]# ln -s /root/aaa/ /usr/bbb
[root@marve1ous ~]# ll /usr/bbb
lrwxrwxrwx 1 root root 10 Dec 29 21:08 /usr/bbb -> /root/aaa/
[root@marve1ous ~]# rm -rf /usr/bbb  
#取消链接,注意:删除目录链接时目录后面加“/”是删除目录,不加是删除链接

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

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

硬链接和软连接的区别

- 软链接可以跨文件系统,硬链接不可以;

- 软链接可以对目录进行连接,硬链接不可以;

- 删除源文件之后,软链接失效,硬链接无影响;

- 软连接有不同的inode号,硬链接相同的inode号;

- 两种链接都可以通过命令 ln 来创建;

- ln 默认创建的是硬链接;

- 使用 -s 参数可以创建软链接。

删除软连接目录时,切记不要在目录名后面加/,否则会将目录中的文件删除
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值