一些基础命令的细则

一.find的命令

1.find 命令的工具
主要进行文件搜索
2.基本语法

find 起始路径 选项 表达式 操作

按文件名搜索:

find /path -name "file.txt" (在 /path 下搜索名为 file.txt 的文件)

按文件类型搜索:

find /path -type d (在 /path 下搜索目录)

find /path -type f (在 /path 下搜索普通文件)

案例,找到httpd.conf文件

3.*通配符

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

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

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

1.文件的时间的概念

window中的时间

1.创建时间

2.修改时间

3.访问时间

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

语法 stat 文件

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

文件:"/opt/test.conf"

# 修改或者创建文件,并设置最后修改时间

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

[root@localhost ~]# ll /opt/

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

语法
find 起始路径 -mtime 时间条件
起始路径:指定要开始搜索的目录位置,可以是相对路径或绝对路径。

-mtime:用于根据文件的修改时间进行搜索。

时间条件:可以是以下几种形式:

n:表示文件在 n 天前的那一天被修改过。
+n:表示文件在 n 天前被修改过。
-n:表示文件在 n 天内被修改过。
案例 搜索 3 天以内的信息,不包含第三个的,而且只搜 txt 文件

四.find的exec选项

语法 find 文件路径
[root@localhost opt]# find /opt/ -name "*.txt" -type f -mtime +3 -exec rm - rf {} \;


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

1.根据文件 size 大小搜索文件
find 起始路径 -size 大小条件

其中,起始路径表示要开始搜索的目录位置,可以是相对路径或绝对路径;大小条件用于指定文件大小的范围。

常用的大小条件表示方法如下:

+n:表示大于n个单位的文件,n可以是k(KB)、m(MB)、g(GB)等,例如+100m表示大于 100MB 的文件。
-n:表示小于n个单位的文件,例如-50k表示小于 50KB 的文件。
n:表示等于n个单位的文件,例如1g表示等于 1GB 的文件。
2.扩展命令 dd
使用 dd 创建扩展命令 ,生成指定大小的测试文件
  dd if=/dev/zero of=文件名 bs=块大小 count=块数量
if=/dev/zero :指定输入文件为不断产生零的设备文件 /dev/zero 。
of=文件名 :指定输出的文件名。
bs=块大小 :设置每次读写的块大小,常见的如 bs=1M 表示 1MB ,bs=1K 表示 1KB 。
count=块数量 :指定要读写的块数量。
案例创建一个 1m 的文件

六.根据文件大小搜索文件
1.语法:find 起始路径 -size 大小条件
起始路径:指定开始搜索的目录位置,例如 /home 或者 .(表示当前目录)。

-size:指定按照文件大小进行搜索。

大小条件 常见的表示形式有:

 
并且可以在数字后面加上特定的单位:

n:表示文件大小恰好为 n 个 512 字节的块。

+n:表示文件大小大于 n 个 512 字节的块。

-n:表示文件大小小于 n 个 512 字节的块。

c:表示字节。

k:表示千字节(KB)。

M:表示兆字节(MB)。

G:表示吉字节(GB)。

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

七 .tree指令

tree 命令用于以树状图的形式列出目录的内容。

语法:tree [选项] [目录]

案例
以树状结构显示 /var/log 目录中的文件

八.计算机克隆操作
注:在关机后进行此操作


九.scp

1. 使用 scp 下载文件和目录
scp [ 选项 ] 用户名 @linux 主机地址 :/ 资源路径 linux 本地文件路径
2.从原主机上下载 /opt/a.txt 到克隆机上的 /opt 目录,注意如果有询问,输入 yes

3. 源主机 opt 目录下创建目录,并且将 a.txt 复制一份在新目录中

4. 在克隆机上使用 scp 指令复制目录到本地 opt 目录,需要添加 -r 选项,无法执行
十.scp上传文件
1.语法 scp [ 选项 ] 本地主机资源路径 { 远程主机 } 用户名 @ 主机 ip: 放置路
2. # 以下操作都是在原主机 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
十二.练习
1. 使⽤ls查看/etc/⽬录下所有的⽂件信息
命令:ls /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.ghi.txt /opt/test/xxx.txt.yyy.txt
8. 修改以上5个⽂件的最后修改时间分别为15,14,13,12,11,10⽇
命令:
touch -t 202201150000 /opt/test/abc.txt
touch -t 202201140000 /opt/test/def.txt.ghi.txt
touch -t 202201130000 /opt/test/xxx.txt.yyy.txt
touch -t 202201120000 /opt/test/ghi.txt
touch -t 202201110000 /opt/test/jkl.txt

9. 在test⽬录下创建a⽬录
命令:mkdir /opt/test/a
10. 将以上5个⽂件复制⼀份到a⽬录中
  命令:
cp /opt/test/abc.txt /opt/test/a/
cp /opt/test/def.txt.ghi.txt /opt/test/a/
cp /opt/test/xxx.txt.yyy.txt /opt/test/a/
cp /opt/test/ghi.txt /opt/test/a/
cp /opt/test/jkl.txt /opt/test/a/

11. 将a⽬录⽂件做成bak.tar.gz⽂件保存到家⽬录中
  命令:
cd /opt/test/a
tar -czvf ~/bak.tar.gz *

12. 使⽤find删除test⽬录下3天前的⽂件
  命令: find /path/to/test -type f -mtime +3 -exec rm {} \;
13. find删除opt⽬录下3天内的⽂件
  命令:find /opt -type f -mtime -3 -exec rm {} \;
14. find删除正好第三天的⽂件
  命令:find /opt -type f -mtime +2 -exec rm {} \;
15. 将/opt/test/a⽬录中的⽂件复制i⼀份到/opt/test/⽬录下
命令:cp -r /opt/test/a /opt/test/
16. 创建⽬录/opt/test0
命令: mkdir /opt/test0
17. 在/opt/test0/⽬录中创建三个⽂件 a.mp4(5M),b.mp4(20M),c.mp4(80M)
  命令:
touch /opt/test0/a.mp4
truncate -s 5M /opt/test0/a.mp4
touch /opt/test0/b.mp4
truncate -s 20M /opt/test0/b.mp4
touch /opt/test0/c.mp4
truncate -s 80M /opt/test0/c.mp4

18. 创建⽬录/opt/test0/b/
命令: mkdir /opt/test0/b
19. 将/op t/test0/中的⽂件复制⼀份/opt/test0/b/⽬录中
命令: cp -r /opt/test0/* /opt/test0/b/
20. find查询/opt/test0/⽬录中⽂件⼤于20M的,并删除
命令:find /opt/test0 -type f -size +20M -delete
21. find查询/opt/test0/⽬录中⽂件⼩于20M的⽂件并删除
命令:find /opt/test0 -type f -size -20M -delete
22. find查找/opt/test0/⽬录中⽂件size为20M的⽂件并删除
命令:find /opt/test0 -type f -size 20M -delete
23. /opt/test0/b中的⽂件复制⼀份到/opt/test0中
命令:cp -r /opt/test0/b/* /opt/test0/
24. 打开新的虚拟主机
命令:scp ~/bak.tar.gz user@192.168.2.3:/opt/

25. 将家⽬录中的bak.tar.gz⽂件上传到新主机的/opt⽬录中
​​​​​​​命令:scp user@192.168.3.4t:/etc/skel /opt/
26. 将新主机的/e tc/skel/⽬录下载到 当前主机的/opt⽬录中
​​​​​​​命令:
# 创建一个脚本文件,例如 backup_repos.sh
echo '#!/bin/bash' > backup_repos.sh
echo 'tar -czvf /tmp/repos_backup_\$(date +%Y%m%d%H%M%S).tar.gz /etc/yum.repos.d/*.repo' >> backup_repos.sh
chmod +x backup_repos.sh

27. 设置计划任务,每周3将/e tc/yum.repos.d/⽬录下的.repo⽂件压缩保存到tmp,在⽂件
名中添加时间戳
# 使用crontab设置定时任务
crontab -e
# 添加以下行到crontab文件中,表示每周三的凌晨1点执行备份任务
0 1 * * 3 /path/to/backup_repos.sh

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值