diff --git "a/45\351\253\230\351\233\205\350\257\227/20240528_\346\226\207\344\273\266\345\217\212\346\226\207\344\273\266\345\244\271\347\256\241\347\220\206find.md" "b/45\351\253\230\351\233\205\350\257\227/20240528_\346\226\207\344\273\266\345\217\212\346\226\207\344\273\266\345\244\271\347\256\241\347\220\206find.md" new file mode 100644 index 0000000000000000000000000000000000000000..c71f2d2957f5937837d168193b060237e4afafaf --- /dev/null +++ "b/45\351\253\230\351\233\205\350\257\227/20240528_\346\226\207\344\273\266\345\217\212\346\226\207\344\273\266\345\244\271\347\256\241\347\220\206find.md" @@ -0,0 +1,110 @@ + + ## **touch**:`touch` 命令主要用于创建新文件和更新文件时间戳。 + + ```bash + + touch filename + #如果 filename 不存在,touch 会创建一个新的空文件。 + #如果 filename 已经存在,touch 会更新该文件的访问和修改时间为当前时间。 + #只更新时间,不更新内容 + ``` + +## 查找文件和目录 + +### 1. `find` 命令 + +语法: + +``` +find 要查找的目录路径 条件【类型,名称,大小,时间】(条件可以组成使用) +``` + +```bash +find /path -name "filename" # 按名称查找文件包含了目录 +find /path -type d -name "dirname" # 按名称查找目录 +find /path -type f -name "*.txt" # 查找所有 .txt 文件 +find /path -type f -exec command {} \; # 查找文件并对其执行命令 + +find /path -type f -size +1M # 查找大于 1MB 的文件 +find /path -type f -mtime -7 # 查找最近7天内修改过的文件 +``` + +### 2. `which` 命令 + +`which` 命令查找可执行文件的位置 + +#### 基本用法 + +```bash +which command # 查找命令的路径 +``` + +### 3. `whereis` 命令 + +`whereis` 命令查找可执行文件、配置文件、源代码文件和手册页的位置。 + +#### 基本用法 + +```bash +whereis command # 查找命令的可执行文件、源代码文件和手册页 +``` + +### 4. `grep` 命令 + +```bash +# 在文件中搜索字符串 +grep "pattern" filename + +# 递归地在目录中搜索字符串 +grep -r "pattern" /path + +# 忽略大小写查找 +grep -i "error" /var/log/syslog + +# 显示不包含关键字的行 +grep -v "success" output.txt + +# 递归查找目录 +grep -r "main" /home/user/projects + +# 显示包含关键字的文件名 +grep -l "TODO" *.py + +# 计数匹配的行数 +grep -c "ERROR" error.log + +# 显示匹配行的行号 +grep -n "def" script.py + +# 仅显示匹配部分 +grep -o "pattern" file.txt + +# 显示匹配行及后面 2 行 +grep -A 2 "START" data.txt + +# 显示匹配行及前面 2 行 +grep -B 2 "END" data.txt + +# 显示匹配行及前后 2 行 +grep -C 2 "marker" document.txt + +``` + +### 5. `find` 与 `grep` 结合使用 + +通过结合 `find` 和 `grep` 命令,可以更加灵活地查找文件及其内容。 + +```bash +find /path -type f -name "*.txt" -exec grep "pattern" {} + # 查找包含特定字符串pattern的所有 .txt 文件 +``` + +### 6. `stat` 命令 + +`stat` 命令用于显示文件或文件系统的详细信息,包括大小、权限、修改时间等。 + +#### 基本用法 + +```bash +stat filename # 显示文件的详细信息 +``` + diff --git "a/45\351\253\230\351\233\205\350\257\227/20240528_\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/45\351\253\230\351\233\205\350\257\227/20240528_\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 0000000000000000000000000000000000000000..338d9a8b78d436b725f37914377804b7fb1fe8c6 --- /dev/null +++ "b/45\351\253\230\351\233\205\350\257\227/20240528_\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,81 @@ +### 操作题 + +1. **查找当前目录及其子目录中所有扩展名为 `.log` 的文件**: + ```bash + gaoyashi@iZf8zh6micauwcw30jcu5tZ:~$ find . -type f -name "*.log" + ``` + +2. **在 `/var/logs` 目录及其子目录中查找所有包含关键字 `error` 的文件,并显示匹配的行**: + ```bash + gaoyashi@iZf8zh6micauwcw30jcu5tZ:~$ grep -r "error" var/logs + ``` + +3. **在 `/home/user/docs` 目录中查找文件名包含 `report` 的所有文件**: + ```bash + gaoyashi@iZf8zh6micauwcw30jcu5tZ:~$ find user/docs -name "*report*" + ``` + +4. **查找 `/etc` 目录中最近7天内修改过的文件**: + ```bash + gaoyashi@iZf8zh6micauwcw30jcu5tZ:~$ find etc -mtime -7 + ``` + +5. **显示 `/usr/bin` 目录中名为 `python` 的可执行文件的路径**: + ```bash + gaoyashi@iZf8zh6micauwcw30jcu5tZ:~$ find usr/bin -name "python" -type f -executable + ``` + +6. **查找系统中名为 `ls` 的命令及其手册页位置**: + ```bash + gaoyashi@iZf8zh6micauwcw30jcu5tZ:~$ whereis ls + ``` + +7. **查找当前目录中包含关键字 `TODO` 的所有文件,并显示匹配的行和文件名**: + ```bash + gaoyashi@iZf8zh6micauwcw30jcu5tZ:~$ grep -rn "TODO" . + ``` + +8. **在 `/home/user/projects` 目录中查找所有包含关键字 `function` 的 `.js` 文件**: + + ```bash + gaoyashi@iZf8zh6micauwcw30jcu5tZ:~$ grep -r "function" /home/user/projects -type f -name "*.js" + ``` + +9. **查找并显示当前目录及其子目录中所有空文件**: + ```bash + gaoyashi@iZf8zh6micauwcw30jcu5tZ:~$ find . -type f -size 0 + ``` + +10. **在 `/var/www` 目录中查找包含关键字 `database` 的所有文件,并只显示文件名**: + ```bash + gaoyashi@iZf8zh6micauwcw30jcu5tZ:~$ find var/www -type f -name "*database*" + ``` + +### 综合操作题 + +**综合操作题:** + +假设你在一个名为 `/home/user/workspace` 的目录中工作。你需要完成以下任务: + +1. 查找该目录中所有扩展名为 `.conf` 的文件。 +2. 在这些 `.conf` 文件中查找包含关键字 `server` 的行。 +3. 将包含关键字 `server` 的文件名和匹配的行保存到一个名为 `server_lines.txt` 的文件中。 + +**预期命令步骤:** + +1. 查找所有扩展名为 `.conf` 的文件: + ```bash + gaoyashi@iZf8zh6micauwcw30jcu5tZ:~$ find ~/user/workspace -type f -name "*.conf" + ``` + +2. 在这些 `.conf` 文件中查找包含关键字 `server` 的行: + ```bash + gaoyashi@iZf8zh6micauwcw30jcu5tZ:~$ grep -r "server" ~/user/workspace/*.conf + ``` + +3. 将结果保存到 `server_lines.txt` 文件中: + ```bash + gaoyashi@iZf8zh6micauwcw30jcu5tZ:~$ grep -rHn "server" ~/user/workspace/*.conf > server_lines.txt + ``` + +通过这套操作题和综合操作题,你可以全面地了解和应用Linux系统中与文件和内容查询相关的常用命令。 \ No newline at end of file