习题练习(find+计划任务)

1. 使⽤ls查看/etc/⽬录下所有的⽂件信息
[root@Linux ~]# ls -l /etc/
总用量 1116
-rw-r--r--.  1 root root       16 5月  25 16:50 adjtime
-rw-r--r--.  1 root root     1518 6月   7 2013 aliases
-rw-r--r--.  1 root root    12288 5月  25 16:51 aliases.db
drwxr-xr-x.  2 root root      236 5月  25 16:48 alternatives

 
2. 使⽤ls查看/etc/⽬录下名包含“a”字⺟的⽂件或者⽬录信息
[root@Linux ~]# ls -l /etc/*a*
-rw-r--r--. 1 root root    16 5月  25 16:50 /etc/adjtime
-rw-r--r--. 1 root root  1518 6月   7 2013 /etc/aliases
-rw-r--r--. 1 root root 12288 5月  25 16:51 /etc/aliases.db
-rw-------. 1 root root   541 4月  11 2018 /etc/anacrontab
-rw-r--r--. 1 root root    55 4月  11 2018 /etc/asound.conf

 
3. 使⽤ls查看/etc/⽬录下以".conf"结尾的⽂件信息
[root@Linux ~]# ls -l /etc/*.conf
-rw-r--r--. 1 root root   55 4月  11 2018 /etc/asound.conf
-rw-r--r--. 1 root root 1045 5月  25 19:13 /etc/chrony.conf
-rw-r--r--. 1 root root 1285 4月  11 2018 /etc/dracut.conf
-rw-r--r--. 1 root root  112 4月  11 2018 /etc/e2fsck.conf
-rw-r--r--. 1 root root   38 4月  11 2018 /etc/fuse.conf
-rw-r--r--. 1 root root  842 11月  6 2016 /etc/GeoIP.conf

 
4. 使⽤ls查看/etc/⽬录中以"y"字⺟开头的⽂件信息

[root@Linux ~]# ls -l /etc/y*
-rw-r--r--. 1 root root 970 4月  13 2018 /etc/yum.conf

/etc/yum:
总用量 4
drwxr-xr-x. 2 root root   6 4月  13 2018 fssnap.d
drwxr-xr-x. 2 root root  54 5月  25 16:50 pluginconf.d
drwxr-xr-x. 2 root root  26 4月  13 2018 protected.d
drwxr-xr-x. 2 root root  37 4月  13 2018 vars
-rw-r--r--. 1 root root 444 4月  13 2018 version-groups.conf

/etc/yum.repos.d:
总用量 4
-rw-r--r--. 1 root root 342 5月  25 18:49 pp.repo

5. find查找/var/⽬录中以“.log”⽂件
[root@Linux ~]# find /var -name "*.log" -type f
/var/log/tuned/tuned.log
/var/log/audit/audit.log
/var/log/anaconda/anaconda.log
/var/log/anaconda/X.log
/var/log/anaconda/program.log
/var/log/anaconda/packaging.log
/var/log/anaconda/storage.log
/var/log/anaconda/ifcfg.log

 
6. 在opt⽬录下创建test⽬录
[root@Linux ~]# mkdir /opt/test
[root@Linux ~]# ls /opt
a.txt  d0  hhhhh.txt  test  zzzzzzzz.txt

 
7. 在test⽬录中创建abc.txt,def.txt.ghi.txt,xxx.txt.yyy.txt五个⽂件
[root@Linux ~]# touch /opt/test/abc.txt 
[root@Linux ~]# touch /opt/test/def.txt 
[root@Linux ~]# touch /opt/test/ghi.txt 
[root@Linux ~]# touch /opt/test/xxx.txt 
[root@Linux ~]# touch /opt/test/yyy.txt 
[root@Linux ~]# ls /opt/test
abc.txt  def.txt  ghi.txt  xxx.txt  yyy.txt

 
8. 修改以上5个⽂件的最后修改时间分别为15,14,13,12,11,10⽇
[root@Linux ~]# touch -m -d "2024-7-15" /opt/test/abc.txt
[root@Linux ~]# touch -m -d "2024-7-14" /opt/test/def.txt
[root@Linux ~]# touch -m -d "2024-7-13" /opt/test/ghi.txt
[root@Linux ~]# touch -m -d "2024-7-12" /opt/test/xxx.txt
[root@Linux ~]# touch -m -d "2024-7-11" /opt/test/yyy.txt
[root@Linux ~]# ls -l /opt/test
总用量 0
-rw-r--r--. 1 root root 0 7月  15 00:00 abc.txt
-rw-r--r--. 1 root root 0 7月  14 00:00 def.txt
-rw-r--r--. 1 root root 0 7月  13 00:00 ghi.txt
-rw-r--r--. 1 root root 0 7月  12 00:00 xxx.txt
-rw-r--r--. 1 root root 0 7月  11 00:00 yyy.txt



 
9. 在test⽬录下创建a⽬录
[root@Linux ~]# mkdir /opt/test/a
[root@Linux ~]# ls /opt/test
a  abc.txt  def.txt  ghi.txt  xxx.txt  yyy.txt

 
10. 将以上5个⽂件复制⼀份到a⽬录中
[root@Linux ~]# cp /opt/test/abc.txt /opt/test/a/
[root@Linux ~]# cp /opt/test/def.txt /opt/test/a/
[root@Linux ~]# cp /opt/test/ghi.txt /opt/test/a/
[root@Linux ~]# cp /opt/test/xxx.txt /opt/test/a/
[root@Linux ~]# cp /opt/test/yyy.txt /opt/test/a/
[root@Linux ~]# ls -l /opt/test/a
总用量 0
-rw-r--r--. 1 root root 0 7月  15 19:15 abc.txt
-rw-r--r--. 1 root root 0 7月  15 19:15 def.txt
-rw-r--r--. 1 root root 0 7月  15 19:15 ghi.txt
-rw-r--r--. 1 root root 0 7月  15 19:15 xxx.txt
-rw-r--r--. 1 root root 0 7月  15 19:15 yyy.txt

 
11. 将a⽬录⽂件做成bak.tar.gz⽂件保存到家⽬录中
[root@Linux ~]# tar -czvf bak.tar.gz /opt/test/a/*
tar: 从成员名中删除开头的“/”
/opt/test/a/
/opt/test/a/abc.txt
/opt/test/a/def.txt
/opt/test/a/ghi.txt
/opt/test/a/xxx.txt
/opt/test/a/yyy.txt
[root@Linux ~]# ls 
bak.tar.gz  

 
12. 使⽤find删除test⽬录下3天前的⽂件
[root@Linux ~]# find /opt/test -type f -mtime +3
/opt/test/yyy.txt
[root@Linux ~]# find /opt/test -type f -mtime +3 -exec rm -rf {} \;
[root@Linux ~]# ls -l /opt/test
总用量 0
drwxr-xr-x. 2 root root 81 7月  15 19:15 a
-rw-r--r--. 1 root root  0 7月  15 00:00 abc.txt
-rw-r--r--. 1 root root  0 7月  14 00:00 def.txt
-rw-r--r--. 1 root root  0 7月  13 00:00 ghi.txt
-rw-r--r--. 1 root root  0 7月  12 00:00 xxx.txt
13. find删除opt⽬录下3天内的⽂件
[root@Linux ~]# find /opt -type f -mtime -3 
/opt/test/abc.txt
/opt/test/def.txt
/opt/test/ghi.txt
/opt/test/a/abc.txt
/opt/test/a/def.txt
/opt/test/a/ghi.txt
/opt/test/a/xxx.txt
/opt/test/a/yyy.txt
[root@Linux ~]# find /opt -type f -mtime -3 | xargs rm -rf
[root@Linux ~]# ls /opt
test
[root@Linux ~]# ls /opt/test
a  xxx.txt

 
14. find删除正好第三天的⽂件
[root@Linux ~]# find /opt -type f -mtime 3 
/opt/test/xxx.txt
[root@Linux ~]# find /opt -type f -mtime 3 | xargs rm -rf
[root@Linux ~]# ls /opt
test
[root@Linux ~]# ls /opt/test
a

 
15. 将/opt/test/a⽬录中的⽂件复制i⼀份到/opt/test/⽬录下
[root@Linux ~]# ls /opt/test/a
[root@Linux ~]# touch /opt/test/a/a.txt
[root@Linux ~]# ls /opt/test/a
a.txt
[root@Linux ~]# cp /opt/test/a/* /opt/test
[root@Linux ~]# ls /opt/test
a  a.txt

 
16. 创建⽬录/opt/test0
[root@Linux ~]# mkdir /opt/test0
[root@Linux ~]# ls /opt
test  test0

 
17. 在/opt/test0/⽬录中创建三个⽂件 a.mp4(5M),b.mp4(20M),c.mp4(80M)
[root@Linux ~]# dd if=/dev/zero of=/opt/test0/a.mp4 bs=5M count=1
记录了1+0 的读入
记录了1+0 的写出
5242880字节(5.2 MB)已复制,0.0283463 秒,185 MB/秒
[root@Linux ~]# dd if=/dev/zero of=/opt/test0/b.mp4 bs=20M count=1
记录了1+0 的读入
记录了1+0 的写出
20971520字节(21 MB)已复制,0.131677 秒,159 MB/秒
[root@Linux ~]# dd if=/dev/zero of=/opt/test0/c.mp4 bs=80M count=1
记录了1+0 的读入
记录了1+0 的写出
83886080字节(84 MB)已复制,0.301175 秒,279 MB/秒
您在 /var/spool/mail/root 中有新邮件

 
18. 创建⽬录/opt/test0/b/
[root@Linux ~]# mkdir /opt/test0/b/
[root@Linux ~]# ls /opt/test0
a.mp4  b  b.mp4  c.mp4

 
19. 将/op t/test0/中的⽂件复制⼀份/opt/test0/b/⽬录中
[root@Linux ~]# cp /opt/test0/*.mp4 /opt/test0/b/
[root@Linux ~]# ls /opt/test0/b
a.mp4  b.mp4  c.mp4

 
20. find查询/opt/test0/⽬录中⽂件⼤于20M的,并删除
[root@Linux ~]# ls /opt/test0
a.mp4  b  b.mp4  c.mp4
[root@Linux ~]# find /opt/test0 -type f -size +20M
/opt/test0/c.mp4
/opt/test0/b/c.mp4
[root@Linux ~]# find /opt/test0 -type f -size +20M | xargs rm -rf
[root@Linux ~]# ls /opt/test0
a.mp4  b  b.mp4

 
21. find查询/opt/test0/⽬录中⽂件⼩于20M的⽂件并删除
[root@Linux ~]# find /opt/test0 -type f -size -20M
/opt/test0/a.mp4
/opt/test0/b/a.mp4
您在 /var/spool/mail/root 中有新邮件
[root@Linux ~]# find /opt/test0 -type f -size -20M | xargs rm -rf
[root@Linux ~]# ls /opt/test0
b  b.mp4

 
22. find查找/opt/test0/⽬录中⽂件size为20M的⽂件并删除
[root@Linux ~]# ls /opt/test0
b  b.mp4
[root@Linux ~]# find /opt/test0 -type f -size 20M 
/opt/test0/b.mp4
/opt/test0/b/b.mp
[root@Linux ~]# find /opt/test0 -type f -size 20M | xargs rm -rf
[root@Linux ~]# ls /opt/test0
b

 
23. /opt/test0/b中的⽂件复制⼀份到/opt/test0中
[root@Linux ~]# ls /opt/test0/b
[root@Linux ~]# touch /opt/test0/b/d.mp4
[root@Linux ~]# ls /opt/test0/b
d.mp4
[root@Linux ~]# cp /opt/test0/b/*.mp4 /opt/test0
[root@Linux ~]# ls /opt/test0
b  d.mp4

 
24. 打开新的虚拟主机
25. 将家⽬录中的bak.tar.gz⽂件上传到新主机的/opt⽬录中
[root@Linux ~]# scp bak.tar.gz root@192.168.118.50:/opt
root@192.168.118.50's password: 
bak.tar.gz                            100%  184    78.2KB/s   00:00    
[root@linux1 ~]# ls /opt
a.txt  bak.tar.gz  d0  dd.txt


 
26. 将新主机的/e tc/skel/⽬录下载到 当前主机的/opt⽬录中
[root@Linux ~]# scp -r root@192.168.118.50:/etc/skel /opt
root@192.168.118.50's password: 
.bash_logout                          100%   18     7.0KB/s   00:00    
.bash_profile                         100%  193    65.3KB/s   00:00    
.bashrc                               100%  231    83.3KB/s   00:00    
notice.text                           100%   14     4.0KB/s   00:00  
[root@Linux ~]# ls /opt
skel  test  test0

 
27. 设置计划任务,每周3将/e tc/yum.repos.d/⽬录下的.repo⽂件压缩保存到tmp,在⽂件
名中添加时间戳
[root@Linux ~]# crontab -e
* * * * */3 /usr/bin/tar -czvf /tmp/repo_$(date "+\%Y\%m\%d\%H\%M\%S").tar.gz /etc/yum.repos.d/*.repo
[root@Linux ~]# ls /tmp

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值