文件查找与打包压缩 查找命令 grep: 文件内容(数据信息)过滤 1 2 3 [root@c-6 ~]# grep 'root' /etc/passwd #从/etc/passwd文件中过滤root字段,查找的是文件内容 root:x:0:0:root:/root:/bin/bash operator:x:11:0:operator:/root:/sbin/nologin
which、type -a 和whereis 1 2 3 4 5 6 7 8 9 [root@c-6 ~]#which ls #查看命令的别名和存放路径, alias ls='ls --color=auto' /usr/bin/ls [root@c-6 ~]# type -a ls #效果与which一致 ls 是 `ls --color=auto' 的别名 ls 是 /usr/bin/ls [root@c-6 ~]# whereis ls #查看命令以及其相关文件 ls: /usr/bin/ls /usr/share/man/man1/ls.1.gz
find详解: 文件查找,针对文件名 1 2 3 4 5 6 7 8 9 10 语法: # find 路径 条件 跟条件相关的操作符 [-exec 动作] 路径: 1.默认不写路径时查找的是当前路径. 2.加路径。 条件: 1.指定的名称 -name 2.文件类型 -type 3.权限 4.时间
按文件名 1 2 3 4 5 6 7 8 9 10 11 12 [root@c-6 ~]# find / -name "ldq" #从根开始查找名为ldq的文件 /root/ldq /var/spool/mail/ldq /home/ldq [root@c-6 ~]# find /mnt -name "centos" #查找mnt下的名为centos的文件 /mnt/centos [root@c-6 ~]# find /mnt -iname "centos" #-iname不区分大小写 /mnt/centos /mnt/Centos [root@c-6 ~]# find /root -name "*.txt" #*通配符,查找root下所有以.txt结尾的文件 /root/2024-06-26 09:25:17.txt /root/20240626-09:26:31.txt
按文件大小 -size 1 2 3 4 5 6 7 8 [root@c-6 ~]# find / -size +5M #大于50M [root@c-6 ~]# find /etc -size 5M #约等于5M [root@c-6 ~]# find /etc -size -5M #小于5M [root@c-6 ~]# find / -size +3M -a -size -5M #查找/下面大于3M而且小于5M的文件 -a:add [root@c-6 ~]# find / -size -1M -o -size +80M #查找/下面小于1M或者大于80M的文件 -o:or [root@c-6 ~]# find / -size -3M -a -name "*.txt" #查找/ 下面小于3M而且名字结尾是.txt的文件
按时间查找 1 2 3 4 5 6 7 8 9 按时间找(atime,mtime,ctime) # 天 -atime= access访问时间 -mtime = modify改变时间 内容修改时间会改变 -ctime =change修改时间 属性修改时间会改变 -amin #分钟 -mmin -cmin
1 2 3 4 5 6 7 [root@c-6 ~]# find /opt -mtime +5 #修改时间5天之前 [root@c-6 ~]# find /opt -atime +1 #访问时间1天之前 [root@c-6 ~]# find . -mtime -2 #当前目录下修改时间2天之内 [root@c-6 ~]# find . -amin +1 #当前目录下访问时间在1分钟之前 [root@c-6 ~]# find /opt -amin -4 #访问时间在4分钟之内 [root@c-6 ~]# find /opt -mmin -2 #修改时间在2分钟之内
按文件类型 1 2 3 4 5 6 7 8 9 10 [root@c-6 ~]# find /dev -type f #f普通文件 [root@c-6 ~]# find / -type f -size -1M -o -name "*.txt" [root@c-6 ~]# find /dev -type d #d目录 [root@c-6 ~]# find /etc/ -type d -name "*.conf.d" [root@c-6 ~]# find /etc -type l #l链接 [root@c-6 ~]# find /dev -type b #b块设备 [root@c-6 ~]# find /dev/ -type b -name "sd*"
按文件权限 1 [root@c-6 ~]# find . -perm 644 #.是当前目录 精确查找644
找到后处理的动作 find使用exec find 目录 选项 参数 -exec 动作 ;
1 2 3 [root@c-6 ~]# find /etc -name "ifcfg*" -exec cp -rf {} /tmp \; #exec命令对之前查找出来的文件做进一步操作----- 查找带ifcfg开头的文件复制到tmp下 [root@c-6 ~]# touch /home/test{1..20}.txt [root@c-6 ~]# find /home/ -name test* -exec rm -rf {} \; #{}为前面查找到的内容,\; 格式
find使用xargs 1 2 3 [root@c-6 ~]# find /etc -name "ifcfg*" | xargs -i {} /tmp #利用管道将etc下查找到的以ifcfg开头的文件复制到tmp目录下 [root@c-6 ~]# touch /home/test{1..20}.txt [root@c-6 ~]# # find /home/ -name "test*" | xargs -i cp {} /tmp/ #找到之后删除处理xargs 参数传递
两种方法最终实现的效果是一样的,但是他们中间的实现过程是不一样的,假如用find命令将要查找出来的文件有100个,需要将他们移动到指定位置。使用管道的话直接查出来100个文件,然后一次性将100个文件全部移动到指定位置。如果使用exec的话,是查到一个移动一个,所以最终他会一共移动100次。这就是两者之间的区别。
打包压缩 打包压缩一起:
1 2 tar cvzf file.tar.gz 源文件 tar cvjf file.tar.bz2 源文件
解压+解包:
1 2 tar xvzf 压缩文件 [-C 解压路径] tar xvjf 压缩文件 [-C 解压路径]
范例:
1 2 3 4 5 6 7 8 9 [root@c-6 ~]# tar cvzf /opt/no.tar.gz /root/NO.txt tar: 从成员名中删除开头的“/” /root/NO.txt [root@c-6 ~]# ls /opt/ lftp-4.4.8-14.el7_9.x86_64.rpm no.tar.gz QF2208 test [root@c-6 ~]# tar xvzf /opt/no.tar.gz -C /opt/ root/NO.txt [root@c-6 ~]# ls /opt/root/ NO.txt
可以发现在打包压缩的时候提示了“tar: 从成员名中删除开头的/”这样一句话,但是后续可以看到还是打包压缩成功了。但是我们会发现我们原本只想打包的NO.txt文件,又多打包了他的上一级目录,虽说里边没有别的文件,但是结果并不是我们想要的。要想解决这种情况,我们需要在打包压缩时,源文件的路径表示时使用相对路径即可。如果使用的绝对路径的话就会出现如上情况。
按时间创建目录或者文件 1 2 # mkdir `date +%F`-upload # touch file-`date +%F`.txt
范例:
1 [root@c-6 ~]# tar czf /tmp/`date +%F-%T`-etc.tar.gz /etc/ #将etc目录打包放到/tmp目录下,并以当前时间开头命名
作业 1 2 3 4 查找系统内所有.gz结尾的文件并备份到/tmp/backup目录下 [root@c-6 ~]# find / -name "*.gz"|xargs -i cp -rvf {} /tmp/backup 查找10天以内被修改过的.txt结尾的文件 [root@c-6 ~]# find / -mtime +10 -name "*.txt"
链接文件 1 2 一般情况下,文件名和inode号码是"一一对应"关系,每个inode号码对应一个文件名。但是,Unix/Linux系统允许,多个文件名指向同一个inode号码。 这意味着,可以用不同的文件名访问同样的内容;对文件内容进行修改,会影响到所有文件;但是,删除一个文件名,不影响另一个文件名的访问。这种情况就被称为"硬链接"(hard link)。
1 2 3 4 5 6 7 8 9 10 11 12 13 创建硬链接语法:ln 源文件 链接文件 [root@c-6 ~]# echo 222 > /file2 [root@c-6 ~]# ll -i /file2 #-i:显示inode编号或者stat命令 34045994 -rw-r--r-- 1 root root 4 Dec 29 20:52 file2 [root@c-6 ~]# ln /file2 /file2-h1 [root@c-6 ~]# 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@c-6 ~]# rm -rf /file2 #删除源文件 [root@c-6 ~]# ll -i /file2-h1 #查看链接文件 34045994 -rw-r--r--. 3 root root 4 Nov 9 15:01 /file2-h1 [root@c-6 ~]# cat /file2-h1 #删除后仍可以看到内容 222
1 2 3 注:硬链接 1. 不能跨文件系统 2. 不支持目录做硬链接
1 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"链接数"不会因此发生变化。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 语法:ln -s 源文件 链接文件 [root@qfedu.com ~]# echo 111 > /file1 [root@qfedu.com ~]# ll -i /file1 545310 -rw-r--r-- 1 root root 4 7月 30 22:06 /file1 [root@qfedu.com ~]# ln -s /file1 /file11 #将文件file1软链接到file11 [root@qfedu.com ~]# ll /file11 lrwxrwxrwx 1 root root 6 Dec 20 17:58 /file11 -> /file1 [root@qfedu.com ~]# 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@qfedu.com ~]# cat /file1 111 [root@qfedu.com ~]# cat /file11 111 [root@qfedu.com ~]# rm -rf /file11 #取消软连接。 [root@qfedu.com ~]# cat /file1 #源文件不受影响 111 [root@qfedu.com ~]# ln -s /file1 /file11 [root@qfedu.com ~]# rm -rf /file1 #删除源文件 [root@qfedu.com ~]# ll /file11 lrwxrwxrwx 1 root root 6 Dec 20 17:58 /file11 -> /file1 #已失效 # 给目录设置软链接必须是绝对路劲 [root@qfedu.com ~]# ln -s /root/aaa/ /usr/bbb [root@qfedu.com ~]# ll /usr/bbb lrwxrwxrwx 1 root root 10 Dec 29 21:08 /usr/bbb -> /root/aaa/ [root@qfedu.com ~]# rm -rf /usr/bbb #取消链接 注意:删除目录链接时目录后面加“/”是删除目录内所有内容,不加是删除链接
面试:软链接和硬链接的区别:
1 2 3 4 5 6 7 8 9 - 软链接可以跨文件系统,硬链接不可以; - 软链接可以对目录进行连接,硬链接不可以; - 删除源文件之后,软链接失效,硬链接无影响; - 两种链接都可以通过命令 ln 来创建; - ln 默认创建的是硬链接; - 使用 -s 参数可以创建软链接。 - 硬链接两者之间iNode号相同,软链接两者之间iNode号不同。 - 软链接删除目录链接时目录后面加“/”是删除目录内所有内容,不加是删除链接。 - 给目录设置软链接必须是绝对路劲