S1-P6 文件搜索、打包归档
约 1054 字大约 4 分钟
2024-11-28
任务1:查找特定文件
任务内容:
查找
group.cfg
文件所在位置find / -name grub.cfg
查找出所有和
grub
有关的文件,保存到file.txt
中find / -name *grub* > file.txt
查找系统中的日志文件,保存为
/root/log.txt
find / -name *.log > /root/log.txt
任务2:查找文件进行管理
任务内容:
查找
/tmp
目录中更改时间在一周前的文件并删除find /tmp -mtime +7 -exec rm -rf {} \;
查找系统中所有txt文件、更改时间在5日以上文件,确认后决定是否删除
find / -name "*.txt" -mtime +5 -ok rm -rf {} \;
查找系统中日志文件,并移动到/root中
find / -name "*.log" -exec mv {} /root \;
查找系统中cfg文件,并备份为文件
/root/backup
find / -name "*.cfg" -exec cp {} /root/backup/ \;
任务3:查询文件内容
任务内容:
找出并显示/etc/grub2.cfg文件中含有“BEGIN”单词的行
grep "BEGIN" /etc/grub2.cfg
至少两种方法找出并显示
/etc
下所有passwd文件中root用户条目方法一:
grep "root" /etc/passwd*
方法二:
find /etc/ -name "passwd*" -exec grep "root" {} \;
统计当前系统中有多少用户
wc -l /etc/passwd
任务4:文件打包归档
任务内容:
将
/boot
目录压缩为gzip格式,保存到家目录中tar -czf boot.tar.gz /boot
将
/boot
目录压缩为bzip2格式,保存到家目录中最小化安装时默认不带
bzip2
包,需要手动安装yum install bzip2
tar -cjf boot.tar.bz2 /boot
在
/etc
目录下查找以“pass”开头的文件find /etc/ -name "pass*"
查找用户
user01
的所有文件find / -user user01
查找并删除后缀名为
html
的文件,删除前确认find / -name "*.html" -ok rm -rf {} \;
在/usr/bin目录下查找以”l”开头的文件,并复制到家目录下的mybin目录
find /usr/bin/ -name "l*" -exec cp {} /home/mybin/ \;
拓展1
mkdir /root/findresults
find / -user iar -exec cp -r {} /root/findresults \;
由于我系统中没有
/usr/share/dict/words
文件,这里以/etc/passwd
中查找bash
为例
grep "bash" /etc/passwd > /root/lines.txt
cat /root/lines.txt

tar -cjf /root/backup.tar.bz2 /usr/local/

拓展2
查找
/etc
下所有xml文件,并打包为xml.tar
,保存到家目录中cd ~ mkdir /tmp/xml find /etc/ -name "*.xml" -exec cp {} /tmp/xml/ \; tar -cvf xml.tar /tmp/xml
将当前目录中的
myfile.txt
文件压缩为myfile.txt.tar.bz2
tar -cjf myfile.txt.tar.bz2 myfile.txt
打包
etc
目录到/test
中,文件名为etc.tar.gz
tar -zcvf /test/etc.tar.gz /etc/
把
user01
个人主目录下的abc
文件夹和123.txt
压缩成为abc123.zip
把
user01
个人主目录下的u1.zip
、u2.zip
、u3.zip
同时解压到user01
个人主目录里面创建指定压缩包:
touch u1 u2 u3 zip u1.zip u1 zip u2.zip u2 zip u3.zip u3
删除文件:
rm -rf u[1-3]
find ./ -name "u[1-3].zip" -exec unzip {} -d /home/user01 \;
版权所有
版权归属:DDoS_LING