实训日记day06

Linux操作命令和习题练习

1.查找文件路径

    find [文件路径]  [选项  选项的值]

    [root@1 ~]# find /opt/ -name "*a*" -type

    -type f:普通文件    d:目录

2.通配符

    *星号,匹配任何字符

   ? 问号,匹配任意一个字符

   []中括号,匹配括号中的一个字符

     *可以代替任意个数字符,?只能代替一个

    

3.按照时间查找

     find 文件路径 -时间 +days/-days

    +号 搜索几天之前的文件信息 -号 搜索几天之内的文件信息

    stat [文件路径]  [文件名]

    创建时间

    touch [文件路径][文件名]   -m -d  "时间”

    [root@1 ~]# touch /opt/b.txt -m -d "2024-7-11 00:00"

    [root@1 ~]# touch /opt/c.txt -m -d "2024-7-12 00:00"

    [root@1 ~]# ls -l /opt/

    -rw-r--r--. 1 root root       0 Jul 11 00:00 b.txt

    -rw-r--r--. 1 root root       0 Jul 12 00:00 c.txt

    修改时间

    touch -m -d   "时间"   文件路径

     touch -m -d "2020-7-7 00:00" /opt/c.txt

    访问时间

    stat 文件路径

     stat /opt/test.conf

    查找//删除几天前的文件

    find 文件路径 -mtime +天数

    find 文件路径 -mtime +天数 | xargs rm -rf  

    find 文件路径 -mtime +天数  -exec rm -rf  {}  \ ;

4.根据内存查找文件

    find  文件目录  -size  文件大小

    [root@1 ~]# find /opt/ -size +5M

    /opt/apache-maven-3.9.8-bin.tar.gz

5.生产指定大小的文件

    dd  if=/dev/zero of=文件路径 bs=文件大小 count=块

    [root@1 ~]# dd if=/dev/zero of=/opt/a.txt bs=1M count=1

    1+0 records in

    1+0 records out

    1048576 bytes (1.0 MB) copied, 0.0021093 s, 497 MB/s

    [root@1 ~]# ls -l /opt/

    -rw-r--r--. 1 root root 1048576 Jul 15 10:28 a.txt

6.tree指令

tree  目录

7.下载

    scp [选项]   旧机器用户名@linux主机地址:/资源路径 新机器linux本地文件路径

    scp   root@192.168.2.11:/opt/a.txt /opt/

    -r 代表递归,主要作用文件夹

8.计划任务

    crontab  [选项]

     -l list查看当前用户的计划任务信息

     -e edit编写计划任务

     crontab 分时日月周 要使用的完整路径 which命令

    0和7都表示周日

    [root@1 ~]# crontab -e

    [root@1 ~]# crontab -l

    */1 * * * * /usr/bin/ls /opt/ >> /root/list

    [root@1 ~]# new crontab

    [root@1 ~]# touch /opt/b.txt

    [root@1 ~]# cat list

    */1 * * * * /usr/bin/ls /opt/ >> /root/list

    b.txt

    输出时间

     [root@localhost ~]# date "+%T" 

     17:24:56

    输出日期和时间 

    [root@localhost ~]# date "+%F%T" 

    2024-07-1417:25:03

    输出年月日时分秒

    [root@localhost ~]# date "+%Y%m%d%H%M%S" 

    20240714172653

分,时,日,月,周 * * * *  *

    data "+%F"  (%F 分%Y 年%m 月 %d 日)

习题练习

练习:

1. 使⽤ls查看/etc/⽬录下所有的⽂件信息

[root@1 ~]# ls -l /opt/

total 112308

drwxr-xr-x. 6 root root        99 Jul  8 16:32 apache-maven-3.9.8

2. 使⽤ls查看/etc/⽬录下名包含“a”字⺟的⽂件或者⽬录信息 

[root@1 ~]# ls -la /etc/ | grep a

total 1120

-rw-r--r--.  1 root root       16 Jul  8 12:40 adjtime......

3. 使⽤ls查看/etc/⽬录下以".conf"结尾的⽂件信息 

[root@1 ~]# ls -l /etc/*.conf

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

[root@1 ~]# ls -la /etc/y*

5. find查找/var/⽬录中以“.log”⽂件 

[root@1 ~]# find /var/ -name "*.log" -type f

6. 在opt⽬录下创建test⽬录 

[root@1 ~]# mkdir /opt/test

7. 在test⽬录中创建abc.txt,def.txt.ghi.txt,xxx.txt.yyy.txt五个⽂件

[root@1 ~]# touch /opt/test/abc.txt /opt/test/def.txt

[root@1 ~]# touch /opt/test/xxx.txt /opt/test/ghi.txt /opt/test/yyy.txt

8. 修改以上5个⽂件的最后修改时间分别为15,14,13,12,11,10⽇

[root@1 ~]# touch -m -d "2024-7-15" /opt/test/abc.txt

[root@1 ~]# touch -m -d "2024-7-14" /opt/test/def.txt

[root@1 ~]# touch -m -d "2024-7-13" /opt/test/ghi.txt

[root@1 ~]# touch -m -d "2024-7-12" /opt/test/xxx.txt

[root@1 ~]# touch -m -d "2024-7-11" /opt/test/yyy.txt

9. 在test⽬录下创建a⽬录 

[root@1 ~]# mkdir /opt/test/a

10. 将以上5个⽂件复制⼀份到a⽬录中 

[root@1 ~]# cp /opt/test/* /opt/test/a

11. 将a⽬录⽂件做成bak.tar.gz⽂件保存到家⽬录中 

[root@1 ~]# tar -zcvf /home/bak.tar.gz /opt/test/a

12. 使⽤find删除test⽬录下3天前的⽂件 

[root@1 ~]# find /opt/test/ -mtime +3 -exec rm -rf  {}  \ ;

13. find删除opt⽬录下3天内的⽂件 

[root@1 ~]# find /opt/test/ -mtime -3 | xargs rm -rf

14. find删除正好第三天的⽂件 

[root@1 ~]#find /opt/test/ -mtime 3 -exec rm -rf {} \;

15. 将/opt/test/a⽬录中的⽂件复制⼀份到/opt/test/⽬录下 

[root@1 ~]# cp /opt/test/a/* /opt/test/

16. 创建⽬录/opt/test0 

[root@1 ~]# mkdir /opt/test0

17. 在/opt/test0/⽬录中创建三个⽂件 a.mp4(5M),b.mp4(20M),c.mp4(80M) 

[root@1 ~]# dd if=/dev/zero of=/opt/test0/a.mp4 bs=5M count=1

[root@1 ~]# dd if=/dev/zero of=/opt/test0/b.mp4 bs=20M count=1

[root@1 ~]# dd if=/dev/zero of=/opt/test0/c.mp4 bs=80M count=1

18. 创建⽬录/opt/test0/b/ 

[root@1 ~]# mkdir /opt/test0/b

19. 将/op t/test0/中的⽂件复制⼀份/opt/test0/b/⽬录中 

[root@1 ~]# cp /opt/test0/* /opt/test0/b

20. find查询/opt/test0/⽬录中⽂件⼤于20M的,并删除 

[root@1 ~]# find /opt/test0 -size +20M -exec rm -rf {} \;

21. find查询/opt/test0/⽬录中⽂件⼩于20M的⽂件并删除 

[root@1 ~]# find /opt/test0 -size -20M -exec rm -rf {} \;

22. find查找/opt/test0/⽬录中⽂件size为20M的⽂件并删除 

[root@1 ~]# find /opt/test0 -size 20M -exec rm -rf {} \;

23. /opt/test0/b中的⽂件复制⼀份到/opt/test0中 

[root@1 ~]# cp /opt/test0/b/* /opt/test0

24. 打开新的虚拟主机 

25. 将家⽬录中的bak.tar.gz⽂件上传到新主机的/opt⽬录中 

[root@1 ~]# scp root@192.168.2.11:/opt /home/bak.tar.gz

26. 将新主机的/e tc/skel/⽬录下载到 当前主机的/opt⽬录中 

[root@1 ~]# scp root@192.168.2.11:/etc/skel/  /opt/

27. 设置计划任务,每周3将/e tc/yum.repos.d/⽬录下的.repo⽂件压缩保存到tmp,在⽂件 名中添加时间戳

[root@1 ~]# crontab -e

0 * * * 3 tar -zcvf /tmp/yum_repos_$(date "+\%Y\%m\%d\%H\%M\%S").tar.gz /etc/yum.repos.d/*.repo

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值