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)研究时间戳
tar -czvf /tmp/etc.tar.gz /etc
which tar
crontab -e
*/2 * * * * /usr/bin/tar -czvf /tmp/etc.tar.gz /etc