Linux中文件的查找、上传文件、计划任务

1 . Find命令:主要进行文件搜索

基本语法:find  [文件路径]  [选项    选项的值]

常见选项:-name  根据文件的名称搜索文件,支持通配符 *

                  -type    文件类型 (f  代表普通文件  , d代表目录)

例:[root@es_001 ~]# find / -name "httpd.conf" -type f   

 #  从 \ (根) 文件下找到httpd.conf文件

/etc/httpd/conf/httpd.conf

/usr/lib/tmpfiles.d/httpd.conf

[root@es_001 ~]# find  /etc  -name  "httpd.conf"  -type  f

将根 \ 目录范围换成 /etc目录的范围 ,这样查找更快,更加节省资源

/etc/httpd/conf/httpd.conf

* 通配符:在Linux系统中,若要查找的文件名称不清晰,可以使用部分文件名+“ * ” 搜索

[root@es_001 ~]# find /etc/ -name "*.conf" -type f        # 获取 /etc 中以  . conf结尾的文件

[root@es_001 ~]# find /etc/ -name "http*" -type f         # 搜索以http开头的文件

/etc/sysconfig/httpd

/etc/logrotate.d/httpd

/etc/httpd/conf/httpd.conf

2 . scp 上传文件

基本语法:scp     [选项]  本地主机资源路径    [远程主机] 用户名@主机IP:放置路径

[root@es_001 ~]# rm -rf /opt/*

[root@es_001 ~]# ls /opt/

[root@es_001 ~]# ssh  -lroot  -p22  192.168.119.55             

The authenticity of host '192.168.119.55 (192.168.119.55)' can't be established.

Are you sure you want to continue connecting (yes/no)? yes

root@192.168.119.55's password:

Last login: Mon Jul 15 14:16:14 2024 from 192.168.119.1

[root@es_005 ~]# ifconfig | head -2

ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500

        inet 192.168.119.55  netmask 255.255.255.0  broadcast 192.168.119.255

[root@es_005 ~]# ls /opt/

b.txt    c.txt

[root@es_005 ~]# scp   /opt/c.txt   root@192.168.119.11:/opt/    

# 上传文件,将es_005中的a.txt文件上传到es_001中

The authenticity of host '192.168.119.11 (192.168.119.11)' can't be established.

Are you sure you want to continue connecting (yes/no)? yes

root@192.168.119.11's password:

c.txt                       100%    0     0.0KB/s   00:00   

[root@es_005 ~]# exit

登出

Connection to 192.168.119.55 closed.

[root@es_001 ~]# ls /opt/

c.txt                # 上传成功

要求必须启用ssh服务:systemctl  start  sshd

3 . 计划任务:crontab  [选项]

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

-e   edit编写计划任务

-r    删除该用户的计时器设置

-u<用户名称>    指定要设定计时器的用户名称

Crontab命令:设置计时器

基本语法:crontab [-u  <用户名称> ] [-e  or  r]

                  crontab [-u  <用户名称> ] [配置文件]

说明:cron是一个常驻服务,它提供计算器的功能,让用户在特定的时间得以执行预设的指令或程序。

格式:Minute   Hour   Day   Month   DayOfWeek   Command

基本格式:* * * * *   Command

第1列表示分钟1~59 :每分钟用 * 或者 * / 1表示

第2列表示小时1~23(0表示0点)

第3列表示日期 1~31

第4列表示月份 1~12

第5列表示星期 0~6(0表示星期天)

[root@es_001 ~]# crontab -l

no crontab for root                  # 没有计划或任务

whereis命令用于定位执行文件、源码文件、帮助文档的位置,方便用户进行管理和使用

格式:whereis命令  [选项]   文件名

[root@es_001 ~]# whereis   tar

tar: /usr/bin/tar /usr/share/man/man1/tar.1.gz

which命令在Linux中用于查找可执行文件的路径。它会在系统的路径环境变量中搜索指定的命令,并返回找到的第一个匹配项的路径。

[root@es_001 ~]# which   tar

/usr/bin/tar

[root@es_001 ~]# crontab -e                 # 编辑计划任务

crontab: installing new crontab

[root@es_001 ~]# crontab -l

*/1 * * * *   /usr/bin/tar   -zcvf   /tmp/etc.tar.gz   /etc/        

 # 每分钟将 /etc 目录下的文件打包保存到tmp目录下

[root@es_001 ~]# ls /tmp                 # 等待一分钟后再查看,发现/tmp目录下多出包

etc.tar.gz

以上的任务会覆盖前面的文件,需要在指令中添加时间因素

[root@es_001 ~]# crontab -e

crontab: installing new crontab

您在 /var/spool/mail/root 中有新邮件

[root@es_001 ~]# crontab -l

*/1 * * * * /usr/bin/tar -zcvf /tmp/etc.tar.gz /etc/

*/1 * * * * /usr/bin/echo "我是回声" >> /root/echo.txt

*/1 * * * * /usr/bin/tar -zcvf /tmp/etc-$(date"+\ %Y \ %m \ %d \ %H \ %M \ %S ).tar.gz /etc/

# 在脚本中出现 % 必须使用转义符 \

[root@es_001 ~]# ls -l /tmp/

总用量 16320

-rw-r--r--.     1    root    root              9828345    7    月          15    17:06    etc.tar.gz

drwx------. 3 root root      17 7月  15 15:25 systemd-private-abedb3aa94f643d0b1f5e66f6e0cf637-chronyd.service-9YG2TS

drwx------. 3 root root      17 7月  15 15:25 systemd-private-

drwx------. 2 root root       6 7月  15 15:25 vmware-root

 [root@es_001 ~]# ls /root

4 . 时间戳

[root@es_005 ~]# date

2024年 07月 15日 星期一 14:42:45 CST

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

14:43:03

[root@es_005 ~]# date "+%F"

2024-07-15

[root@es_005 ~]# date "+%Y"

2024

[root@es_005 ~]# date "+%Y%m%d"

20240715

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

20240715144551

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

2024-07-1517:13:28

您在 /var/spool/mail/root 中有新邮件

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

2024-07-15-17:13:42

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

2024-07-15 17:13:59

[root@es_001 ~]# date "+%F_%T"

2024-07-15_17:14:12

Scp实现Linux系统和Linux系统之间的文件传输

5 . 使用find按文件大小搜索,找到旧的文件删掉

[root@es_001 ~]# find /root/ -size +100M -exec rm -rf {} \;

[root@es_001 ~]# find /root -size +100M

find   文件路径  -size  size值 (单位M 、G 、 k)

Size值        搜索等于size大小的文件

-size值       [ 0,size ]

+size值      (size值,无穷大)

[root@es_001 ~]# ls -lh                  # 普通单位(M 、G 、 k )查看文件信息

总用量 301M

-rw-------. 1 root root 1.3K 4月  24 17:38 anaconda-ks.cfg

-rw-r--r--. 1 root root  221 7月  15 17:22 echo.txt

-rw-r--r--. 1 root root 301M 4月  25 09:40 elasticsearch-7.17.8-x86_64.rpm

drwxr-xr-x. 5 root root  182 12月 15 2020 es-head

-rw-r--r--. 1 root root 405K 4月  26 08:40 head.tar.gz

[root@es_001 ~]# find ./ -size 301M                # 搜索文件大小为301M的文件

./elasticsearch-7.17.8-x86_64.rpm

[root@es_001 ~]# find ./ -size 405k

./head.tar.gz

[root@es_001 ~]# find ./ -size +1M                  # 搜索文件大小大于1M的文件

./elasticsearch-7.17.8-x86_64.rpm

[root@es_001 ~]# find ./ -size -1M

6 . 创建文件列表,将文件名以树的形式展示

[root@es_001 ~]# yum -y install tree

[root@es_001 ~]# tree /var/log/                 # 以树状结构显示  /var/ lib目录中的文件

7 . 扩展命令dd

使用dd创建扩展命令,生成指定大小的测试文件

语法:dd   if = / dev /zero   of = 文件名称   bs = 1M   count = 1

1If表示输入文件               2of 表示输出文件           3bs 代表字节为单位的块大小         

4count代表被复制的块

其中/dev/zore是一个字符设备,会不断地返回0字节地文件

[root@es_005 ~]# cd /opt/

[root@es_005 opt]# ls

b.txt  c.txt

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

 # 创建名称为a.txt,大小为1M的文件

记录了1+0 的读入

记录了1+0 的写出

1048576字节(1.0 MB)已复制,0.00836676 秒,125 MB/秒

[root@es_005 opt]# ls -l

总用量 1024

-rw-r--r--. 1 root root 1048576 7月  15 18:48 a.txt

-rw-r--r--. 1 root root       0 7月  14 00:00 b.txt

-rw-r--r--. 1 root root       0 7月  13 00:00 c.txt

[root@es_005 opt]# ls -lh

总用量 1.0M

-rw-r--r--. 1 root root 1.0M 7月  15 18:48 a.txt

-rw-r--r--. 1 root root    0 7月  14 00:00 b.txt

-rw-r--r--. 1 root root    0 7月  13 00:00 c.txt

8 . find的exec选项:

删除系统 /var/log 10天之前的日志,格式都是 . log文件

[root@es_005 opt]# find /opt/ -name "*.txt" -type f -mtime +3

[root@es_005 opt]# find /opt/ -name "*.txt" -type f -mtime +1

/opt/c.txt

[root@es_005 opt]# find /opt/ -name "*.txt" -type f -mtime +1 | rm -rf      

[root@es_005 opt]# ls -l *.txt         

 # 查看发现c . txt 并没有被删除,这是因为rm 与ls不支持管道

-rw-r--r--. 1 root root 1048576 7月  15 18:48 a.txt

-rw-r--r--. 1 root root       0 7月  14 00:00 b.txt

-rw-r--r--. 1 root root       0 7月  13 00:00 c.txt

[root@es_005 opt]# find /opt/ -name "*.txt" -type f -mtime +1 | xargs rm -rf

# 使用xargs将查询结果交给rm,即可删除

[root@es_005 opt]# ls -l *.txt

-rw-r--r--. 1 root root 1048576 7月  15 18:48 a.txt

-rw-r--r--. 1 root root       0 7月  14 00:00 b.txt

[root@es_005 opt]# touch -m -d "2024-7-10 00:00" e.txt     

 # 创建文件并且指定文件修改日期

[root@es_005 opt]# ls -l *.txt

-rw-r--r--. 1 root root 1048576 7月  15 18:48   a.txt

-rw-r--r--. 1 root root       0 7月  14 00:00 b.txt

-rw-r--r--. 1 root root       0 7月  10 00:00 e.txt

[root@es_005 opt]# find /opt/ -name "*.txt" -type f -mtime +3         # 查找三天以前的文件

/opt/e.txt

[root@es_005 opt]# find /opt/ -name "*.txt" -type f -mtime +3 -exec rm -rf  { }  \ ;

# 使用-exec文件调用rm函数,{ }表示前面find查到的内容,\ ; 表示标识符

9 . 通过文件的最后修改时间搜索文件:find   文件路径   -mtime   +days / -days

-mtime 根据文件最后修改时间搜索文件

+号:搜索几天之的文件信息

-号:搜索几天之的文件信息

[root@es_005 opt]# ls

a.txt  b.txt

[root@es_005 opt]# touch tuan.txt -m -d "2024-7-13 00:00"

[root@es_005 opt]# touch summer.txt -m -d "2024-7-12 00:00"

[root@es_005 opt]# touch winter.txt -m -d "2024-7-11 00:00"

[root@es_005 opt]# ls -l

总用量 1024

-rw-r--r--. 1 root root 1048576 7月  15 18:48  a.txt

-rw-r--r--. 1 root root       0 7月  14 00:00 b.txt

-rw-r--r--. 1 root root       0 7月  12 00:00 summer.txt

-rw-r--r--. 1 root root       0 7月  13 00:00 tuan.txt

-rw-r--r--. 1 root root       0 7月  11 00:00 winter.txt

[root@es_005 opt]# find /opt/ -name "*.txt" -type f -mtime +3

/opt/winter.txt

[root@es_005 opt]# find /opt/ -name "*.txt" -type f -mtime -3      

 # 搜索三天以内的文件,包含今天(7.15)

/opt/b.txt

/opt/a.txt

/opt/tuan.txt

10 . 文件中时间的概念:创建时间、修改时间、访问时间

使用stat命令获取文件的时间信息:stat   文件

[root@es_005 opt]# stat /opt/summer.txt

  文件:"/opt/summer.txt"

  大小:0            块:0          IO 块:4096   普通空文件

设备:fd00h/64768d   Inode:34759629    硬链接:1

权限:(0644/-rw-r--r--)  Uid:(    0/    root)   Gid:(    0/    root)

环境:unconfined_u:object_r:usr_t:s0

最近访问:2024-07-15 19:27:24.126498435 +0800

最近更改:2024-07-12 00:00:00.000000000 +0800

最近改动:2024-07-15 19:27:24.126498435 +0800

创建时间:-

创建文件,并配置文件的修改时间:touch   -m   -d    日期时间格式    文件名称

(文件不存在就创建并修改时间,文件已存在只配置最后修改时间)

[root@es_005 opt]# touch qus.txt -m -d "2024-7-5 00:00"

[root@es_005 opt]# stat /opt/qus.txt

练习

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

[root@web2 ~]# ls /etc/*

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

[root@web2 ~]# ls /etc -name *a

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

[root@web2 ~]#ls /etc -name ".conf" -type f

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

[root@web2 ~]#ls /etc -name "y*" -type f

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

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

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

[root@web2 ~]# mkdir -p /opt/test

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

[root@web2 ~]# touch /opt/test/abc.txt /opt/test/def.txt /opt/test/ghi.txt /opt/test/xxx.txt /opt/test/yyy.txt

[root@web2 ~]# ls -l /opt/test/

-rw-r--r--. 1 root root 0 7月  15 22:06 abc.txt

drwxr-xr-x. 2 root root 6 7月  15 18:59 d0

-rw-r--r--. 1 root root 0 7月  15 22:06 def.txt

-rw-r--r--. 1 root root 0 7月  15 22:06 ghi.txt

drwxr-xr-x. 2 root root 6 7月  15 22:05 test

-rw-r--r--. 1 root root 0 7月  15 22:06 xxx.txt

-rw-r--r--. 1 root root 0 7月  15 22:06 yyy.txt

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

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

您在 /var/spool/mail/root 中有新邮件

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

[root@web2 ~]# touch /opt/def.txt -m -d "2024-7-14 00:00"

[root@web2 ~]# touch /opt/ghi.txt -m -d "2024-7-13 00:00"

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

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

[root@web2 ~]# ls -l /opt/test/

-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@web2 ~]# mkdir -p /opt/test/a

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

[root@web2 ~]# cp -r /opt/test/* /opt/test/a

[root@web2 ~]# ls /opt/test/a

abc.txt  def.txt  ghi.txt  xxx.txt  yyy.txt

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

[root@web2 ~]# pwd

/root

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

[root@web2 ~]# ls /home

anaconda-ks.cfg  bak.tar.gz  echo.txt  list  minute.txt

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

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

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

[root@mj ~]# touch /opt/m.txt /opt/n.txt

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

-rw-r--r--. 1 root root 13022098 7月   8 14:27 apache-tomcat-10.1.25.tar.gz

-rw-r--r--. 1 root root      267 7月   8 19:00 application.perperties

-rw-r--r--. 1 root root      101 7月   8 22:10 Main.java

-rw-r--r--. 1 root root        0 7月  16 00:41 m.txt

-rw-r--r--. 1 root root        0 7月  16 00:41 n.txt

[root@mj ~]# find /opt/ -mtime -3 -type f -exec rm -rf {} \;

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

总用量 12728

drwxr-xr-x. 2 root root        6 7月  16 00:41 123.txt

-rw-r--r--. 1 root root 13022098 7月   8 14:27 apache-tomcat-10.1.25.tar.gz

-rw-r--r--. 1 root root      267 7月   8 19:00 application.perperties

-rw-r--r--. 1 root root      101 7月   8 22:10 Main.java

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

[root@mj ~]# find /opt -time 3 -type f -exec rm -rf {} \;

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

[root@web2 ~]# cp -r /opt/test/a/* /opt/test/

16. 创建⽬录/opt/test0

[root@web2 ~]# mkdir -p /opt/test0

[root@web2 ~]# ls /opt

 test0 

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

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

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

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

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

[root@web2 ~]# cd /opt/test0

[root@web2 test0]# pwd

/opt/test0

[root@web2 test0]# mkdir b

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

[root@web2 test0]# ls

mp4  b  b.mp4  c.mp4

[root@web2 test0]# cp -r ./* /opt/test0/b

[root@web2 test0]# ls /opt/test0/b

a.mp4  b  b.mp4  c.mp4

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

[root@web2 test0]# ls

a.mp4  b  b.mp4  c.mp4

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

[root@web2 test0]# ls /opt/test0

a.mp4  b

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

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

[root@web2 test0]# ls -l /opt/test0

drwxr-xr-x. 3 root root 15 7月  15 23:15 b

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

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

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

[root@web2 test0]# cp -r /opt/test0/b/* /opt/test0/

[root@web2 test0]# ls /opt/test0

b  test0

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

[root@web2 ~]#scp /home/bak.tar.gz root@192.168.2.12:/opt/

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

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

27. 设置计划任务,每周3将/etc/yum.repos.d/⽬录下的.repo⽂件压缩保存到tmp,在⽂件

名中添加时间戳

[root@web2 ~]# whereis tar

tar: /usr/bin/tar /usr/include/tar.h /usr/share/man/man1/tar.1.gz

[root@web2 ~]# crontab -e

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

                         

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值