培训第六天(学习find与crontab)

上午

1、find:主要进行文件搜索

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

常见的选项

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

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

     通配符*匹配零个或多个字符,通配符也可以使用在ls中)

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

  1001  find /opt/ -name "*a*"      //查找包含字母a的所有文件
  1002  find /opt/ -name "*a*"  -type f    //查找包含字母a的所有文本文件
  1003  find /opt/ -name "*a*"  -type d    //查找包含字母a的所有目录(文件夹)
  1007  find / -name httpd.conf -type f    //查找文件名为httpd.conf的文本文件
  1008  rpm -q httpd        //查找是否有httpd的软件包
  
  [root@1 ~]# touch /opt/zhangminlaoshi.abc    //创建一个文本文件
  [root@1 ~]# find / -name "zhangmin*" -type f   //查找以zhangmin开头的文本文件,从根目录开始查找速度会稍微有点慢
  /opt/zhangminlaoshi.abc
  [root@1 ~]# find /opt -name "zhangmin*" -type f   //查找以zhangmin开头的文本文件,从opt目录开始查找速度会快一点
  /opt/zhangminlaoshi.abc
案例1:找到httpd.conf文件
 [root@1 ~]# find / -name "httpd.conf" -type f
 # 无法找到,发现是没有安装httpd服务
 [root@1 ~]# yum install -y httpd
 # 安装htppd服务
 [root@1 ~]# find / -name "httpd.conf" -type f
 # 下面就是查找出的文件
 /etc/httpd/conf/httpd.conf
 /usr/lib/tmpfiles.d/httpd.conf
 # 将/范围换成/etc/目录范围,这样查找更快,更加节省计算自资源
 [root@1 ~]# find /etc/ -name "httpd.conf" -type f
 /etc/httpd/conf/httpd.conf
案例2 :获取/etc/中以.conf结尾的文件
 [root@1 ~]# find /etc/ -name "*.conf" -type f
 /etc/resolv.conf
 /etc/libaudit.conf
 /etc/depmod.d/dist.conf
 /etc/dracut.conf
 /etc/prelink.conf.d/nss-softokn-prelink.conf
 /etc/prelink.conf.d/fipscheck.conf
 /etc/prelink.conf.d/grub2.conf
 /etc/modprobe.d/tuned.conf
 /etc/modprobe.d/firewalld-sysctls.conf
 ......
案例3: 搜索以http开头的文件
 [root@1 ~]# find /etc/ -name "http*" -type f
 /etc/sysconfig/httpd
 /etc/logrotate.d/httpd
 /etc/httpd/conf/httpd.conf

2、stat:获取文件访问时间

 [root@1 ~]# stat /opt/zhangminlaoshi.abc 
   文件:"/opt/zhangminlaoshi.abc"
   大小:0          块:0          IO 块:4096   普通空文件
 设备:fd00h/64768d Inode:33808488    硬链接:1
 权限:(0644/-rw-r--r--)  Uid:(    0/    root)   Gid:(    0/    root)
 环境:unconfined_u:object_r:usr_t:s0
 最近访问:2024-07-15 09:10:43.906250733 +0800
 最近更改:2024-07-15 09:10:43.906250733 +0800
 最近改动:2024-07-15 09:10:43.906250733 +0800
 创建时间:-

3、touch:创建文件时规定创建时间

touch创建文件,没有文件就新建,有文件就修改

touch 文件名称 -m -d “日期时间格式”

  1038  touch /opt1/c.txt -m -d "2024-7-13 00:00"
  1039  touch /opt1/d.txt -m -d "2024-7-12 00:00"
  1040  touch /opt1/e.txt -m -d "2024-7-11 00:00"
  1041  touch /opt1/f.txt -m -d "2023-1-1 00:00"
  1047  ls -l /opt1
  1048  touch /opt1/f.txt -m -d "2024-7-10 00:00"
  1049  ls -l /opt1

4、dd:创建文件时规定文件大小

dd介绍:

dd 是一个用于复制文件和转换文件的工具。

在这个命令中:

  • if=/dev/zero 表示输入文件为 /dev/zero,这是一个提供连续零字节的设备文件。

  • of=/opt/hh.txt 指定输出文件为 /opt/hh.txt

  • bs=100M 表示每次读写的块大小为 100 兆字节(M)。

  • count=1 表示读写的块数量为 1。

综合起来,这个命令的作用是在 /opt 目录下创建一个大小为 100 兆字节的文件 hh.txt,文件的内容全部为零。

命令:

(1)清空opt1目录下所有文件

 [root@1 ~]# rm -rf /opt1/*
 [root@1 ~]# ls /opt1

(2)在opt1目录下创建a.txt,b.txt,c.txt

 [root@1 ~]# dd if=/dev/zero of=/opt1/a.txt bs=1M count=1
 记录了1+0 的读入
 记录了1+0 的写出
 1048576字节(1.0 MB)已复制,0.00246819 秒,425 MB/秒
 [root@1 ~]# ls -l /opt1/
 总用量 1024
 -rw-r--r--. 1 root root 1048576 7月  15 10:28 a.txt
 [root@1 ~]# ls -lh /opt1/
 总用量 1.0M
 -rw-r--r--. 1 root root 1.0M 7月  15 10:28 a.txt
 [root@1 ~]# dd if=/dev/zero of=/opt1/b.txt bs=5M count=1
 记录了1+0 的读入
 记录了1+0 的写出
 5242880字节(5.2 MB)已复制,0.0469299 秒,112 MB/秒
 [root@1 ~]# dd if=/dev/zero of=/opt1/c.txt bs=10M count=1
 记录了1+0 的读入
 记录了1+0 的写出
 10485760字节(10 MB)已复制,0.0799924 秒,131 MB/秒
 [root@1 ~]# ls -lh /opt1
 总用量 16M
 -rw-r--r--. 1 root root 1.0M 7月  15 10:28 a.txt
 -rw-r--r--. 1 root root 5.0M 7月  15 10:29 b.txt
 -rw-r--r--. 1 root root  10M 7月  15 10:29 c.txt

5、使用find按时间查找旧文件并删除

find介绍:

使用find按时间搜索,找到旧的文件删掉

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

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

+号 搜索⼏天之前的⽂件信息

-号 搜索⼏天之内的⽂件信息

案例1: 搜索三天以前的信息,不包含第三天的,⽽且只搜txt⽂件
 [root@1 ~]# find /opt1/ -mtime +3
 /opt1/e.txt
 /opt1/f.txt
 [root@1 ~]# ls -l /opt1
 总用量 0
 -rw-r--r--. 1 root root 0 7月  15 09:32 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
 -rw-r--r--. 1 root root 0 7月  12 00:00 d.txt
 -rw-r--r--. 1 root root 0 7月  11 00:00 e.txt
 -rw-r--r--. 1 root root 0 7月  10 00:00 f.txt
案例2: 搜索三天以内的.txt⽂件 ,包含今天
 [root@1 ~]# find /opt1/ -mtime -3
 /opt1/
 /opt1/a.txt
 /opt1/b.txt
 /opt1/c.txt
 [root@1 ~]# ls -l /opt1
 总用量 0
 -rw-r--r--. 1 root root 0 7月  15 09:32 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
 -rw-r--r--. 1 root root 0 7月  12 00:00 d.txt
 -rw-r--r--. 1 root root 0 7月  11 00:00 e.txt
 -rw-r--r--. 1 root root 0 7月  10 00:00 f.txt
案例3:搜索从今天开始前第三天的.txt文件
 [root@1 ~]# find /opt1 -mtime 3
 /opt1/d.txt
问题:ls与find找到文件之后不能直接删除文件
方法一:-exec

find 命令结合 exec 选项可以用来删除文件。

使⽤-exec ⽂件调⽤rm函数

{}表示前⾯find查到的内容

\;表示标识符

 [root@1 ~]# find /opt1/ -mtime +3 -exec rm -rf {} \;
 [root@1 ~]# ls -l /opt1
 总用量 0
 -rw-r--r--. 1 root root 0 7月  15 09:32 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
 -rw-r--r--. 1 root root 0 7月  12 00:00 d.txt
方法二:xargs

find与xargs结合使用

xargs 是一个在 UNIX 和类 UNIX 系统中非常有用的命令,用于将标准输入转换为命令行参数,并执行指定的命令。

它的主要作用是将前面命令的输出结果,按照一定的规则传递给后续的命令作为参数。

 [root@1 ~]# find /opt1/ -mtime +2 | xargs rm -rf
 [root@1 ~]# ls -l /opt1
 总用量 0
 -rw-r--r--. 1 root root 0 7月  15 09:32 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

用法 1:将输入转换为命令参数

假设我们有一个文件 files.txt ,里面包含了一些文件名,我们可以使用以下命令来删除这些文件:

cat files.txt | xargs rm

用法 2:指定每次传递的参数个数

通过 -n 选项可以指定每次传递给后续命令的参数个数。例如,每次传递 2 个参数给 ls 命令:

cat files.txt | xargs -n 2 ls

用法 3:结合 find 命令使用

可以与 find 命令结合,对找到的文件进行操作。例如,查找当前目录下所有的 .txt 文件并修改其权限:

find. -type f -name "*.txt" | xargs chmod 644

用法 4:与其他命令配合

比如,使用 mkdir 创建多个目录:

echo dir1 dir2 dir3 | xargs mkdir

6、使用find按文件大小进行搜索

 [root@1 ~]# find / -size +100M    //查找大于100M的文件
 [root@1 ~]# find / -size +1G     //查找大于1G的文件
 [root@1 ~]# find / -size -100k    //查找小于100k的文件

使用find按照文件大小查找文件(也能找到隐藏文件)

 [root@1 ~]# find /opt1/ -size +5M
 /opt1/c.txt
 [root@1 ~]# find /opt1/ -size -5M
 /opt1/
 /opt1/a.txt
 [root@1 ~]# find /opt1/ -size 5M
 /opt1/b.txt

7、tree:用于以树状结构显示目录和文件

基本用法: 直接在终端输入 tree ,将显示当前目录的树状结构。

指定目录: 如果要显示特定目录的树状结构,只需在命令后添加目录路径,例如:

tree /home/user/directory

控制显示深度: 使用 -L 选项指定显示的深度,例如显示 2 层深度:

tree -L 2

只显示目录: 使用 -d 选项,例如:

tree -d

显示文件权限等详细信息: 使用 -p 选项,例如:

tree -p

输出到文件: 可以将树状结构输出到文件中,例如:

tree > tree_output.txt

以下是一个示例,假设在 /home/user/project 目录下有以下结构:

 .
 ├── file1.txt
 ├── dir1
 │   ├── file2.txt
 │   └── subdir1
 │       └── file3.txt
 └── dir2
     └── file4.txt</span></span>

执行 tree -L 2 命令后,输出可能如下:

 /home/user/project
 ├── file1.txt
 ├── dir1
 │   ├── file2.txt
 │   └── subdir1
 └── dir2
     └── file4.txt</span></span>

执行 tree -d 命令,输出可能如下:

 .
 ├── dir1
 └── dir2</span></span>

执行 tree -p 命令,输出可能包含文件权限信息,例如:

 .
 ├── [-rw-r--r--]  file1.txt
 ├── [drwxr-xr-x]  dir1
 │   ├── [-rw-r--r--]  file2.txt
 │   └── [drwxr-xr-x]  subdir1
 └── [drwxr-xr-x]  dir2</span></span>

8、scp:上传下载文件

SCP介绍:

下载 把数据从远程主机保存到本地主机

上传 把本地文件保存到远程主机

SCP(Secure Copy)是一种用于在本地和远程主机之间安全传输文件和文件夹的命令行工具,它基于 SSH 协议,并提供了加密和身份验证机制,确保数据的安全性和完整性。(要求本地主机与远程主机都是linux系统)

命令:
 scp /opt1/a.txt  root@10.0.0.20:/opt   //上传文件到远程主机
 scp   -r   /opt1/  root@10.0.0.20:/  //上传目录(文件夹)到远程主机
 执行命令时,系统会要求输入远程主机的密码进行身份验证。
 scp  root@10.0.0.20:/opt1/b.txt /opt1/   //从远程主机下载文件
 scp  -r   root@10.0.0.20:/opt1/haha  /opt1/   //从远程主机下载目录(文件夹)

其他常用选项:

  • -p:保留文件的最后修改时间、最后访问时间和权限模式;

  • -c:使用指定的加密算法进行数据加密;

  • -q:不显示传输进度信息;

  • -P:指定 SSH 服务器的端口号(如果不是默认的 22 端口);

  • -i:指定用于身份验证的私钥文件路径(如果使用 SSH 密钥对进行认证)。

如果远程主机上的 SSH 服务器使用非标准端口 2222,可以使用以下命令上传文件:

scp -p 2222 /path/to/local/file user@remote:/path/to/destination

如果希望使用 SSH 密钥对进行身份验证,可以通过添加-i选项指定私钥文件的路径:

scp -i /path/to/private_key /path/to/local/file user@remote:/path/to/destination

9、crontab:计划任务

Crontab 介绍:

Crontab 是类 Unix 操作系统(包括 Linux 系统)中的一个用于设置定时任务的工具。

它通过一种特定的语法来指定任务执行的时间规则。例如,* * * * * command 表示每分钟执行一次 command 命令。

其时间设置字段从左到右依次为:分钟(0 - 59)、小时(0 - 23)、日(1 - 31)、月(1 - 12 或 jan - dec)、星期(0 - 7 或 sun - sat,0 和 7 都表示星期日)。

以下是一些常见的例子:

  • 0 5 * * * command :每天早上 5 点执行。

  • 30 18 * * 1-5 command :周一到周五的 18 点 30 分执行。

  • 0 0 1 1 * command :每年 1 月 1 日的 0 点执行。

使用 crontab 可以方便地实现各种定时任务,如定时备份数据、定时清理缓存、定时发送邮件等。它在系统管理和自动化任务处理中发挥着重要作用。

命令:

(1)查看是否存在计划任务

 crontab -l    //查看是否有计划任务
 which ls    //查看ls命令位于什么位置

(2)每分钟自动将opt目录中的文件名写到root目录下的list中

 crontab  -e    //编写计划任务
 */1 * * * * /usr/bin/ls  /opt/ >> /root/list   (*表示分时日月周)     //每分钟执行一次
 49 * * * *  /usr/bin/echo "哈哈" > /root/echo.txt  //每到49分时执行一次,类如:11:49可以执行一次

(3)验证

 ls
 vim list
 vim echo.txt

(4)使用dd命令进行测试

 dd if=/dev/zero of=/opt/hh.txt bs=100M count=1
 vim list

(5)每分钟在/tmp目录下创建一个tar包(包含etc目录下的所有文件)

 tar -czvf /tmp/etc.tar.gz  /etc
 which tar
 crontab -e
 */2 * * * *  /usr/bin/tar -czvf /tmp/etc.tar.gz  /etc

(6)研究时间戳

 date
 date "+%T"   //只显示时间
 date "+%F"   //只显示日期
 date "+%Y%m%d%H%M%S"    //年月日时分秒
 crontab -e
 */1 * * * * /usr/bin/tar -czvf /tmp/etc_$(date"+\%Y\%m\%d\%H\%M\%S").tar.gz  /etc/

下午

1、find简单练习

 [root@1 ~]# mkdir /opt1
 [root@1 ~]# ls /opt1
 [root@1 ~]# touch /opt1/hh.txt
 [root@1 ~]# touch /opt1/xx.txt
 [root@1 ~]# touch /opt1/oo.txt
 [root@1 ~]# touch /opt1/aa.txt
 [root@1 ~]# touch /opt1/cc.txt
 [root@1 ~]# touch /opt1/bb.txt
 [root@1 ~]# ls /opt1/
 aa.txt  bb.txt  cc.txt  hh.txt  oo.txt  xx.txt
 [root@1 ~]# find /opt1/*.txt
 /opt1/aa.txt
 /opt1/bb.txt
 /opt1/cc.txt
 /opt1/hh.txt
 /opt1/oo.txt
 /opt1/xx.txt

2、stat练习

 [root@1 ~]# stat /opt1/aa.txt
   文件:"/opt1/aa.txt"
   大小:0          块:0          IO 块:4096   普通空文件
 设备:fd00h/64768d Inode:34872933    硬链接:1
 权限:(0644/-rw-r--r--)  Uid:(    0/    root)   Gid:(    0/    root)
 环境:unconfined_u:object_r:default_t:s0
 最近访问:2024-07-15 16:24:27.743052285 +0800
 最近更改:2024-07-15 16:24:27.743052285 +0800
 最近改动:2024-07-15 16:24:27.743052285 +0800
 创建时间:-
 [root@1 ~]# stat /opt1/oo.txt
   文件:"/opt1/oo.txt"
   大小:0          块:0          IO 块:4096   普通空文件
 设备:fd00h/64768d Inode:34872932    硬链接:1
 权限:(0644/-rw-r--r--)  Uid:(    0/    root)   Gid:(    0/    root)
 环境:unconfined_u:object_r:default_t:s0
 最近访问:2024-07-15 16:24:24.252036738 +0800
 最近更改:2024-07-15 16:24:24.252036738 +0800
 最近改动:2024-07-15 16:24:24.252036738 +0800
 创建时间:-

3、touch规定创建文件时间练习

 [root@1 ~]# touch /opt1/1.txt -m -d "2024-7-14 00:00"
 [root@1 ~]# touch /opt1/2.txt -m -d "2024-7-13 00:00"
 [root@1 ~]# touch /opt1/3.txt -m -d "2024-7-12 00:00"
 [root@1 ~]# touch /opt1/4.txt -m -d "2024-7-11 00:00"
 [root@1 ~]# touch /opt1/5.txt -m -d "2024-7-10 00:00"
 [root@1 ~]# ls -l /opt1
 总用量 0
 -rw-r--r--. 1 root root 0 7月  14 00:00 1.txt
 -rw-r--r--. 1 root root 0 7月  13 00:00 2.txt
 -rw-r--r--. 1 root root 0 7月  12 00:00 3.txt
 -rw-r--r--. 1 root root 0 7月  11 00:00 4.txt
 -rw-r--r--. 1 root root 0 7月  10 00:00 5.txt
 -rw-r--r--. 1 root root 0 7月  15 16:24 aa.txt
 -rw-r--r--. 1 root root 0 7月  15 16:24 bb.txt
 -rw-r--r--. 1 root root 0 7月  15 16:24 cc.txt
 -rw-r--r--. 1 root root 0 7月  15 16:24 hh.txt
 -rw-r--r--. 1 root root 0 7月  15 16:24 oo.txt
 -rw-r--r--. 1 root root 0 7月  15 16:24 xx.txt

4、dd规定创建文件时间练习

 [root@1 ~]# dd if=/dev/zero of=/opt1/A.txt bs=10M count=1
 记录了1+0 的读入
 记录了1+0 的写出
 10485760字节(10 MB)已复制,0.0816065 秒,128 MB/秒
 [root@1 ~]# dd if=/dev/zero of=/opt1/B.txt bs=20M count=1
 记录了1+0 的读入
 记录了1+0 的写出
 20971520字节(21 MB)已复制,0.0766901 秒,273 MB/秒
 [root@1 ~]# dd if=/dev/zero of=/opt1/C.txt bs=30M count=1
 记录了1+0 的读入
 记录了1+0 的写出
 31457280字节(31 MB)已复制,0.111892 秒,281 MB/秒
 [root@1 ~]# ls -lh /opt1
 总用量 60M
 -rw-r--r--. 1 root root   0 7月  14 00:00 1.txt
 -rw-r--r--. 1 root root   0 7月  13 00:00 2.txt
 -rw-r--r--. 1 root root   0 7月  12 00:00 3.txt
 -rw-r--r--. 1 root root   0 7月  11 00:00 4.txt
 -rw-r--r--. 1 root root   0 7月  10 00:00 5.txt
 -rw-r--r--. 1 root root   0 7月  15 16:24 aa.txt
 -rw-r--r--. 1 root root 10M 7月  15 16:45 A.txt
 -rw-r--r--. 1 root root   0 7月  15 16:24 bb.txt
 -rw-r--r--. 1 root root 20M 7月  15 16:45 B.txt
 -rw-r--r--. 1 root root   0 7月  15 16:24 cc.txt
 -rw-r--r--. 1 root root 30M 7月  15 16:45 C.txt
 -rw-r--r--. 1 root root   0 7月  15 16:24 hh.txt
 -rw-r--r--. 1 root root   0 7月  15 16:24 oo.txt
 -rw-r--r--. 1 root root   0 7月  15 16:24 xx.txt

5、find查找旧文件并删除练习

drwxr-xr-x. 2 root root 32 7月 15 16:57 .(默认单位是b)

目录大小是随着目录里文件的大小变化而变化的

 [root@1 ~]# touch /opt1/A.txt -m -d "2024-7-10 00:00"
 [root@1 ~]# touch /opt1/B.txt -m -d "2024-7-11 00:00"
 [root@1 ~]# touch /opt1/C.txt -m -d "2024-7-12 00:00"
 [root@1 ~]# ls -lh /opt1
 总用量 60M
 -rw-r--r--. 1 root root   0 7月  14 00:00 1.txt    
 -rw-r--r--. 1 root root   0 7月  13 00:00 2.txt
 -rw-r--r--. 1 root root   0 7月  12 00:00 3.txt
 -rw-r--r--. 1 root root   0 7月  11 00:00 4.txt
 -rw-r--r--. 1 root root   0 7月  10 00:00 5.txt
 -rw-r--r--. 1 root root   0 7月  15 16:24 aa.txt
 -rw-r--r--. 1 root root 10M 7月  10 00:00 A.txt
 -rw-r--r--. 1 root root   0 7月  15 16:24 bb.txt
 -rw-r--r--. 1 root root 20M 7月  11 00:00 B.txt
 -rw-r--r--. 1 root root   0 7月  15 16:24 cc.txt
 -rw-r--r--. 1 root root 30M 7月  12 00:00 C.txt
 -rw-r--r--. 1 root root   0 7月  15 16:24 hh.txt
 -rw-r--r--. 1 root root   0 7月  15 16:24 oo.txt
 -rw-r--r--. 1 root root   0 7月  15 16:24 xx.txt
 [root@1 ~]# find /opt1/ -mtime +3
 /opt1/4.txt
 /opt1/5.txt
 /opt1/A.txt
 /opt1/B.txt
 [root@1 ~]# find /opt1/ -mtime +3 -size +5M
 /opt1/A.txt
 /opt1/B.txt
 [root@1 ~]# find /opt1/ -mtime +3 -size +5M | xargs rm -rf
 [root@1 ~]# ls -lh /opt1
 总用量 30M
 -rw-r--r--. 1 root root   0 7月  14 00:00 1.txt
 -rw-r--r--. 1 root root   0 7月  13 00:00 2.txt
 -rw-r--r--. 1 root root   0 7月  12 00:00 3.txt
 -rw-r--r--. 1 root root   0 7月  11 00:00 4.txt
 -rw-r--r--. 1 root root   0 7月  10 00:00 5.txt
 -rw-r--r--. 1 root root   0 7月  15 16:24 aa.txt
 -rw-r--r--. 1 root root   0 7月  15 16:24 bb.txt
 -rw-r--r--. 1 root root   0 7月  15 16:24 cc.txt
 -rw-r--r--. 1 root root 30M 7月  12 00:00 C.txt
 -rw-r--r--. 1 root root   0 7月  15 16:24 hh.txt
 -rw-r--r--. 1 root root   0 7月  15 16:24 oo.txt
 -rw-r--r--. 1 root root   0 7月  15 16:24 xx.txt
 [root@1 ~]# find /opt1/ -mtime 3 
 /opt1/3.txt
 /opt1/C.txt
 [root@1 ~]# find /opt1/ -mtime 3 -exec rm -rf {} \;
 [root@1 ~]# ls -lh /opt1
 总用量 0
 -rw-r--r--. 1 root root 0 7月  14 00:00 1.txt
 -rw-r--r--. 1 root root 0 7月  13 00:00 2.txt
 -rw-r--r--. 1 root root 0 7月  11 00:00 4.txt
 -rw-r--r--. 1 root root 0 7月  10 00:00 5.txt
 -rw-r--r--. 1 root root 0 7月  15 16:24 aa.txt
 -rw-r--r--. 1 root root 0 7月  15 16:24 bb.txt
 -rw-r--r--. 1 root root 0 7月  15 16:24 cc.txt
 -rw-r--r--. 1 root root 0 7月  15 16:24 hh.txt
 -rw-r--r--. 1 root root 0 7月  15 16:24 oo.txt
 -rw-r--r--. 1 root root 0 7月  15 16:24 xx.txt
 [root@1 ~]# find /opt1/ -mtime -3 | xargs rm 
 rm: 无法删除"/opt1/": 是一个目录
 [root@1 ~]# ls -lh /opt1
 总用量 0
 -rw-r--r--. 1 root root 0 7月  11 00:00 4.txt
 -rw-r--r--. 1 root root 0 7月  10 00:00 5.txt
 [root@1 ~]# ls -lah /opt1
 总用量 0
 drwxr-xr-x.  2 root root  32 7月  15 16:57 .
 dr-xr-xr-x. 18 root root 236 7月  15 16:23 ..
 -rw-r--r--.  1 root root   0 7月  11 00:00 4.txt
 -rw-r--r--.  1 root root   0 7月  10 00:00 5.txt
 [root@1 ~]# find /opt1/ -mtime -3 | xargs rm -rf
 [root@1 ~]# ls -lah /opt1
 ls: 无法访问/opt1: 没有那个文件或目录
 [root@1 ~]# mkdir /opt1
 [root@1 ~]# ls -lah /opt1
 总用量 0
 drwxr-xr-x.  2 root root   6 7月  15 16:59 .
 dr-xr-xr-x. 18 root root 236 7月  15 16:59 ..
 [root@1 ~]# touch /opt1/1.txt -m -d "2024-7-10 00:00"
 [root@1 ~]# dd if=/dev/zero of=/opt1/A.txt bs=10M count=1
 记录了1+0 的读入
 记录了1+0 的写出
 10485760字节(10 MB)已复制,0.0280236 秒,374 MB/秒
 [root@1 ~]# ls -lah /opt1
 总用量 10M
 drwxr-xr-x.  2 root root  32 7月  15 17:01 .
 dr-xr-xr-x. 18 root root 236 7月  15 16:59 ..
 -rw-r--r--.  1 root root   0 7月  10 00:00 1.txt
 -rw-r--r--.  1 root root 10M 7月  15 17:01 A.txt
 [root@1 ~]# find /opt1/ -size +50k -exec rm {} \;
 [root@1 ~]# ls -lah /opt1
 总用量 0
 drwxr-xr-x.  2 root root  19 7月  15 17:03 .
 dr-xr-xr-x. 18 root root 236 7月  15 16:59 ..
 -rw-r--r--.  1 root root   0 7月  10 00:00 1.txt
 [root@1 ~]# find /opt1/ -size -10b -exec rm {} \;
 rm: 无法删除"/opt1/": 是一个目录
 [root@1 ~]# find /opt1/ -size -10 -exec rm {} \;
 rm: 无法删除"/opt1/": 是一个目录
 [root@1 ~]# ls -ah /opt1
 .  ..
 [root@1 ~]# dd if=/dev/zero of=/opt1/A.txt bs=10K count=1
 记录了1+0 的读入
 记录了1+0 的写出
 10240字节(10 kB)已复制,0.000261972 秒,39.1 MB/秒
 [root@1 ~]# ls -lah /opt1
 总用量 12K
 drwxr-xr-x.  2 root root  19 7月  15 17:11 .
 dr-xr-xr-x. 18 root root 236 7月  15 16:59 ..
 -rw-r--r--.  1 root root 10K 7月  15 17:11 A.txt

6、tree练习

 [root@1 ~]# tree /opt1
 /opt1
 └── A.txt
 ​
 0 directories, 1 file
 [root@1 ~]# mkdir -p  /opt1/A/B/C/
 [root@1 ~]# tree /opt1
 /opt1
 ├── A
 │   └── B
 │       └── C
 └── A.txt
 ​
 3 directories, 1 file
 [root@1 ~]# tree -L 2 /opt1
 /opt1
 ├── A
 │   └── B
 └── A.txt
 ​
 2 directories, 1 file

7、scp练习

 1:
 [root@1 ~]# scp -r /opt1/ root@10.0.0.20:/ 
 The authenticity of host '10.0.0.20 (10.0.0.20)' can't be established.
 ECDSA key fingerprint is SHA256:/p0PnfBbtW2E/MOWaBziXwCBEnuznb+x1DiNgh1+qJw.
 ECDSA key fingerprint is MD5:62:73:f0:9f:e3:b5:f1:d2:d4:73:b3:2e:1b:14:16:d0.
 Are you sure you want to continue connecting (yes/no)? yes
 Warning: Permanently added '10.0.0.20' (ECDSA) to the list of known hosts.
 root@10.0.0.20's password: 
 A.txt                                                         100%   10KB   4.5MB/s   00:00  
 2:
 [root@2 ~]# ls /opt1
 A  a.txt  A.txt  b.txt  c.txt
 1:
 [root@1 ~]# mkdir /opt1/B
 [root@1 ~]# scp /opt1/ root@10.0.0.20:/     //上传目录必须加选项-r,否侧会报错
 root@10.0.0.20's password: 
 /opt1: not a regular file
 [root@1 ~]# touch /opt1/hh.txt 
 [root@1 ~]# scp /opt1/hh.txt root@10.0.0.20:/opt1/     //上传文本文件不用加-r
 root@10.0.0.20's password: 
 hh.txt                                                        100%    0     0.0KB/s   00:00
 2:
 [root@2 ~]# ls /opt1
 A  a.txt  A.txt  b.txt  c.txt  hh.txt
 [root@2 ~]# mkdir /opt2
 1:
 [root@1 ~]# scp -r root@10.0.0.20:/opt2 /
 root@10.0.0.20's password: 
 [root@1 ~]# ls /
 bin   dev  home  lib64  mnt  opt1  proc  run   srv  tmp  var
 boot  etc  lib   media  opt  opt2  root  sbin  sys  usr
 2:
 [root@2 ~]# touch /opt2/1.txt
 [root@2 ~]# touch /opt2/2.txt -m -d "2024-7-10 00:00"
 [root@2 ~]# ls -lh /opt2/
 总用量 0
 -rw-r--r--. 1 root root 0 7月  15 17:35 1.txt
 -rw-r--r--. 1 root root 0 7月  10 00:00 2.txt
 1:
 [root@1 ~]# scp root@10.0.0.20:/opt2/* /opt2/
 root@10.0.0.20's password: 
 1.txt                                                         100%    0     0.0KB/s   00:00    
 2.txt                                                         100%    0     0.0KB/s   00:00 
 [root@1 ~]# ls /opt2
 1.txt  2.txt

8、crontab练习

 [root@1 ~]# crontab -l
 no crontab for root
 [root@1 ~]# which echo
 /usr/bin/echo
 [root@1 ~]# which tar
 /usr/bin/tar
 [root@1 ~]# which ls
 alias ls='ls --color=auto'
     /usr/bin/ls
 [root@1 ~]# crontab -e
      */1 * * * * /usr/bin/ls /opt1 > /opt2/1.txt
 no crontab for root - using an empty one
 crontab: installing new crontab
 [root@1 ~]# cat /opt2/1.txt
 A
 A.txt
 B
 hh.txt
 [root@1 ~]# crontab -e
     */1 * * * * /usr/bin/echo "哈哈" >> /opt2/2.txt
 crontab: installing new crontab
 [root@1 ~]# date
 2024年 07月 15日 星期一 18:07:03 CST
 [root@1 ~]# cat /opt2/2.txt
 哈哈
 [root@1 ~]# crontab -e
 10 * * * * /usr/bin/tar -czvf /opt1/opt2.tar.zg /opt2/*
 crontab: installing new crontab
 [root@1 ~]# ls /opt1/
 A  A.txt  B  hh.txt  opt2.tar.gz
 [root@1 ~]# crontab -e
 */1 * * * * /usr/bin/tar -czvf /opt1/opt_$(date "+\%Y\%m\%d\%H\%M\%S").tar.gz /opt2/*
 [root@1 ~]# ls /opt1/
 A  A.txt  B  hh.txt  opt_20240715181902.tar.gz  opt2.tar.gz

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值