Linux-find与tar练习

本文详细介绍Linux下find命令的高级使用,包括文件查找、权限、类型、时间戳的筛选,以及通过shell思想进行文件处理。同时,深入解析tar、gzip、bzip2等压缩工具的使用方法,对比不同压缩格式的特性,以及如何测试压缩工具的性能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一. find介绍:

用途查找符合特征的文件并在此基础上执行相关操作。(which :找命令位置)

1.1 命令格式:

find 查找的路径 选项 文件名

1.2根据权限查找

[root@localhost etc]# find /etc -perm 644 #在/etc 查找644权限的文件 (精确匹配权限)
[root@localhost etc]# find . -perm -755 查找每一位权限都必须精确匹配文件权限,-内容可灵活的变化
Rwx r-x r-x
[root@localhost etc]# find . -perm /755 查找权限中有任何一们符合条件的文件

1.3 根据文件名查找

[root@localhost etc]# find / -name 1.txt
[root@localhost etc]# find / -name manual-zip.css  #根据文件名查找
/usr/local/apache/manual/style/css/manual-zip.css
/YSB/httpd-2.4.27/docs/manual/style/css/manual-zip.css
[root@localhost etc]# find / -iname MaNUal-zIp.cSS  #不区分大小写的查找
/usr/local/apache/manual/style/css/manual-zip.css
/YSB/httpd-2.4.27/docs/manual/style/css/manual-zip.css
[root@localhost etc]# find -name ifcfg-eth0			#不给路径的时候默认查找的是当前目录及当前目录子目录查找
./sysconfig/network-scripts/ifcfg-eth0
[root@localhost etc]# find . -name ifcfg-eth0
./sysconfig/network-scripts/ifcfg-eth0

1.4 根据文件大小查找

[root@localhost etc]# find /etc -size +512k		#查找大于512k的文件
/etc/selinux/targeted/modules/active/policy.kern
/etc/selinux/targeted/policy/policy.24
/etc/services
/etc/gconf/gconf.xml.defaults/%gconf-tree.xml
/etc/pki/tls/certs/ca-bundle.trust.crt
/etc/pki/tls/certs/ca-bundle.crt
[root@localhost etc]# ls -lh /etc/pki/tls/certs/ca-bundle.crt 
-rw-r--r--. 1 root root 740K 9月   4 2013 /etc/pki/tls/certs/ca-bundle.crt
[root@localhost etc]# find /etc -size -512k		#查找小于512k的文件
[root@localhost etc]# find /etc -size 512k		#查找等于512k的文件

1.5 根据文件的类型查找及所有文件类型

[root@localhost ~]# find /dev -type l 查找链接文件
[root@localhost ~]# find /etc -type f 查找普通文件
[root@localhost ~]# find /etc -type d 查找目录
注意:
-代表的是普通文件
d代表的是目录(/dev)
l代表链接(dev)
c代表字符文件(/dev)
b代表特殊块文件(/dev)
s socket(套接字文件)
p FIFO(管道文件)

1.6 根据时间来查找

-atime 访问时间
-ctime 改变时间
-mtime 修改时间
[root@localhost ~]# find /etc -amin 10 查找/etc目录中,恰好10分钟访问过的文件
[root@localhost etc]# find /etc -amin -10查找/etc目录中,恰好10分钟内访问过的文件
[root@localhost etc]# find /etc -amin +10查找/etc目录中,恰好10分钟前访问过的文件

查找一个文件在什么时间内是否被人改动过
[root@localhost etc]# find /usr/share/man/man8/iptables.8.gz -mtime -2 #两天以内没有人动过该文件
[root@localhost etc]# find /etc/sysconfig/iptables -mtime -2 #两天以内有人动过该文件

1.7 文件时间戳

文件的时间戳包含在它的元数据中,属于其本身属性信息。
文件的时间戳包含有三种时间分别如下:
acess time 访问时间
modify time 修改时间(更具体说是修改数据时的时间)
change time 改变时间 (修改元数据的时间)

1.8 三种时间戳的解释

access时间:读一次文件的内容,这个时间就会更新。(比如more、cat等命令)参照一下可能有问题。ls、stat命令不会修改atime

modify时间:修改时间是文件内容最后一次被修改的时间。比如:vim操作后保存文件。ls -l列出的就是这个时间

change时间:是该文件的inode节点最后一次被修改的时间,通过chmod、chown命令修改一次文件属性,这个时间就会更新。

1.9 查找为空的文件或目录(find shell 思想判断一个目录或文件是否为空)

[root@localhost etc]# find / -empty
[root@localhost etc]# find / -empty -type f |wc –l 系统中所有空文件的个数
验证一个文件是否为空文件
[root@localhost etc]# find /etc/resolv.conf -empty
[root@localhost etc]# touch 1.txt
[root@localhost etc]# find /etc/1.txt -empty #说明1.txt文件是空文件
/etc/1.txt

1.10 根据文件属主来查找

[root@localhost etc]# find / -user daiv #系统中所有为daiv用户文件

[root@localhost etc]# id daiv
uid=500(daiv) gid=500(daiv) groups=500(daiv)
[root@localhost etc]# find / -uid 500
/home/daiv
/home/daiv/.mozilla
/home/daiv/.mozilla/plugins
/home/daiv/.mozilla/extensions
/home/daiv/.bashrc
/home/daiv/.gnome2
/home/daiv/.bash_logout
/home/daiv/.bash_profile
/home/daiv/.viminfo
/home/daiv/.bash_history
find: /proc/3149/task/3149/fd/5': No such file or directory find:/proc/3149/task/3149/fdinfo/5’: No such file or directory
find: /proc/3149/fd/5': No such file or directory find:/proc/3149/fdinfo/5’: No such file or directory
/var/spool/mail/daiv

1.11 根据文件的属组来查找

[root@localhost etc]# find / -group daiv
[root@localhost etc]# id daiv
uid=500(daiv) gid=500(daiv) groups=500(daiv)
[root@localhost etc]#
[root@localhost etc]#
[root@localhost etc]# find / -gid 500

1.12 Find高级命令的使用:

查找一个文件并进行相应的动作。
[root@localhost /]# find /root/mytest -name linux-90
/root/mytest/file/file/linux-90
[root@localhost /]# find /root/mytest -name linux-90|xargs rm –rf #查找一个文件并删除
[root@localhost /]# find /root/mytest -name linux-90

xargs命令 :给出一个动作

-exec 命令 {} \ ;

1.13 OK命令:

[root@localhost mytest]# find /root/mytest -type f -ok ls –l;与exec {} \区别,查找出结果以询问的方式一步一步处理

1.14 find shell 思想:查找一个文件并移动一个目录

[root@localhost /]# a=find /root/mytest -name linux-11#定义一个变量并把find /root/mytest -name linux-11执行结果交给a变量
[root@localhost /]# echo KaTeX parse error: Expected 'EOF', got '#' at position 61: …ot@localhost /]#̲ mv "a" /tmp #mv /root/mytest/file/file/linux-11 /tmp
[root@localhost /]# cd /tmp

综合成一条命令
[root@localhost tmp]# b=find /root/mytest -name linux-12 && mv “$b” /tmp #只有&&命令执行成功以后才能执行&&后面的命令。

1.15 find 参数的自由组合总结

[root@localhost mytest]# find /root/mytest -type f -perm 644 -user root -mtime -5 -size 0k -name linux* -group root -empty
/root/mytest/file/linux-311

  1. 参数的自由组合
  2. 参数是and关系
  3. Find命令的查询的几中方式。

1.16 find 参数之间的关系

与: -a 相当于and
或:-o 相当于or
非:-not,!
运用
[root@localhost mytest]# find /root/mytest -not -perm 644 查找出权限不是644的文件
[root@localhost mytest]# find /root/mytest -name linux1 -o -name linux2 查找出以linux开头以1或者2结尾的文件
/root/mytest/file/linux-311
/root/mytest/file/linux-641
/root/mytest/file/linux-422

[root@localhost mytest]# find /root/mytest -name linux1 -a -name linux2查找出以linux开头并具同是以1和2结尾的文件

1.17 Find shell思想:取出查找的所有文件名并保存

[root@localhost mytest]# find /root/mytest -name linux* -printf “%f \n” >1.txt 其中%f表示输出文件名 \n表示换行符
[root@localhost mytest]# cat cat 1.txt
cat: cat: No such file or directory
linux-311
linux-794
linux-335
第15.14.1 find printf输出格式
%p 输出文件名抱括路径
%f表示输出文件名
%m以8进制方式输出文件权限
%h 输出所有文件的目录
%u 输出文件的所属主
%g输出文件的所属组
[root@localhost mytest]# find /root/mytest -name linux* -printf “filename:%f;filepath:%h;fileuid:%u;filegid:%g;fileperm:%m;all:%p \n” >1.txt
[root@localhost mytest]# cat 1.txt

1.18 find shell思想找出一个文件并过滤出关键指定内容(查找一个文件是否有关键字)

我们要删除jex帐号:/etc/passwd
[root@localhost mytest]# find /etc -name passwd |xargs grep “jex”
[root@localhost mytest]# find /etc -name hosts |xargs cat
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6

1.19 Linux常用的配置文件总结

[root@localhost /]# find /etc[在哪个位置上去找] -name ifcfg-eth0[查找文件的名称]
/etc/sysconfig/network-scripts/ifcfg-eth0
[root@localhost /]# find /var -name maillog
/var/log/maillog
[root@localhost /]# find /etc -name resolv.conf
/etc/resolv.conf
[root@localhost /]# find /etc/hosts
/etc/hosts
[root@localhost /]# find /etc -name CentOS-Debuginfo.repo
/etc/yum.repos.d/CentOS-Debuginfo.repo
[root@localhost /]# find /etc -name fstab
/etc/fstab
[root@localhost /]# find /etc -name inittab
/etc/inittab
[root@zcq_CentOS1 ~]# find / -name passwd
/etc/passwd 系统上的用户信息
[root@zcq_CentOS1 mytest]# find / -name httpd.conf
/etc/httpd/conf/httpd.conf apache配置文件
用户相关的配置文件添加这个用户相关记录
[root@zcq_CentOS1 ~]# cat /etc/passwd
daiv : x:501:501::/home/daiv:/bin/bash
在密码配置文件里添加这个用户相关的记录
[root@zcq_CentOS1 ~]# cat /etc/shadow
daiv: 6 6 6hWUXEAsz$SLplPHNxRiYCbPV5kO//HuTXhqqqkmsDeq2Mgyw.ZQZ8gKO624Q6g3x0/th2U7RA.y2nC3LGIGIQMpPJl2snw1:17439:0:99999:7:::
用户组相关配置文件添加这个用户相关组记录
[root@zcq_CentOS1 ~]# cat /etc/group
daiv: x:501:
用户相关的组配置文件添加这个用户相关记录
[root@zcq_CentOS1 ~]# cat /etc/gshadow
目录/etc/skel里面存放的是用户的初始环境变量。
创建用户策略全局文件
[root@zcq_CentOS1 ~]# cat /etc/login.defs

[root@localhost ~]# cat /etc/default/useradd
该文件主要是定义默认家目录、环境配置文件目录、登入执行首个程序等等

二、Linux压缩与解压缩

2.1 压缩与解压缩的应用

应用:网站下载源码 对数据库的备份与还原 网站传输页面过大进行压缩。
1:减少占用线路带宽
2:将文件进行归档处理

2.2 tar 命令介绍

可以为linux的文件和目录创建档案。利用tar,可以为某一特定文件创建档案(备份文件),也可以在档案中改变文件,或者向档案中加入新的文件。tar最初被用来在磁带上创建档案,现在,用户可以在任何设备上创建档案。利用tar命令,可以把一大堆的文件和目录全部打包成一个文件,这对于备份文件或将几个文件组合成为一个文件以便于网络传输是非常有用的。 首先要弄清两个概念:打包和压缩。打包是指将一大堆文件或目录变成一个总的文件;压缩则是将一个大的文件通过一些压缩算法变成一个小文件。
常用的压缩工具有:tar zip 、gzip、bzip2、xz
压缩准备工作:
[root@localhost aa]# cp -rf /etc . #复制文件夹到当前目录
[root@localhost aa]# ls
a etc
[root@localhost aa]# du -sh /etc #统计一个文件夹的大小
39M /etc

2.3 tar打包归档

tar 本身只能对文件归档但不能压缩解压操作,打包归档后的文件有可能比原文件大。

tar 命令的讲解
-c: 建立压缩档案
-f: 使用档案名字,切记,这个参数是最后一个参数,后买你只能接档案名;

压缩前的大小:
[root@localhost yy]# ls -l
总用量 12
drwxr-xr-x. 118 root root 12288 8月 26 00:20 etc
压缩后:
[root@localhost yy]# tar -cf yy.tar etc
[root@localhost yy]# ls
etc yy.tar
[root@localhost yy]# ls -l
总用量 34584
drwxr-xr-x. 118 root root 12288 8月 26 00:20 etc
-rw-r–r--. 1 root root 35399680 8月 26 00:20 yy.tar
[root@localhost yy]# du -sh yy.tar
34M yy.tar
[root@localhost yy]# du -sh etc
38M etc

2.4 tar压缩归档追加

-r:向压缩归档文件末尾追加文件
-f:使用档案名字,切记,这个参数是最后一个参数,后面只能接档案名。
[root@localhost yy]# touch 1.txt
[root@localhost yy]# tar -rf yy.tar 1.txt

2.5 更新压缩归档内容

-u:更新原压缩包中的文件
-f:使用档案名字,切记,这个参数是最后一个参数,后面只能接档案名。
[root@localhost yy]# ls -l
总用量 34588
-rw-r–r--. 1 root root 23 8月 26 00:27 1.txt
drwxr-xr-x. 118 root root 12288 8月 26 00:20 etc
-rw-r–r--. 1 root root 35399680 8月 26 00:28 yy.tar
[root@localhost yy]# cat 1.txt
this is my first linux
[root@localhost yy]# echo “this is my first linux”>1.txt
[root@localhost yy]# cat 1.txt
this is my first linux
[root@localhost yy]# tar -uf yy.tar 1.txt
[root@localhost yy]# ls -l
总用量 34596
-rw-r–r--. 1 root root 23 8月 26 00:29 1.txt
drwxr-xr-x. 118 root root 12288 8月 26 00:20 etc
-rw-r–r--. 1 root root 35409920 8月 26 00:30 yy.tar
[root@localhost yy]# rm -rf 1.txt
[root@localhost yy]# ls -l
总用量 34592
drwxr-xr-x. 118 root root 12288 8月 26 00:20 etc
-rw-r–r--. 1 root root 35409920 8月 26 00:30 yy.tar
[root@localhost yy]# tar -xvf yy.tar
[root@localhost yy]# ls -l
总用量 34596
-rw-r–r--. 1 root root 23 8月 26 00:29 1.txt
drwxr-xr-x. 118 root root 12288 8月 26 00:20 etc
-rw-r–r--. 1 root root 35409920 8月 26 00:30 yy.tar
[root@localhost yy]# cat 1.txt
this is my first linux

2.6 查看压缩包的内容

-t:查看内容
-f:使用档案名,切记,这个参数是最后一个参数,后面只能接档案名。
[root@localhost yy]# tar -tf yy.tar
[root@localhost yy]# tar -tf yy.tar| grep “ifcfg-eth0” #搜索压缩包里面是否有指定的内容
etc/sysconfig/network-scripts/ifcfg-eth0
[root@localhost yy]#

2.7 tar解压缩

-x:解压
-v:显示所有过程
-f:使用文档名字,切记,这个参数是最后一个参数,后面只能接档案名。
[root@localhost yy]# tar -xvf daiv.tar

2.8 压缩或解压其他格式的参数

下面的参数是根据需要在压缩或解压档案时可选的。
-z:有gzip属性的(前提是安装了gzip工具)
-j:有bz2属性的(前提是安装了bzip2工具)
-Z:有compress属性的(前提是安装了compress工具)
-v:显示所有过程
-O:将文件解压到标准输出

2.9 .tar格式的压缩和解压

[root@localhost yy]# tar -cvf yy.tar etc
tar -xvf yy.tar #解压tar包

2.10 .tar.gz 格式的压缩和解压

[root@localhost yy]# tar -czf yy.tar.gz etc
tar -xzvf yy.tar.gz # 解压tar.gz

2.11 .tar.bz2格式的压缩和解压

[root@localhost yy]# tar -cif yy.tar.bz2 etc
tar -xivf yy.tar.bz2 etc #解压 tar.bz2

2.12 .tar.Z 格式的压缩和解压

[root@localhost mytest]# tar -cZf daiv.tar.Z etc //将目录里所有etc目录下面的所有文件打包成daiv.tar后,并且将其用compress压缩,生成一个umcompress压缩过的包,命名为daiv.tar.Z
tar –xZvf daiv.tar.Z //解压tar.Z

2.13 .rar格式的压缩和解压

rar a yy.rar * .jpg //rar格式的压缩,需要先下载rar for linux
unrar e yy.rar //解压rar

2.14 .zip 格式的压缩和解压

zip jpg.zip * .jpg //格式的压缩,需要先下载zip for linux
unzip file.zip //解压zip

2.15 压缩注意事项

1.文件的后缀对Linux系统无实际意义,但是在压缩解压有实际意义,要注意文件的扩展名;
2.gzip、bzip、xz都只支持压缩文件,不支持目录;
3.tar 本身只能对文档归档但不能压缩解压操作,通过调用gzip、bzip2、xz实现归档压缩
4.tar 纯粹做归档操作,得到的文件大小会比其归档的每个文件大小和还要大
5.tar 展开压缩的归档文件是无需指明需要解压扩展的文件的压缩算法,因为可以自动识别;
6.压缩格式各项类型:.tar .tar.gz .tar.bz2 .tar.Z zip rar

2.16 如何测试压缩工具的性能(时间,压缩比)

[root@localhost data]# time zip test.zip test.mp3
adding: test.mp3
(deflated 100%)

real 0m16.070s
user 0m7.190s
sys 0m4.481s
[root@localhost data]#
[root@localhost data]# ls -lh
total 2.0G
drwx------. 2 root root 16K Sep 8 23:05 lost+found
-rw-r–r--. 1 root root 2.0G Sep 18 16:25 test.mp3
-rw-r–r--. 1 root root 2.0M Sep 18 16:27 test.zip

三、Linux文件查看

3.1 Linux查看文件内容的常用命令

文件查看工具:内容(cat less more ) 文件类型(file )
cat (一次显示整个文件内容)

3.2 cat 常用三种功能

1:一次显示整个文件的内容
[root@localhost yy]# cat 1.txt
this is my first linux
2:创建一个文件
[root@localhost yy]# cat >2.txt
nihao 2.txt #输入的内容
^C #ctrl+c结束输入
3:将几个文件合并成一个文件
[root@localhost yy]# cat 1.txt 2.txt >3.txt

3.3 cat -n显示时打印出行号

[root@localhost yy]# cat -n 3.txt
1 this is my first linux
2 nihao 2.txt

3.4 cat -b 空行不显示行号(nl空行显示行号)

[root@localhost yy]# cat -n 4.txt
1 nihao
2
3 nihao
4 ss
5 dd
6
7
8
9 sddff
10 adfasdfs
11 dfsdgsdf
12
13
14
15 acfasdcf
16
17
[root@localhost yy]# cat -b 4.txt
1 nihao

 2	nihao 
 3	ss
 4	dd



 5	sddff
 6	adfasdfs
 7	dfsdgsdf



 8	acfasdcf

3.5 cat 生成文件

[root@localhost yy]# cat >yy.txt<<EOF

this is cat
sddd
dfdfd
fdf
DDD
FGG
dd
dsfs
EOF
反向显示:
[root@localhost yy]# tac yy.txt
dsfs
dd
FGG
DDD
fdf
dfdfd
sddd
this is cat
拼接显示:
[root@localhost yy]# cat 3.txt yy.txt
this is my first linux
nihao 2.txt
this is cat
sddd
dfdfd
fdf
DDD
FGG
dd
dsfs
不仅可以从文件中读取内容,还能添加内容。
[root@localhost mytest]# echo “hei hei” |cat - 1.txt
注意:这里的-代表的是"hei hei"的临时文件

Cat file1 – file2 先输出file1 中间是从链盘输入最后显示file2. Ctrl+D
[root@localhost mytest]# cat 1.txt - 2.txt
11111
sigheigheirghrer
sigheigheirghrer
wgehgrehtrh
wgehgrehtrh
22222

-s 将连续的两行及以上的空白行,换成一行的空白行
[root@localhost mytest]# cat 1.txt
11111

222222

333333

444444
[root@localhost mytest]# cat -s 1.txt
11111

222222

333333

444444

[root@localhost mytest]# cat -A /etc/fstab 在每行的最后加上一个$

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值