From 8e2fea2bbc426ba3528bc601cc51b7e146f41d33 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E4=BD=B3=E6=80=A1?= <11785396+im-going-to-bed-at-one-oclock@user.noreply.gitee.com> Date: Wed, 29 May 2024 11:54:57 +0000 Subject: [PATCH] =?UTF-8?q?=E4=BD=9C=E4=B8=9A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 李佳怡 <11785396+im-going-to-bed-at-one-oclock@user.noreply.gitee.com> --- ...63\347\232\204\347\273\203\344\271\240.md" | 204 ++++++++++++++++++ 1 file changed, 204 insertions(+) create mode 100644 "06 \346\235\216\344\275\263\346\200\241/2024.05.28\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/06 \346\235\216\344\275\263\346\200\241/2024.05.28\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/06 \346\235\216\344\275\263\346\200\241/2024.05.28\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..844204a --- /dev/null +++ "b/06 \346\235\216\344\275\263\346\200\241/2024.05.28\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,204 @@ +## 作业 + +~~~java +## 查找文件或目录 + +- find + + `find` 是一个功能非常强大的查找工具,可以递归地搜索目录及其子目录中的文件和目录。它支持多种搜索条件,如名称、大小、修改时间等。 + + find 路径 条件 + + 1. 按名称查找 -name '名称规则' //模糊查询 + + 2. 按类型查找 -type f(file 文件) d(dir 目录) + + 3. 按时间查找 -atime(访问) -ctime(创建) -mtime(修改) + + +n 超过n天 -n n天以内 + + 4. 按大小查找 -size +/- -empty 为空(不为空,取反即可) + + 限制递归层级 -maxdepth 数字 + + !!!排除=》取反 !/ -not + +- which + + `which` 命令查找可执行文件的位置。它在用户的 `PATH` 环境变量中搜索命令。 + + which 命令名 + + ①文件路径 ②检测命令是否存在 + +- whereis + + whereis 命令 + + ①文件路径 ②配置文件 ③源代码 ④手册 + +- type + + `type` 命令显示命令的类型,例如是否是内置命令、别名或可执行文件。 + + type 命令名 + +- grep + + `grep` 命令用于在文件内容中搜索匹配的字符串。虽然它通常用于搜索文件内容,但也可以结合其他命令一起使用来查找文件。 + + grep '关键字' 文件名 + + -H 显示文件名 -r 目录递归 + + -l 显示查找到关键字的文件名 + + -m 仅匹配一行 + + --color=auto 显示颜色 + + -H/+文件名显示 + + {} {}.bak 文件备份重命名 + + !!!find和grep组合命令 -exec传递 {}find查找到的文件 \;结束 + +- stat + + `stat` 命令用于显示文件或文件系统的详细信息,包括大小、权限、修改时间等。 + + stat 文件名 + +- 特殊写法 + +### 操作题 + +1. **查找当前目录及其子目录中所有扩展名为 `.log` 的文件**: + + ```bash + root@iZbp165mdz3f1zavzs9xn7Z:~$ find ./* -name '*.log' + ./docs/book/book.log + ./docs/banana.log + ``` + +2. **在 `/var/logs` 目录及其子目录中查找所有包含关键字 `error` 的文件,并显示匹配的行**: + + ```bash + root@iZbp165mdz3f1zavzs9xn7Z:~/var/logs$ grep -r -n 'error' ./* + ./error.txt:1:error + ./error.txt:2:errorerror + ./error.txt:3:errorerrorerror + ./white/black.js:2:error + ./white/black.js:4:not error + ``` + +3. **在 `/home/user/docs` 目录中查找文件名包含 `report` 的所有文件**: + + ```bash + root@iZbp165mdz3f1zavzs9xn7Z:~$ find /home/user/docs -name 'report*.*' + /home/user/docs/report.txt + /home/user/docs/report_file.md + ``` + +4. **查找 `/etc` 目录中最近7天内修改过的文件**: + ```bash + root@iZbp165mdz3f1zavzs9xn7Z:~/etc$ find ./ -mtime -7 -type f + ./num/3.txt + ./num/4.txt + ./num/1.txt + ./num/5.txt + ./num/2.txt + ./6.txt + ./7.txt + ./9.txt + ./8.txt + ``` + +5. **显示 `/usr/bin` 目录中名为 `python` 的可执行文件的路径**: + + ```bash + root@iZbp165mdz3f1zavzs9xn7Z:~$ whereis usr/bin/python + python: /usr/bin/python3.9 /usr/bin/python3.9-config /usr/lib/python3.9 /usr/lib/python2.7 /etc/python3.9 /usr/local/lib/python3.9 /usr/include/python3.9 + ``` + +6. **查找系统中名为 `ls` 的命令及其手册页位置**: + + ```bash + root@iZbp165mdz3f1zavzs9xn7Z:~$ whereis ls + ls: /usr/bin/ls /usr/share/man/man1/ls.1.gz + ``` + +7. **查找当前目录中包含关键字 `TODO` 的所有文件,并显示匹配的行和文件名**: + + ```bash + root@iZbp165mdz3f1zavzs9xn7Z:~$ find ./* -type f -exec grep -n 'TODO' {} +; + ./file2.txt:1:TODO + ``` + +8. **在 `/home/user/projects` 目录中查找所有包含关键字 `function` 的 `.js` 文件**: + + ```bash + root@iZbp165mdz3f1zavzs9xn7Z:~$ find /home/user/projects -name '*.js' -type f -exec grep 'function' {} +; + /home/user/projects/rain.js:function + ``` + +9. **查找并显示当前目录及其子目录中所有空文件**: + + ```bash + root@iZbp165mdz3f1zavzs9xn7Z:~$ find ./* -empty -type f + ./docs/book/book.txt + ./docs/book/book.log + ./docs/money.js + ./docs/banana.log + ./exam.c + ./usr/bin/python + ./var/logs/white/white.js + ``` + +10. **在 `/var/www` 目录中查找包含关键字 `database` 的所有文件,并只显示文件名**: + + ```bash + root@iZbp165mdz3f1zavzs9xn7Z:~$ find ./var/www/* -type f -exec grep -l 'database' {} \; + ./var/www/linda.txt + ./var/www/milk.md + ``` + +### 综合操作题 + +**综合操作题:** + +假设你在一个名为 `/home/user/workspace` 的目录中工作。你需要完成以下任务: + +1. 查找该目录中所有扩展名为 `.conf` 的文件。 +2. 在这些 `.conf` 文件中查找包含关键字 `server` 的行。 +3. 将包含关键字 `server` 的文件名和匹配的行保存到一个名为 `server_lines.txt` 的文件中。 + +**预期命令步骤:** + +1. 查找所有扩展名为 `.conf` 的文件: + ```bash + root@iZbp165mdz3f1zavzs9xn7Z:/home/user/workspace$ find ./* -name '*.conf' -type f + ./la.conf + ./lala.conf + ./li.conf + ``` + +2. 在这些 `.conf` 文件中查找包含关键字 `server` 的行: + ```bash + root@iZbp165mdz3f1zavzs9xn7Z:/home/user/workspace$ find ./* -name '*.conf' -type f -exec grep -n 'server' {} +; + ./lala.conf:3:server + ``` + +3. 将结果保存到 `server_lines.txt` 文件中: + ```bash + root@iZbp165mdz3f1zavzs9xn7Z:/home/user/workspace$ sudo chmod a+w server_lines.txt + root@iZbp165mdz3f1zavzs9xn7Z:/home/user/workspace$ find ./* -name '*.conf' -type f -exec grep -n 'server' {} + > server_lines.txt + root@iZbp165mdz3f1zavzs9xn7Z:/home/user/workspace$ cat server_lines.txt + ./lala.conf:3:server + ``` + +通过这套操作题和综合操作题,你可以全面地了解和应用Linux系统中与文件和内容查询相关的常用命令。 + + +~~~ + -- Gitee