From 349b7443da9beb4e39e09ac25ab8abefc23b6f2d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9E=97=E6=80=A1=E7=90=B4?= <2667546072@qq.com> Date: Wed, 29 May 2024 00:14:15 +0800 Subject: [PATCH] zy --- ...63\347\232\204\347\273\203\344\271\240.md" | 168 ++++++++++++++++++ 1 file changed, 168 insertions(+) create mode 100644 "43\346\236\227\346\200\241\347\220\264/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" diff --git "a/43\346\236\227\346\200\241\347\220\264/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/43\346\236\227\346\200\241\347\220\264/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 0000000..830d411 --- /dev/null +++ "b/43\346\236\227\346\200\241\347\220\264/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,168 @@ +##### touch + +- touch 文件 + + - 创建空文件 + + - 文件名存在,就修改时间,并不会更新内容 + +- touch {文件1,文件2} :创建多个文件 + + + +##### find + +```js +// find 路径 条件[名称,类型,大小,时间] +// name:名称 +// type:类型 (d:目录;f:文件) +// size:大小 (size +2M:大于2MB大小的文件) +// mtime:时间 (mtime -7:最近7前内的文件) + +find 路径 -type f/d -name "*.doc" + +//排除隐藏文件 +find 路径 -type f -not -name "*.doc" + +``` + +##### which + +- 查找可执行文件的位置 + + ```js + which 可执行文件 + ``` + +##### whereis + +- 查找文件位置 + + ```js + whereis 文件 + ``` + +##### grep + +- 参数 + - -i :忽略大小写 + - -v :不包含关键字的行 + - -r :归查找目录 + - -l :显示包含关键字的文件名 + - -c :计数匹配的行数 + - -n:显示匹配行的行号 + - -A:匹配行及后面 2 行 + - -B :匹配行及前面 2 行 + - -C :匹配行及前后 2 行 + +```js +grep "关键字" 文件(路径) +``` + + + +##### find和grep结合使用 + +```js +find /path -type f -name "*.txt" -exec grep "pattern" {} + # 查找包含特定字符串pattern的所有 .txt 文件 +``` + + + +##### stat + +- 显示文件或文件系统的详细信息,包括大小、权限、修改时间 + + ```js + stat 文件 + ``` + + + +### 操作题 + +1. **查找当前目录及其子目录中所有扩展名为 `.log` 的文件**: + + ```bash + find ./ -type f -name "*.log" + ``` + +2. **在 `/var/logs` 目录及其子目录中查找所有包含关键字 `error` 的文件,并显示匹配的行**: + + ```bash + find ./0528/var/logs -type f -exec grep -Hn 'error' {} \; + ``` + +3. **在 `/home/user/docs` 目录中查找文件名包含 `report` 的所有文件**: + + ```bash + find ./ -type f -name "*report*" + ``` + +4. **查找 `/etc` 目录中最近7天内修改过的文件**: + ```bash + find ./etc/ -type f -mtime -7 + ``` + +5. **显示 `/usr/bin` 目录中名为 `python` 的可执行文件的路径**: + + ```bash + find usr/bin/ -type f -name "*python*" + ``` + +6. **查找系统中名为 `ls` 的命令及其手册页位置**: + ```bash + whereis ls + ``` + +7. **查找当前目录中包含关键字 `TODO` 的所有文件,并显示匹配的行和文件名**: + + ```bash + grep -r "TODO" ./ + ``` + +8. **在 `/home/user/projects` 目录中查找所有包含关键字 `function` 的 `.js` 文件**: + + ```bash + find ./ -type f -name '*funtion*.js' | grep -n "function" + ``` + +9. **查找并显示当前目录及其子目录中所有空文件**: + + ```bash + find ./ -type f -empty + ``` + +10. **在 `/var/www` 目录中查找包含关键字 `database` 的所有文件,并只显示文件名**: + + ```bash + find var/www -type f -exec grep -l "database" {} + + ``` + +### 综合操作题 + +**综合操作题:** + +假设你在一个名为 `/home/user/workspace` 的目录中工作。你需要完成以下任务: + +1. 查找该目录中所有扩展名为 `.conf` 的文件。 +2. 在这些 `.conf` 文件中查找包含关键字 `server`。 + +**预期命令步骤:** + +1. 查找所有扩展名为 `.conf` 的文件: + ```bash + find user/workspace/ -type f -name "*.conf" + ``` + +2. 在这些 `.conf` 文件中查找包含关键字 `server` 的行: + ```bash + find user/workspace/ -type f -name "*.conf" -exec grep -n 'server' {} \; + ``` + +3. 将结果保存到 `server_lines.txt` 文件中: + ```bash + find user/workspace/ -type f -name "*.conf" -exec grep -n 'server' {} \; > user/workspace/server_lines.txt + ``` + +通过这套操作题和综合操作题,你可以全面地了解和应用Linux系统中与文件和内容查询相关的常用命令。 \ No newline at end of file -- Gitee