diff --git "a/33 \346\236\227\347\247\200\346\270\205/\344\275\234\344\270\232/20240524-\346\226\207\344\273\266\345\217\212\346\226\207\344\273\266\345\244\271\347\273\235\345\257\271\350\267\257\345\276\204\344\270\216\347\233\270\345\257\271\350\267\257\345\276\204..md" "b/33 \346\236\227\347\247\200\346\270\205/\344\275\234\344\270\232/20240525-\346\226\207\344\273\266\345\217\212\346\226\207\344\273\266\345\244\271\347\273\235\345\257\271\350\267\257\345\276\204\344\270\216\347\233\270\345\257\271\350\267\257\345\276\204..md" similarity index 100% rename from "33 \346\236\227\347\247\200\346\270\205/\344\275\234\344\270\232/20240524-\346\226\207\344\273\266\345\217\212\346\226\207\344\273\266\345\244\271\347\273\235\345\257\271\350\267\257\345\276\204\344\270\216\347\233\270\345\257\271\350\267\257\345\276\204..md" rename to "33 \346\236\227\347\247\200\346\270\205/\344\275\234\344\270\232/20240525-\346\226\207\344\273\266\345\217\212\346\226\207\344\273\266\345\244\271\347\273\235\345\257\271\350\267\257\345\276\204\344\270\216\347\233\270\345\257\271\350\267\257\345\276\204..md" diff --git "a/33 \346\236\227\347\247\200\346\270\205/\344\275\234\344\270\232/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/33 \346\236\227\347\247\200\346\270\205/\344\275\234\344\270\232/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..77c9c2a3396c40d5842ea9372e3e2209ed586b32 --- /dev/null +++ "b/33 \346\236\227\347\247\200\346\270\205/\344\275\234\344\270\232/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,140 @@ +### 操作题 + +1. **查找当前目录及其子目录中所有扩展名为 `.log` 的文件**: + ```bash + linxq@hecs-155764:~$ find ~ -type f -name "*.log" + /home/linxq/1.log + ``` + +2. **在 `/var/logs` 目录及其子目录中查找所有包含关键字 `error` 的文件,并显示匹配的行**: + + ```bash + linxq@hecs-155764:~/var/logs$ ls + error error.pdf error.txt lc lc.txt + linxq@hecs-155764:~/var/logs$ find ./ -type f -name "error*" + ./error + ./error.txt + ./error.pdf + ``` + +3. **在 `/home/user/docs` 目录中查找文件名包含 `report` 的所有文件**: + + ```bash + linxq@hecs-155764:~/docs$ ls -la + total 8 + drwxr-xr-x 2 linxq linxq 4096 May 28 10:45 . + drwxr-xr-x 19 linxq linxq 4096 May 28 10:34 .. + -rw-r--r-- 1 linxq linxq 0 May 27 20:35 3.pdf + -rw-r--r-- 1 linxq linxq 0 May 27 20:35 4.pdf + -rw-r--r-- 1 linxq linxq 0 May 28 10:45 .report.txt + -rw-r--r-- 1 linxq linxq 0 May 28 10:26 report.txt + -rw-r--r-- 1 linxq linxq 0 May 27 20:30 temp_2.txt + linxq@hecs-155764:~/docs$ find ./ -type f -name "*report*" + ./report.txt + ./.report.txt + ``` + +4. **查找 `/etc` 目录中最近7天内修改过的文件**: + + ```bash + linxq@hecs-155764:~$ find ./etc -type f -mtime -7 + ./etc/etc.txt + ./etc/etc.pdf + ``` + +5. **显示 `/usr/bin` 目录中名为 `python` 的可执行文件的路径**: + + ```bash + linxq@hecs-155764:~$ whereis 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 /home/linxq/bin/python.txt /home/linxq/bin/python.exe + + ``` + +6. **查找系统中名为 `ls` 的命令及其手册页位置**: + + ```bash + linxq@hecs-155764:~$ whereis ls + ls: /usr/bin/ls /usr/share/man/man1/ls.1.gz + ``` + +7. **查找当前目录中包含关键字 `TODO` 的所有文件,并显示匹配的行和文件名**: + + ```bash + linxq@hecs-155764:~$ grep -r -n "TODO" ./ + ./report.txt:1:TODO这是一个饭桶的个人文件。 + ./.bash_history:1042:echo TODO这是一个饭桶的个人文件。> report.txt + ./.bash_history:1046:echo TODO这是一个饭桶的个人文件。> 1.log + ./1.log:1:TODO这是一个饭桶的个人文件。 + ``` + +8. **在 `/home/user/projects` 目录中查找所有包含关键字 `function` 的 `.js` 文件**: + + ```bash + linxq@hecs-155764:~/projects$ grep -l "funtion" *.js + funtion.js + ``` + +9. **查找并显示当前目录及其子目录中所有空文件**: + ```bash + linxq@hecs-155764:~/projects$ find ./ -type f -size 0 + ./1.js + ./iq.txt + ./2.js + ./q/iq.txt + ./.funtion.js + ``` + +10. **在 `/var/www` 目录中查找包含关键字 `database` 的所有文件,并只显示文件名**: + ```bash + linxq@hecs-155764:~/var/www$ grep -l "database" * + database.txt + eee.txt + ``` + +### 综合操作题 + +**综合操作题:** + +假设你在一个名为 `/home/user/workspace` 的目录中工作。你需要完成以下任务: + +1. 查找该目录中所有扩展名为 `.conf` 的文件。 +2. 在这些 `.conf` 文件中查找包含关键字 `server` 的行。 +3. 将包含关键字 `server` 的文件名和匹配的行保存到一个名为 `server_lines.txt` 的文件中。 + +**预期命令步骤:** + +1. 查找所有扩展名为 `.conf` 的文件: + ```bash + linxq@hecs-155764:~/workspace$ touch 1.conf 1.txt 2.conf + linxq@hecs-155764:~/workspace$ echo sqlserver 是一个数据库 > 1.conf + linxq@hecs-155764:~/workspace$ ls + 1.conf 1.txt 2.conf + linxq@hecs-155764:~/workspace$ find ./ -type f -name "*.conf" + ./2.conf + ./1.conf + ``` + +2. 在这些 `.conf` 文件中查找包含关键字 `server` 的行: + ```bash + linxq@hecs-155764:~/workspace$ grep -n -r "server" *.conf + 1.conf:1:sqlserver 是一个数据库 + + ``` + +3. 将结果保存到 `server_lines.txt` 文件中: + ```bash + linxq@hecs-155764:~/workspace$ find ./ -name "*.conf" -exec grep -n "server" {} + > server_lines.txt + linxq@hecs-155764:~/workspace$ tree + . + ├── 1.conf + ├── 1.txt + ├── 2.conf + └── server_lines.txt + + 0 directories, 4 files + linxq@hecs-155764:~/workspace$ cat server_lines.txt + ./1.conf:1:sqlserver 是一个数据库 + + ``` + +通过这套操作题和综合操作题,你可以全面地了解和应用Linux系统中与文件和内容查询相关的常用命令。 \ No newline at end of file diff --git "a/33 \346\236\227\347\247\200\346\270\205/\347\254\224\350\256\260/20240525-\347\233\270\345\257\271\350\267\257\345\276\204\344\270\216\347\273\235\345\257\271\350\267\257\345\276\204.md" "b/33 \346\236\227\347\247\200\346\270\205/\347\254\224\350\256\260/20240525-\347\233\270\345\257\271\350\267\257\345\276\204\344\270\216\347\273\235\345\257\271\350\267\257\345\276\204.md" new file mode 100644 index 0000000000000000000000000000000000000000..79ffbd97f1741151d445a0f1d87b0ab0c73689f6 --- /dev/null +++ "b/33 \346\236\227\347\247\200\346\270\205/\347\254\224\350\256\260/20240525-\347\233\270\345\257\271\350\267\257\345\276\204\344\270\216\347\273\235\345\257\271\350\267\257\345\276\204.md" @@ -0,0 +1,4 @@ +### 相对路径与绝对路径 + +1. 相对路径:从当前路径开始执行,比较灵活方便 +2. 绝对路径:无论在哪个目录下它执行都是从根目录开始,指明文件的明确位置,路径单一,不易更改 \ No newline at end of file diff --git "a/33 \346\236\227\347\247\200\346\270\205/\347\254\224\350\256\260/20240528-\346\237\245\346\211\276\346\226\207\344\273\266\345\244\271\347\233\270\345\205\263\345\221\275\344\273\244.md" "b/33 \346\236\227\347\247\200\346\270\205/\347\254\224\350\256\260/20240528-\346\237\245\346\211\276\346\226\207\344\273\266\345\244\271\347\233\270\345\205\263\345\221\275\344\273\244.md" new file mode 100644 index 0000000000000000000000000000000000000000..550aa9168a7f71c48c616bdaae1bb67e935a2119 --- /dev/null +++ "b/33 \346\236\227\347\247\200\346\270\205/\347\254\224\350\256\260/20240528-\346\237\245\346\211\276\346\226\207\344\273\266\345\244\271\347\233\270\345\205\263\345\221\275\344\273\244.md" @@ -0,0 +1,50 @@ +## 查找文件及文件夹的相关命令 + +### 一、find 查找文件夹及子目录的文件夹和文件 + +1. find 查找路径 -name ”查找的关键字“ :查找的是文件和目录 +2. find 查找路径 -type f (-not :不包括隐藏文件) -name ”查找的关键字“ :查找的只有文件,包括隐藏文件,**not是不包括隐藏文件** +3. find 查找路径 -type f -size +(-)文件大小 :按文件大小来查找,**+表示大于 ,-表示小于** +4. find 查找路径 -type f -mtime +(-)天数 :按时间查找文件 ,**+表示大于 ,-表示小于** +5. find 查找路径 -type f -name "关键字" **exec** 命令 {} \;先找文件在执行命令 +6. find 查找路径 -name ”关键字“ > 文件名 :意思是将左边的文件写到右边的文件中 + - ">":写入文件中,会覆盖内容 + - ">>":追加文件,不会覆盖,在某行添加 + +### 二、grep 查找文件中指定的关键字 + +1. grep "关键字" 查找文件名 :查找包含这个关键字的文件 + +2. grep -r "关键字" 查找的目录 :递归查找 + - -o :只显示匹配的部分 + - -n:显示匹配行的行号 + - -l :包含关键字的文件名 + - -v :不包含关键字的行 + - -**A** n :匹配行及**后**n行 + - -**B** n :匹配行及**前**n行 + - -**C** n :匹配行及**前后**n行 + +3. **find 与grep结合** 例句: + + ```js + 包含关键字 `server` 的文件名和匹配的行保存到一个名为 `server_lines.txt` 的文件中 + linxq@hecs-155764:~/workspace$ find ./ -name "*.conf" -exec grep -n "server" {} + > server_lines.txt + ``` + + + +### 三、which 查找可执行文件返回到是该文件路径 + +1. which 文件名 + +### 四、whereis 查找可执行文件、配置文件、源代码文件及手册页位置 + +1. whereis 文件名 + +### 五、type 显示命令类型以及内置的别名、命令、可执行文件 + +1. type ls :显示 ls 命令的类型 + +### 六、stat 查找文件及文件详细信息(文件大小、权限、时间) + +1. stat 文件名 \ No newline at end of file