find及scp

一.find命令的通

主要进行文件搜索

二.基本语法

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

-name *

-type f|d

常见的选项

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

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

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

# 无法找到,发现是没有安装httpd服务

[root@localhost ~]# yum install -y httpd

# 安装htppd服务

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

# 下面就是查找出的文件

/etc/httpd/conf/httpd.conf

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

# 将/范围换成/etc/目录范围,这样查找更快,更加节省计算自资源

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

/etc/httpd/conf/httpd.conf

三.*通配符

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

1.文件的时间的概念

window中的时间

1.创建时间

2.修改时间

3.访问时间

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

语法 stat 文件

[root@localhost ~]# stat /opt/test.conf

文件:"/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@localhost ~]# touch -m -d "2020-7-7 00:00" /opt/abc.txt

[root@localhost ~]# ll /opt/

总用量 44

-rw-r--r--. 1 root root 17992 6月 17 13:58 1.png

-rw-r--r--. 1 root root 17503 6月 17 14:02 2.png

-rw-r--r--. 1 root root 0 7月 7 2020 abc.txt

-rw-r--r--. 1 root root 23 7月 14 13:28 test.conf

[root@localhost ~]# stat /opt/abc.txt

文件:"/opt/abc.txt"

大小:0

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

设备:fd00h/64768d

Inode:33680845 硬链接:1

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

环境:unconfined_u:object_r:usr_t:s0

最近访问:2024-07-14 13:36:38.418740506 +0800最近更改:2020-07-07 00:00:00.000000000 +0800

最近改动:2024-07-14 13:36:38.418740506 +0800

通过文件的最后修改时间搜索文件

语法

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

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

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

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

语法 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

根据文件size大小搜索文件

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

size值 搜索等于size的文件

-size值 【0,size值)

+size值 (size值,正无穷)

扩展命令 dd

使用dd创建扩展命令

生成指定大小的测试文件

语法

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

if表示输入文件

of表示输出文件

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

count代表被复制的块

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

基本语法

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

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

需要使用yum指令进行安装

yum -y install tree

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 上传文件
语法 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.135.132
The authenticity of host '192.168.135.132 (192.168.135.132)' 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.135.132' (ECDSA) to the list of known
hosts.
# 输入密码
root@192.168.135.132'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.135.132 netmask 255.255.255.0 broadcast
192.168.135.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.135.129:/opt/
root@192.168.135.129's password:
a.txt
100% 1024KB 16.1MB/s 00:00
# 退出 ssh 连接
[root@localhost ~]# exit
登出 # --------------------------------------------------------
Connection to 192.168.135.132 closed.
# 查看源主机中是否已经存在 a.txt 文件
[root@localhost ~]# ls /opt/
a.txt # 上传成功
[root@localhost ~]#
上传目录,把克隆机中的 folder 目录上传到源主机的 opt 目录
# 再次使用 ssh 管理克隆机
[root@localhost ~]# ssh -lroot -p22 192.168.135.132
root@192.168.135.132's password:
Last login: Sun Jul 14 16:23:48 2024 from 192.168.135.129
# 使用 scp 上传目录到源主机上,第二次连接直接输入密码,不需要输入账号
[root@localhost ~]# scp -r /opt/folder/ root@192.168.135.129:/opt/
root@192.168.135.129'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.135.132 closed.
# 查看上传结果
[root@localhost ~]# ls /opt/
a.txt folder
要求必须启用 ssh 服务
systemctl start sshd
systemctl stop sshd
六、计划任务和tar结合实现文件备份
1. 计划任务
crontab [ 选项 ]
-l list 查看当前用户的计划任务信息
-e edit 编写计划任务
[root@localhost ~]# crontab -l
no crontab for root # 没有计划任务
编写计划任务
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
案例 每分钟将 /etc 目录下的文件打包保存在 tmp 目录中
1. 找到 tar 指令的 which 位置
[root@localhost /]# which tar
/usr/bin/tar
2. 编辑计划任务
[root@localhost /]# crontab -e
*/1 * * * * /usr/bin/tar -zcvf /tmp/etc.tar.gz /etc/
3. 以上的任务会覆盖前面的文件,需要在指令中添加时间因素
*/1 * * * * /usr/bin/tar -zcvf /tmp/etc-$(date "+%Y%m%d%H%M%S").tar.gz /etc
4. 在脚本中出现 %, 必须使用转义符 \
*/1 * * * * /usr/bin/tar -zcvf /tmp/etc-$(date "+\%Y\%m\%d\%H\%M\%S").tar.gz
/etc
5. 查看备份文件
显示
[root@localhost /]# ls -l /tmp/
总用量 116560
-rw-r--r--. 1 root root 11933315 7 月 14 17:49 etc-20240714174901.tar.gz
-rw-r--r--. 1 root root 11933315 7 月 14 17:50 etc-20240714175001.tar.gz
-rw-r--r--. 1 root root 11933315 7 月 14 17:51 etc-20240714175101.tar.gz
-rw-r--r--. 1 root root 11933315 7 月 14 17:52 etc-20240714175201.tar.gz
-rw-r--r--. 1 root root 11933315 7 月 14 17:53 etc-20240714175301.tar.gz
-rw-r--r--. 1 root root 11933315 7 月 14 17:54 etc-20240714175401.tar.gz
-rw-r--r--. 1 root root 11933315 7 月 14 17:55 etc-20240714175501.tar.gz -rw-r--r--. 1 root root 11933315 7 月 14 17:56 etc-20240714175601.tar.gz
-rw-r--r--. 1 root root 11933315 7 月 14 17:57 etc-20240714175701.tar.gz
-rw-r--r--. 1 root root 11933315 7 月 14 17:58 etc-20240714175801.tar.gz
时间
# 输出时间
[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"
20240714172653
[root@localhost ~]#
七、作业
1.使⽤ls查看/etc/⽬录下所有的⽂件信息
ls -l /etc/
2.使⽤ls查看/etc/⽬录下名包含“a”字⺟的⽂件或者⽬录信息
ls -l /etc/*a*
3.使⽤ls查看/etc/⽬录下以".conf"结尾的⽂件信息
ls -l /etc/*.conf
4.使⽤ls查看/etc/⽬录中以"y"字⺟开头的⽂件信息
ls -l /etc/y*
5.find查找/var/⽬录中以“.log”⽂件
find /var/ -type f -name "*.log"
6.在opt⽬录下创建test⽬录
mkdir /opt/test
7.在test⽬录中创建abc.txt,def.txt.ghi.txt,xxx.txt.yyy.txt五个⽂件
touch /opt/test/abc.txt /opt/test/def.txt /opt/test/ghi.txt /opt/test/xxx.txt /opt/test/yyy.txt
8.修改以上5个⽂件的最后修改时间分别为15,14,13,12,11,10⽇
touch -t 202407150000 /opt/test/abc.txt
touch -t 202407140000 /opt/test/def.txt
touch -t 202407130000 /opt/test/ghi.txt
touch -t 202407120000 /opt/test/xxx.txt
touch -t 202407110000 /opt/test/yyy.txt
9.在test⽬录下创建a⽬录
mkdir /opt/test/a
10.将以上5个⽂件复制⼀份到a⽬录中
cp /opt/test/*.txt /opt/test/a/
11.将a⽬录⽂件做成bak.tar.gz⽂件保存到家⽬录中
tar -czvf ~/bak.tar.gz -C /opt/test a
12.使⽤find删除test⽬录下3天前的⽂件
find /opt/test -type f -mtime +3 -exec rm -f {} \;
13.find删除opt⽬录下3天内的⽂件
find /opt -type f -mtime -3 -exec rm -f {} \;
14.find删除正好第三天的⽂件
find /opt -type f -mtime 3 -exec rm -f {} \;
15.将/opt/test/a⽬录中的⽂件复制i⼀份到/opt/test/⽬录下
cp /opt/test/a/* /opt/test/
16.创建⽬录/opt/test0
mkdir /opt/test0
17.在/opt/test0/⽬录中创建三个⽂件 a.mp4(5M),b.mp4(20M),c.mp4(80M)
fallocate -l 5M /opt/test0/a.mp4
fallocate -l 20M /opt/test0/b.mp4
fallocate -l 80M /opt/test0/c.mp4
18.创建⽬录/opt/test0/b/
mkdir /opt/test0/b
19.将/op t/test0/中的⽂件复制⼀份/opt/test0/b/⽬录中
cp /opt/test0/*.mp4 /opt/test0/b/
20.find查询/opt/test0/⽬录中⽂件⼤于20M的,并删除
find /opt/test0 -type f -size +20M -exec rm -f {} \;
21.find查询/opt/test0/⽬录中⽂件⼩于20M的⽂件并删除
find /opt/test0 -type f -size -20M -exec rm -f {} \;
22.find查找/opt/test0/⽬录中⽂件size为20M的⽂件并删除
find /opt/test0 -type f -size 20M -exec rm -f {} \;
23./opt/test0/b中的⽂件复制⼀份到/opt/test0中
cp /opt/test0/b/* /opt/test0/
24.打开新的虚拟主机
vmrun start /path/to/your/vm.vmx
25.将家⽬录中的bak.tar.gz⽂件上传到新主机的/opt⽬录中
scp ~/bak.tar.gz user@newhost:/opt/
26.将新主机的/e tc/skel/⽬录下载到 当前主机的/opt⽬录中
scp -r user@newhost:/etc/skel/ /opt/
27.设置计划任务,每周3将/e tc/yum.repos.d/⽬录下的.repo⽂件压缩保存到tmp,在⽂件名中添加时间戳 写出对应的以上linux命令
(crontab -l ; echo "0 0 * * 3 tar -czvf /tmp/yum_repos_$(date +\%Y\%m\%d\%H\%M\%S).tar.gz /etc/yum.repos.d/*.repo") | crontab -
 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值