Linux系统管理---基础操作

一、find命令

1.功能

主要进行文件搜索

2.用法

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

3.常见选项

-name 根据文件名查找,支持通配符*

案例:找到httpd.conf⽂件

find / -name "httpd.conf"

若是无法找到,说明没有安装httpd服务,需要安装httpd服务再进行查找

-type 根据文件类型查找,f代表普通文件,d代表目录

find /etc -type 

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

案例1:获取/etc/中以.conf结尾的⽂件

find /etc/ -name "*.conf" -type f

案例2:搜索以http开头的文件

 find /etc/ -name "http*" -type f

-size 根据文件大小查找

基本语法:find 文件路径 -size size值(单位k M G)

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

-size值:[0,size值)

+size值:(size值,无穷大)

普通单位查看文件信息

 ls -lh

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

 find / -size +100M

案例2:删除root目录下大于100M的文件

find /root/ -size +100M -exec rm -rf {} \;

4.扩展命令dd—用于生成指定大小的文件

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

        if表示输⼊⽂件

        of表示输出⽂件

        bs代表字节为单位的块⼤⼩

        count代表被复制的块

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

案例1:创建一个1M文件

[root@localhost ~]# mkdir /oo  #新创建一个目录
[root@localhost ~]# cd /oo
[root@localhost oo]# dd if=/dev/zero of=a.txt bs=1M count=1  #生成1M大小的文件a.txt
[root@localhost oo]# ls -lh
[root@localhost oo]# dd if=/dev/zero of=b.txt bs=1M count=1
[root@localhost oo]# dd if=/dev/zero of=c.txt bs=1M count=1
[root@localhost oo]# ls -lh

5.find的exec选项

它允许 Linux 用户对找到的文件执行任何命令。换句话说,可以对找到的文件执行操作。

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

方法1:使⽤xargs 将查询结果交给rm

find /opt/ -name "*.txt" -type f -mtime +3 xargs rm -rf
ls -l "*.txt"

方法2:使用find执行-exec

find /opt/ -name "*.txt" -type f -mtime +3 -exec rm -rf {} \;

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

使⽤stat命令获取⽂件的时间信息

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

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

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

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

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

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

        语法:find ⽂件路径 -mtime +days/-days -mtime

        根据⽂件最后修改时间搜索⽂件,+号 搜索几天之前的⽂件信息,-号 搜索几天之内的⽂件信息

案例:搜索3天以内的文件,包含今天,而且只搜txt⽂件

二、tree指令

创建⽂件列表,将文件名称以树的形式展示,需要使用yum指令进行安装

yum -y install tree
tree /var/log/

三、计算机克隆

虚拟机克隆需要先关闭正在运行的机器,虚拟机克隆有两种克隆方式,一种是完整克隆,需要内存较大,但是再使用的时候可以单独打开,另一种是链接克隆,需要内存不大,但使用时需要将被克隆的机器一并打开使用。

第一步:

第二步:出现克隆虚拟机向导,点击下一页选择克隆虚拟机当前状态,点击下一页选择克隆类型

第三步:命名虚拟机并选择要保存的路径,点击完成

第四步:出现以下信息则表示克隆成功

四、scp命令

使用scp命令时要求两台主机都是Linux系统

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

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

查看克隆机的ip地址,并且清空opt目录中的⽂件

查看源主机的ip地址,并且查看opt目录中的数据

从原主机上下载/opt/a.txt到克隆机上的/opt目录,注意如果有询问,输入yes

再输⼊密码即可

2.scp上传文件

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

需要必须启用ssh服务

将克隆机的z.txt上传到源主机的/opt/目录下

[root@localhost ~]# systemctl stop sshd  #启用ssh服务
[root@localhost ~]# ssh -lroot -p22 192.168.1.11   #连接源主机

[root@localhost ~]# scp z.txt root@192.168.1.10:/opt/  #上传文件

源主机查看

五、计划任务

语法格式:crontab [选项]

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

         -e edit编写计划任务

1.查看现有任务计划

[root@localhost ~]# crontab -l
no crontab for root

2.编写一个任务计划

[root@localhost ~]# crontab -e

3.计划任务和tar结合实现文件备份

将/etc/目录下的东西打包etc.tar.gz放到/tmp/目录下

[root@localhost ~]# crontab -e
*2 * * * * /usr/bin/tar -zcvf /tmp/etc.tar.gz /etc/     

六、练习

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

[root@aaa ~]# ls -l /etc/

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

[root@aaa ~]# ls -l /etc/*a*/ 

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

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

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

[root@aaa ~]# ls -l /etc/y*/

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

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

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

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

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

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

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

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

[root@aaa ~]# touch /opt/test/ghi.txt

[root@aaa ~]# touch /opt/test/xxx.txt

[root@aaa ~]# touch /opt/test/yyy.txt

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

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

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

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

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

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

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

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

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

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

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

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

[root@aaa ~]# cp -i /opt/test/*.txt /opt/test/a/

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

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

[root@aaa ~]# tar -czvf bak.tar.gz /opt/test/a

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

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

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

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

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

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

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

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

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

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

[root@aaa ~]# cp -i /opt/test/a/*.txt /opt/test/

cp:是否覆盖"/opt/test/abc.txt"? y

cp:是否覆盖"/opt/test/def.txt"? y

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

16.创建⽬录/opt/test0

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

[root@aaa ~]# ls /opt

b.txt  qq.abc  test  test0

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

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

记录了1+0 的读入

记录了1+0 的写出

5242880字节(5.2 MB)已复制,0.0575525 秒,91.1 MB/秒

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

记录了1+0 的读入

记录了1+0 的写出

20971520字节(21 MB)已复制,0.207271 秒,101 MB/秒

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

记录了1+0 的读入

记录了1+0 的写出

83886080字节(84 MB)已复制,0.805766 秒,104 MB/秒

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

[root@aaa ~]# ls -l /opt/test0

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

[root@aaa ~]# cp -r /opt/test0/*.mp4  /opt/test0/b/

[root@aaa ~]# ls /opt/test0/b

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

[root@aaa ~]# find /opt/test0/ -size +20M | xargs rm -rf

[root@aaa ~]# ls -l /opt/test0

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

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

[root@aaa ~]# ls -l /opt/test0

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

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

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

[root@aaa ~]#   cp -r /opt/test0/b /opt/test0

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

[root@aaa ~]#   cp -r /opt/test0/b /opt/test0

25.打开新的虚拟主机

根据第三大标题步骤即可。

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

[root@localhost ~]# scp /home/bak.tar.gz root@192.168.1.11:/opt/

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

[root@localhost ~]#  scp  root@192.168.1.11:/etc/skel/ /opt/

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

[root@localhost ~]# (crontab -l 2>/dev/null; echo "0 0 * * 3 /bin/bash -c 'tar -czvf /tmp/yum_repos_$(date +\%Y\%m\%d-\%H\%M\%S).tar.gz /etc/yum.repos.d/*.repo'") | crontab -e

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值