From f057c57a8da7574af0f0a74b75563d8b631d5f1e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E6=AC=A3?= <12078128+rainbow-meet-rose-die@user.noreply.gitee.com> Date: Wed, 29 May 2024 14:49:45 +0000 Subject: [PATCH] =?UTF-8?q?=E7=AC=94=E8=AE=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 李欣 <12078128+rainbow-meet-rose-die@user.noreply.gitee.com> --- ...63\347\232\204\347\273\203\344\271\240.md" | 156 ++++++++++++++++++ 1 file changed, 156 insertions(+) create mode 100644 "51 \346\235\216\346\254\243/0528 \346\237\245\350\257\242\345\221\275\344\273\244\347\233\270\345\205\263\347\232\204\347\273\203\344\271\240.md" diff --git "a/51 \346\235\216\346\254\243/0528 \346\237\245\350\257\242\345\221\275\344\273\244\347\233\270\345\205\263\347\232\204\347\273\203\344\271\240.md" "b/51 \346\235\216\346\254\243/0528 \346\237\245\350\257\242\345\221\275\344\273\244\347\233\270\345\205\263\347\232\204\347\273\203\344\271\240.md" new file mode 100644 index 0000000..31d74d1 --- /dev/null +++ "b/51 \346\235\216\346\254\243/0528 \346\237\245\350\257\242\345\221\275\344\273\244\347\233\270\345\205\263\347\232\204\347\273\203\344\271\240.md" @@ -0,0 +1,156 @@ +#### find 查找文件或目录 + +实时且递归命令代表可见文件或目录 + +no缩写!排除 + +*代表可见文件或目录 + +find 路径 条件选项 + +##### 文件名 -name“文件名” -name "exan.c" -name "*.txt" + +##### 类型 -type 文件 f 目录 d -type f -name "*.txt" + +##### 时间 + +###### 防问 -atime N*24 +1超过两天 -2两天以内 + +###### 修改 -ntime +/-n 大于超过几天 + +###### 创建 -ctime +/-n 小于几天以内 + +###### 大小 -empty 空的 -size +-13k 大于3k -2m 小于2m + +##### 第三方 locate + +##### 找可执行文件 + +###### which + +找它的文件路径检测是否有这个命令 + +###### whereis + +找可执行文件的工径、自己置件、源代码手册 + +##### type 查看能类型 type 命令 + +##### stat 查看文件或国录的属性 + +##### grep 从文件内容中找指定关键字 查找文件,目录 + + + +### 操作题 + +1. **查找当前目录及其子目录中所有扩展名为 `.log` 的文件**: + ```bash + lmd@hecs-288852:~$ find . -name '*.log' -type f + ./a/2.log + ./a/1.log + ./b/4.log + ./b/3.log + ``` + +2. **在 `/var/logs` 目录及其子目录中查找所有包含关键字 `error` 的文件,并显示匹配的行**: + + ```bash + lmd@hecs-288852:/var/logs$ find /var/logs/* -type -exec grep -H 'error' {} \; + error1 + error3 + ``` + +3. **在 `/home/user/docs` 目录中查找文件名包含 `report` 的所有文件**: + ```bash + lmd@hecs-288852:~/docs$ find /home/lmd/docs/* -type f -name 'report*' + /home/lmd/docs/report1.txt + ``` + +4. **查找 `/etc` 目录中最近7天内修改过的文件**: + ```bash + lmd@hecs-288852:~/etc$ find ~/etc/* -mtime -7 -type f + /home/lmd/etc/1.txt + /home/lmd/etc/2.txt + /home/lmd/etc/3.txt + ``` + +5. **显示 `/usr/bin` 目录中名为 `python` 的可执行文件的路径**: + ```bash + lmd@hecs-288852:~/usr/bin$ find /usr/bin/* -name 'python*' -exec which {} \; + /usr/bin/python3 + /usr/bin/python3.9 + /usr/bin/python3.9-config + /usr/bin/python3-config + ``` + +6. **查找系统中名为 `ls` 的命令及其手册页位置**: + ```bash + lmd@hecs-288852:~/usr/bin$ whereis ls + ls: /usr/bin/ls /usr/share/man/man1/ls.1.gz + ``` + +7. **查找当前目录中包含关键字 `TODO` 的所有文件,并显示匹配的行和文件名**: + + ```bash + find ./* -type f -exec grep -H 'TODO' {} \; + ``` + +8. **在 `/home/user/projects` 目录中查找所有包含关键字 `function` 的 `.js` 文件**: + + ```bash + find /home/user/projects/* -name '*.js' -type f -exec grep -H 'function' {} \; + ``` + +9. **查找并显示当前目录及其子目录中所有空文件**: + ```bash + find ./* -empty -type f + ``` + +10. **在 `/var/www` 目录中查找包含关键字 `database` 的所有文件,并只显示文件名**: + + ```bash + find /var/www/* -type f -exec grep -l 'database' {} \; + ``` + +### 综合操作题 + +**综合操作题:** + +假设你在一个名为 `/home/user/workspace` 的目录中工作。你需要完成以下任务: + +1. 查找该目录中所有扩展名为 `.conf` 的文件。 +2. 在这些 `.conf` 文件中查找包含关键字 `server` 的行。 +3. 将包含关键字 `server` 的文件名和匹配的行保存到一个名为 `server_lines.txt` 的文件中。 + +**预期命令步骤:** + +1. 查找所有扩展名为 `.conf` 的文件: + ```bash + lmd@hecs-288852:/home/user/workspace$ find /home/user/workspace -name '*.conf' -type f + /home/user/workspace/2.conf + /home/user/workspace/3.conf + /home/user/workspace/1.conf + ``` + +2. 在这些 `.conf` 文件中查找包含关键字 `server` 的行: + ```bash + lmd@hecs-288852:/home/user/workspace$ find /home/user/workspace -name '*.conf' -type f -exec grep 'server' {} \; + server 5 + server 1 + server 2 + server 3 + ``` + +3. 将结果保存到 `server_lines.txt` 文件中: + ```bash + lmd@hecs-288852:~/user$ sudo chmod a+w ./workspace/server_lines.txt + lmd@hecs-288852:~/user$ find /home/lmd/user/workspace/* -name '*.conf' -type f -exec grep 'server' {} + > ./workspace/server_lines.txt + lmd@hecs-288852:~/user$ cat ./workspace/server_lines.txt + /home/lmd/user/workspace/1.conf:server 1 + /home/lmd/user/workspace/1.conf:server 2 + /home/lmd/user/workspace/1.conf:server 3 + /home/lmd/user/workspace/3.conf:server 5 + ``` + +通过这套操作题和综合操作题,你可以全面地了解和应用Linux系统中与文件和内容查询相关的常用命令。 \ No newline at end of file -- Gitee