linux 高级管理(第六天)

一、find的命令

1、find命令的通

主要进行文件搜索

2、基本语

find【文件路径】 【选项  选项的值】

-name*

type f/d

常见的选项

-name 根据文件的名称搜索文件,支持通配符*

-type f 代表普通文件,d代表目录

案例,找到httpd.conf的文件

         [root@oneday ~]# yum -y install httpd    #安装httpd服务

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

           /etc/httpd/conf/httpd.conf

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

3、*通配符

在Linux系统中,如果要查找的文件的名称不清晰,可以使用部分文件+*搜索

案例 获取/etc/中以.conf 结尾的文件

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

案例 搜索以http开头的文件

[root@oneday ~]# find /etc/ -name "http*" -type f

二、创建并设置文件最后修改的时间

1.文件的时间的概念

window中的时间

(1)创建时间

(2)修改时间

(3)访问时间

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

[root@oneday ~]#  stat /opt/test.conf   (语法 stat 文件)

      文件: "/opt/test.conf"

      大小: 23

      块: 8 IO 块: 4096 普通文件

      设备: fd00h/64768d

      Inode : 34362655 硬链接: 1

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

      环境: unconfined_u:object_r:usr_t:s0

      最近访问: 2024-07-14 13:28:50.938662360 +0800

      最近更改: 2024-07-14 13:28:50.938662360 +0800

      最近改动: 2024-07-14 13:28:50.976662431 +080

案例:

      创建文件,并 p 配置文件的修改时间

      语法 touch -m -d 日期时间格式 文件名称

        [root@oneday ~]#  touch -m -d "2020-7-7 00:00" /opt/abc.txt

          文件不存在就创建并修改时间

          文件存在只配置最后修改时间

三、 根据文件的最后修改时间搜索文件

1、语法

find 文件路径 -mtime +days/-days

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

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

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

案例:搜索前3天以前的信息,不包含第三个,而且只搜txt文件

     [root@oneday ~]# find /opt/ -name "*.txt" -mtime +3/opt/e.txt

 四、find的exec选项

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

(1)报错,rm不支持这种写法

(2)rm和ls不支持管道

(3)使用xargs将查询结果交给rm,可行

[root@oneday ~]# find /opt/ -name "*.txt" -type f -mtime +3 xargs rm -rf

(4)使用find执行 -exec

语法find文件路径

# 查看目录中的 txt 文件

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

# 没有 e.txt 文件,在前面被删除了

-rw-r--r--. 1 root root 0 7 月 14 13:54 a.txt

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

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

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

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

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

[root@localhost opt]# ls -l

总用量 0

-rw-r--r--. 1 root root 0 7 月 14 13:54 a.txt

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

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

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

# 查找三天以前的文件

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

/opt/e.txt

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

# 这里在 {} 后面没有打空格报错了,在 {} 后应该打空格

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

rf {}\;

find: 遗漏 “-exec” 的参数

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

rf {} \;

总用量 0

-rw-r--r--. 1 root root 0 7 月 14 13:54 a.txt

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

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

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

五、用于快速生成指定大小的文件

1、根据文件size大小搜索文件

find 路径 -size 文件大小 [ 常用单位 k M G]

size 值 搜索等于 size 的文件

-size 值 【 0 , size 值 )

+size 值 ( size 值,正无穷)

扩展命令 dd

使用 dd 创建扩展命令

生成指定大小的测试文件

2、语法

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

if 表示输入文件

of 表示输出文件

bs 代表字节为单位的块大小

count 代表被复制的块

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

案例创建一个 1m 的文件

# 查看文件

[root@localhost opt]# ls

a.txt b.txt c.txt d.txt

# 删除文件

[root@localhost opt]# rm -rf *

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

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

记录了 1+0 的读入

记录了 1+0 的写出

1048576 字节 (1.0 MB) 已复制, 0.0027841 秒, 377 MB/ 秒

# 查看文件信息,使用单位字节

[root@localhost opt]# ls -l

总用量 1024 -rw-r--r--. 1 root root 1048576 7 月 14 14:37 a.txt

# 查看文件信息,使用文件大小单位 默认 m

[root@localhost opt]# ls -lh

总用量 1.0M

-rw-r--r--. 1 root root 1.0M 7 月 14 14:37 a.txt

生成其他大小的文件

记录了 1+0 的读入

记录了 1+0 的写出

5242880 字节 (5.2 MB) 已复制, 0.0111468 秒, 470 MB/ 秒

[root@localhost opt]# dd if=/dev/zero of=c.txt bs=10M count=1

记录了 1+0 的读入

记录了 1+0 的写出

10485760 字节 (10 MB) 已复制, 0.0476839 秒, 220 MB/ 秒

[root@localhost opt]# ls -l

总用量 16384

-rw-r--r--. 1 root root 1048576 7 月 14 14:37 a.txt

-rw-r--r--. 1 root root 5242880 7 月 14 14:42 b.txt

-rw-r--r--. 1 root root 10485760 7 月 14 14:42 c.txt

[root@localhost opt]# ls -lh

总用量 16M

-rw-r--r--. 1 root root 1.0M 7 月 14 14:37 a.txt

-rw-r--r--. 1 root root 5.0M 7 月 14 14:42 b.txt

-rw-r--r--. 1 root root 10M 7 月 14 14:42 c.txt

六、根据文件大小搜索文件

1、基本语法

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

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

-size 值 [0,size 值 )

+size 值 ( size 值 , 无穷大)

# 普通单位查看文件信息

[root@localhost opt]# ls -lh

总用量 16M

-rw-r--r--. 1 root root 1.0M 7 月 14 14:37 a.txt

-rw-r--r--. 1 root root 5.0M 7 月 14 14:42 b.txt

-rw-r--r--. 1 root root 10M 7 月 14 14:42 c.txt

# 搜索文件大小为 5M 的文件

[root@localhost opt]# find ./ -size 5M

./b.txt

# 搜索文件大小小于 10M 的文件

[root@localhost opt]# find ./ -size 10M

./c.txt

# 搜索小于 1m 的文件,隐藏文件也找出来了

[root@localhost opt]# find ./ -size 1M

./

./a.txt

# 搜索文件大小大于 1m 的文件

[root@localhost opt]# find ./ -size +1M

./b.txt

./c.txt

[root@localhost opt]# find ./ -size -1M

案例 搜索系统大于100M的文件  

[root@localhost opt]# find / -size +100M

/proc/kcore

find: ‘/proc/9352/task/9352/fd/6’: 没有那个文件或目录

find: ‘/proc/9352/task/9352/fdinfo/6’: 没有那个文件或目录

find: ‘/proc/9352/fd/5’: 没有那个文件或目录

find: ‘/proc/9352/fdinfo/5’: 没有那个文件或目录

/sys/devices/pci0000:00/0000:00:0f.0/resource1_wc

/sys/devices/pci0000:00/0000:00:0f.0/resource1

/root/mysql-8.4.1-1.el7.aarch64.rpm-bundle.tar

/root/mysql-community-debuginfo-8.4.1-1.el7.aarch64.rpm

/root/mysql-community-test-8.4.1-1.el7.aarch64.rpm /var/cache/yum/x86_64/7/updates/gen/primary_db.sqlite

/usr/lib/locale/locale-archive

删除 root 目录下文件大大于 100m 的文件

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

[root@localhost opt]# find / -size +100M

/proc/kcore

find: ‘/proc/9440/task/9440/fd/6’: 没有那个文件或目录

find: ‘/proc/9440/task/9440/fdinfo/6’: 没有那个文件或目录

find: ‘/proc/9440/fd/5’: 没有那个文件或目录

find: ‘/proc/9440/fdinfo/5’: 没有那个文件或目录

/sys/devices/pci0000:00/0000:00:0f.0/resource1_wc

/sys/devices/pci0000:00/0000:00:0f.0/resource1

/var/cache/yum/x86_64/7/updates/gen/primary_db.sqlite

/usr/lib/locale/locale-archive

 七、tree指令

[root@localhost~]# yum -y install tree  #安装书协议包

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

八、计算机克隆操作

scp 实现 linux 系统和 linux 之间的克隆操作

实现 linux 和 linux 之间的文件传输

需要两个 linux

克隆操作 使用克隆快速生成

九、scp

scp 要求两台主机的系统都是 linux 系统

1. 使用 scp 下载文件和目录

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

复制文件

(1)查看克隆机的 ip 地址,并且清空 opt 目录中的文件

(2)查看原主机的 ip 地址,并且查看 opt 目录中的数据

(3)从原主机上下载 /opt/a.txt 到克隆机上的 /opt 目录,注意如果有询问,输入 yes 再输入密码即可

复制目录

(1)源主机 opt 目录下创建目录,并且将 a.txt b.txt c.txt 复制一份在新目录中

(2)在克隆机上使用 scp 指令复制目录到本地 opt 目录,需要添加 -r 选项,无法执行

第二次连接主机,不需要再次输入 yes

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

十、scp上传文件

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

上传文件,将克隆机中的a.txt 文件上传到源主机中

# 以下操作都是在原主机 192.168.135.129 中执行的

# 清空 opt 目录中的文件

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

[root@localhost ~]# ls /opt/

#ssh 管理克隆机

[root@localhost ~]# ssh -lroot -p22 192.168.1.15

The authenticity of host '192.168.1.15 (192.168.1.15)' can't be

established.

ECDSA key fingerprint is SHA256:CkKRXsYIVPxBU2aCwVy42OZPQpcOnsPp4lK0qesv0is.

ECDSA key fingerprint is

MD5:cb:e1:2c:97:ca:f1:54:7a:e6:c2:d1:22:32:41:04:c8.

# 第一次连接需要确认输入 yes

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

Warning: Permanently added '192.168.1.15' (ECDSA) to the list of known

hosts.

# 输入密码

root@192.168.1.15's password:

Last login: Sun Jul 14 15:58:47 2024

#---------------------------------------------------------------

# 现在是用源主机登录克隆机进行操作

# 查看 ip 地址

[root@localhost ~]# ifconfig ens33

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

inet 192.168.1.15 netmask 255.255.255.0 broadcast

192.168.1.255

inet6 fe80::5ed:b2b5:75a9:d491 prefixlen 64 scopeid 0x20<link>

inet6 fe80::aa7b:35ff:81db:2de7 prefixlen 64 scopeid 0x20<link>

ether 00:0c:29:10:ac:cc txqueuelen 1000 (Ethernet)

RX packets 14518 bytes 20940364 (19.9 MiB)

RX errors 0 dropped 0 overruns 0 frame 0

TX packets 1346 bytes 123880 (120.9 KiB)

TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0

# 将克隆机上的 a.txt 上传到 源主机上

[root@localhost ~]# scp /opt/a.txt root@192.168.1.25:/opt/

root@192.168.1.25's password:

a.txt

100% 1024KB 16.1MB/s 00:00

# 退出 ssh 连接

[root@localhost ~]# exit

登出 # --------------------------------------------------------

Connection to 192.168.1.15 closed.

# 查看源主机中是否已经存在 a.txt 文件

[root@localhost ~]# ls /opt/

a.txt # 上传成功

[root@localhost ~]#

上传目录,把克隆机中的 folder 目录上传到源主机的 opt 目录

# 再次使用 ssh 管理克隆机

[root@localhost ~]# ssh -lroot -p22 192.168.1.15

root@192.168.1.15's password:

Last login: Sun Jul 14 16:23:48 2024 from 192.168.1.15

# 使用 scp 上传目录到源主机上,第二次连接直接输入密码,不需要输入账号

[root@localhost ~]# scp -r /opt/folder/ root@192.168.1.25:/opt/

root@192.168.1.25's password:

a.txt

100% 1024KB 12.9MB/s 00:00

b.txt

100% 5120KB 30.0MB/s 00:00

c.txt

100% 10MB 70.8MB/s 00:00

# 退出 ssh 连接

[root@localhost ~]# exit

登出

Connection to 192.168.1.15 closed.

# 查看上传结果

[root@localhost ~]# ls /opt/

a.txt folde

要求必须启用 ssh 服务

systemctl start sshd

systemctl stop sshd

十一、计划任务和tar结合实现文件备份

1、计划任务

crontab 【选项】

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

-e edit 编写计划任务

2、编写任务

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

0和7表示星期日

[root@localhost ~]# whereis tar

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

[root@localhost ~]# which tar /usr/bin/tar

(1)找到tar指令的which位置

 

(2)编辑计划任务

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

(4)在脚本中出现%,必须使用转义符\

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

/etc

(5)查看备份文件

显示:

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

时间:

# 输出时间

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

17:24:56

# 输出日期和时间

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

2024-07-1417:25:03

# 在日期和时间中添加间隔

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

2024-07-14-17:25:11

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

2024-07-14 17:25:15

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

2024-07-14_17:25:29

# 输出年

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

2024

# 输出年月日

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

20240714

# 输出年月日时分秒

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值