diff --git "a/42 \345\210\230\350\213\217\350\220\214/20240528_touch\343\200\201find\343\200\201grep\343\200\201whereis\345\221\275\344\273\244.md" "b/42 \345\210\230\350\213\217\350\220\214/20240528_touch\343\200\201find\343\200\201grep\343\200\201whereis\345\221\275\344\273\244.md" new file mode 100644 index 0000000000000000000000000000000000000000..6d2e234dbd3c76eddce9fbdc2cd9c5d4daccb3ca --- /dev/null +++ "b/42 \345\210\230\350\213\217\350\220\214/20240528_touch\343\200\201find\343\200\201grep\343\200\201whereis\345\221\275\344\273\244.md" @@ -0,0 +1,285 @@ +## touch、find、grep命令 + +1. #### touch:命令主要用于创建新文件和更新文件时间 + + - touch 文件名:创建新文件 + - 若该文件已存在,则更新文件时间 + - touch {1..3}.txt + - 创建文件:1.txt 2.txt 3.txt + - touch{11..16..2}.doc + - 创建11~16间的doc文件,且间隔数为2 即:11.doc 13.doc 15.doc + +2. #### find:命令主要用于查找文件和目录 + + - 语法:( -type f:文件 d:目录 ) + + ```bash + find 要查找的目录路径 条件【类型,名称,大小,时间】(条件可以组成使用) + ``` + + - 按名称查找文件包含了目录 + + ```bash + find 起始目录 -name "文件名模式" + ``` + + - 按名称查找目录 + + ```bash + find 起始目录 -type d -name "目录名" + ``` + + - 查找所有 .txt 文件 + + ```bash + find 起始目录 -type f -name "*.txt" + ``` + + - 查找文件并对其执行命令 + + ```bash + find 起始目录 -type f -exec command {} \; + ``` + + - 查找大于 1MB 的文件 + + ```bash + find 起始目录 -type f -size +1M + ``` + + - 查找最近7天内修改过的文件 + + ```bash + find 起始目录 -type f -mtime -7 + ``` + + ##### 示例: + + ```bash + find /home/user -name "document.txt" # 在 /home/user 目录及其子目录中查找名为 document.txt 的文件 + find /var/log -type f -name "*.log" -mtime -1 # 查找 /var/log 目录中最近一天内修改过的 .log 文件 + ``` + + + +3. ##### grep:命令用于在文件内容中搜索匹配的字符串 + + - 在文件中搜索字符串 + + ```bash + grep "要搜索的字符串" 文件名 + ``` + + - 递归地在目录中搜索字符串 + + ```bash + grep -r "要搜索的字符串" 目录 + ``` + + - 忽略大小写查找(`-i` 用于忽略大小写) + + ```bash + grep -i "要搜索的字符串" 查找目录(路径) + ``` + + - 显示不包含关键字的行( `-v` 不包含) + + ```bash + grep -v "关键字" 文件名 + ``` + + - 递归查找目录 + + ```bash + grep -r "要查找的字符串" 目录路径 + ``` + + - 显示包含关键字的文件名 + + ```bash + grep -l "关键字" *.py + ``` + + - 计数匹配的行数 + + ```bash + grep -c "搜索字符串" 文件名 + ``` + + - 显示匹配行的行号 + + ```bash + grep -n "搜索字符串" 文件名 + ``` + + - 仅显示匹配部分 + + ```bash + grep -o "要查找的文本" 文件名 + ``` + + - 显示匹配行及后面 2 行 + + ```bash + grep -A 2 "搜索字符串" 文件名 + ``` + + - 显示匹配行及前面 2 行 + + ```bash + grep -B 2 "搜索字符串" 文件名 + ``` + + - 显示匹配行及前后 2 行 + + ```bash + grep -C 2 "搜索字符串" 文件名 + ``` + + ##### 示例 + + ```bash + grep "error" /var/log/syslog # 在 /var/log/syslog 文件中搜索包含 "error" 的行 + grep -r "TODO" /home/user/projects # 在 /home/user/projects 目录及其子目录中搜索包含 "TODO" 的文件 + ``` + + ##### 4. `whereis` :命令查找可执行文件、配置文件、源代码文件和手册页的位置 + + ##### 基本用法 + + ```bash + whereis command # 查找命令的可执行文件、源代码文件和手册页 + #例子 + whereis ls # 查找 ls 命令的可执行文件、源代码文件和手册页 + ``` + + ##### 5. `find` 与 `grep` 结合使用 + + 通过结合 `find` 和 `grep` 命令,可以更加灵活地查找文件及其内容。 + + ```bash + find /path -type f -name "*.txt" -exec grep "pattern" {} + # 查找包含特定字符串pattern的所有 .txt 文件 + ``` + + + + + +## 作业: + +### 操作题 + +1. **查找当前目录及其子目录中所有扩展名为 `.log` 的文件**: + + ```bash + find . -type f -name "*.log" + ``` + +2. **在 `/var/logs` 目录及其子目录中查找所有包含关键字 `error` 的文件,并显示匹配的行**: + + ```bash + cd var/logs + echo "This is an error log line." > error_1.doc + echo "Another warning, but no error here." > error_22.pdf + echo "Error found in subsystem X at 03:45." > error_3.doc + find ./var/logs/ -type f -exec grep -H "error" {} \; + ``` + + ![20240528_第二题](./img/20240528_第二题.png) + +3. **在 `/home/user/docs` 目录中查找文件名包含 `report` 的所有文件**: + + ```bash + find ./user/docs -type f -name "*report*" + ``` + +4. **查找 `/etc` 目录中最近7天内修改过的文件**: + + ```bash + mkdir etc + cd etc + touch file{1..2}.txt + echo "This is a test config file." > file1.txt + echo "Another test config file, modified recently." > file2.txt + find ./etc/ -type f -mtime -7 + ``` + +5. **显示 `/usr/bin` 目录中名为 `python` 的可执行文件的路径**: + + ```bash + cd ../.. + find usr/bin -type f -name "*python*" + ``` + +6. **查找系统中名为 `ls` 的命令及其手册页位置**: + + ```bash + whereis ls + ``` + +7. **查找当前目录中包含关键字 `TODO` 的所有文件,并显示匹配的行和文件名**: + + ```bash + mkdir my_project + echo -e "# Project Plan\nTODO: Finish module A implementation\n" > plan.txt + echo -e "## Code\n// TODO: Optimize function B for better performance\n" > code.js + touch empty.txt + cd my_project + grep -rn 'TODO' + ``` + +8. **在 `/home/user/projects` 目录中查找所有包含关键字 `function` 的 `.js` 文件**: + + ```bash + mkdir -p user/projects + grep -r "function" ./user/projects/*.js + ``` + +9. **查找并显示当前目录及其子目录中所有空文件**: + + ```bash + find . -type f -empty + ``` + +10. **在 `/var/www` 目录中查找包含关键字 `database` 的所有文件,并只显示文件名**: + + ```bash + grep -lR 'database' /var/www + ``` + +### 综合操作题 + +**综合操作题:** + +假设你在一个名为 `/home/user/workspace` 的目录中工作。你需要完成以下任务: + +1. 查找该目录中所有扩展名为 `.conf` 的文件。 +2. 在这些 `.conf` 文件中查找包含关键字 `server` 的行。 +3. 将包含关键字 `server` 的文件名和匹配的行保存到一个名为 `server_lines.txt` 的文件中。 + +**预期命令步骤:** + +1. 查找所有扩展名为 `.conf` 的文件: + + ```bash + find . -type f -name "*.conf" + ``` + +2. 在这些 `.conf` 文件中查找包含关键字 `server` 的行: + + ```bash + echo "This is server 1" > 1.conf + echo "This is server 2" > 2.conf + echo "This is 3" > 3.conf + find . -type f -name "*.conf" -exec grep -H "server" {} \; + ``` + +3. 将结果保存到 `server_lines.txt` 文件中: + + ```bash + find . -type f -name "*.conf" -exec grep -H "server" {} \; > server_lines.txt + ``` + +通过这套操作题和综合操作题,你可以全面地了解和应用Linux系统中与文件和内容查询相关的常用命令。 + +![20240528_综合操作题](./img/20240528_综合操作题.png) \ No newline at end of file diff --git "a/42 \345\210\230\350\213\217\350\220\214/img/20240528_\347\254\254\344\272\214\351\242\230.png" "b/42 \345\210\230\350\213\217\350\220\214/img/20240528_\347\254\254\344\272\214\351\242\230.png" new file mode 100644 index 0000000000000000000000000000000000000000..61052a25ce83a7ac02138518f07aae4d070df590 Binary files /dev/null and "b/42 \345\210\230\350\213\217\350\220\214/img/20240528_\347\254\254\344\272\214\351\242\230.png" differ diff --git "a/42 \345\210\230\350\213\217\350\220\214/img/20240528_\347\273\274\345\220\210\346\223\215\344\275\234\351\242\230.png" "b/42 \345\210\230\350\213\217\350\220\214/img/20240528_\347\273\274\345\220\210\346\223\215\344\275\234\351\242\230.png" new file mode 100644 index 0000000000000000000000000000000000000000..a4ebfcdcd74664e21d8a652f7865b78afeb8c7e3 Binary files /dev/null and "b/42 \345\210\230\350\213\217\350\220\214/img/20240528_\347\273\274\345\220\210\346\223\215\344\275\234\351\242\230.png" differ