From b29a08640420937d77b8b0d7d4fa6305e8884839 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E8=88=92=E6=B1=B6?= <3228916049@qq.com> Date: Thu, 23 May 2024 03:50:19 +0000 Subject: [PATCH 1/4] =?UTF-8?q?=E7=AC=AC=E4=BA=94=E6=AC=A1=E4=BD=9C?= =?UTF-8?q?=E4=B8=9A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 李舒汶 <3228916049@qq.com> --- ...24\350\256\260\344\275\234\344\270\232.md" | 88 +++++++++++++++++++ 1 file changed, 88 insertions(+) create mode 100644 "\346\235\216\350\210\222\346\261\266/20240522Linux\347\254\224\350\256\260\344\275\234\344\270\232.md" diff --git "a/\346\235\216\350\210\222\346\261\266/20240522Linux\347\254\224\350\256\260\344\275\234\344\270\232.md" "b/\346\235\216\350\210\222\346\261\266/20240522Linux\347\254\224\350\256\260\344\275\234\344\270\232.md" new file mode 100644 index 0000000..4f2b503 --- /dev/null +++ "b/\346\235\216\350\210\222\346\261\266/20240522Linux\347\254\224\350\256\260\344\275\234\344\270\232.md" @@ -0,0 +1,88 @@ +作业 + +假设您刚刚登录到一个Linux系统,并位于您的家目录(`~`)下。您需要完成以下一系列复杂的操作来组织和清理您的文件和目录。请按照顺序执行,并给出相应的命令。 + +1. **创建测试文件**:在家目录下创建三个文本文件,分别命名为`.hidden.txt`(隐藏文件)、`visible1.txt`和`visible2.txt`。 + + ```js + touch .hidden.txt && touch {visible1,visible2}.txt + + // 创建隐藏文件 + touch .文件名 + ``` + +2. **列出文件和目录**:列出家目录(`~`)下的所有文件和目录,包括隐藏文件,并查看其详细权限和属性。 + + ```js + ls -al ~ + + // -a 显示所有文件包括隐藏文件 + // -l 显示详细权限和属性 + // ~ 表示家目录,cd ~也可以使用 + ``` + +3. **创建工作区**:创建一个新的目录`work_area`,并在其中创建三个子目录:`project_a`、`project_b`和` + +3. `。 + + ``` + mkdir work_area && mkdir ./work_area/{project_a,project_b,docs} + ``` + +4. **移动文本文件**:将家目录下的所有`.txt`文件移动到`work_area/docs`目录中,并确保这些文件在移动后仍然是隐藏的(如果它们是隐藏的)。 + + ```js + mv 路径/* 路径 + + // * 所有文件 + ``` + +5. **创建新文件**:在`work_area/project_a`目录下创建一个新的文本文件`notes.txt`,并添加一些内容(例如:`echo "Initial notes for project A" > work_area/project_a/notes.txt`)。 + + ``` + touch notes.txt && echo "Initial notes for project A" > work_area/project_a/notes.txt + ``` + +6. **复制目录**:递归地复制`work_area/project_a`目录到`work_area/project_b`,并命名为`project_a_backup`。 + + ``` + cp -r work_area/project_a work_area/project_b/project_a_backup + ``` + +7. **列出文件并按大小排序**:列出`work_area/docs`目录下的所有文件,并按文件大小降序排列。 + + ```js + ls -Sl + + // -S 大小降序排列 + // -l 以长列表格式显示文件 + ``` + +8. **删除所有文件**:删除`work_area/docs`目录下所有文件。 + + ``` + rm -rf work_area/docs/* + ``` + +9. **删除目录**:假设您不再需要`work_area/project_b`目录及其所有内容,请递归地强制删除它。 + + ``` + rm -rf work_area/project_b + ``` + +10. **清理空目录**:清理`work_area`目录,删除其中所有的空目录(注意:不要删除非空的目录)。 + + ``` + rmdir work_area/ + ``` + +11. **创建别名**:回到您的家目录,并创建一个别名`llh`,该别名能够列出当前目录下文件和目录的长格式列表,并以人类可读的格式显示文件大小(类似于`ls -lh`命令)。 + + ```js + vim .bashrc + + // 在编辑器中添加这一行,在alias大部队里 + alias llh='ls -lh' + ``` + + \ No newline at end of file -- Gitee From 65ddc635021f87df13978a101627e58dc9b411c4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E8=88=92=E6=B1=B6?= <3228916049@qq.com> Date: Fri, 24 May 2024 15:33:47 +0000 Subject: [PATCH 2/4] =?UTF-8?q?=E7=AC=AC=E5=85=AD=E6=AC=A1=E4=BD=9C?= =?UTF-8?q?=E4=B8=9A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 李舒汶 <3228916049@qq.com> --- .../20240523\347\273\203\344\271\240.md" | 119 ++++++++++++++++++ 1 file changed, 119 insertions(+) create mode 100644 "\346\235\216\350\210\222\346\261\266/20240523\347\273\203\344\271\240.md" diff --git "a/\346\235\216\350\210\222\346\261\266/20240523\347\273\203\344\271\240.md" "b/\346\235\216\350\210\222\346\261\266/20240523\347\273\203\344\271\240.md" new file mode 100644 index 0000000..28d5357 --- /dev/null +++ "b/\346\235\216\350\210\222\346\261\266/20240523\347\273\203\344\271\240.md" @@ -0,0 +1,119 @@ +相关和目录可自行创建后再操作 + +### 练习题 1:复制文件 + +假设你有两个目录:`/home/user/docs` 和 `/home/user/backup`。 + +1. 将`/home/user/docs`目录下的`file1.txt`复制到`/home/user/backup`目录下。 + + ```bash + cp file1.txt /home/hdsd/work_area/backup + ``` + + + +2. 复制`/home/user/docs`目录下的所有`.txt`文件到`/home/user/backup`目录下。 + + ```bash + cp *.txt /home/hdsd/work_area/backup + ``` + + + +### 练习题 2:复制文件夹及其内容 + +1. 假设`/home/user/photos`是一个包含多个图片文件夹的目录,请复制整个`/home/user/photos`目录及其所有内容到`/home/user/archive`目录下。 + + ```js + 1. cp -R *.png /home/hdsd/work_area/photos //目录1 + /home/hdsd/work_area/archive //目录2 + 2.cp -R /home/hdsd/work_area/photos //目录1 + /home/hdsd/work_area/archive //目录2 + + ``` + + + +### 练习题 3:移动文件 + +1. 将`/home/user/docs`目录下的`file2.docx`移动到`/home/user/papers`目录下。 + + ```js + mv file2.txt /home/hdsd/work_area/papers + ``` + + + +2. 如果`/home/user/papers`目录下已经有一个名为`file2.docx`的文件,请确保移动操作会询问你是否要覆盖它。 + + ```js + mv -i file2.docx /home/hdsd/work_area/papers/ + ``` + + + +### 练习题 4:重命名文件 + +1. 将`/home/user/docs`目录下的`oldname.pdf`重命名为`newname.pdf`。 + + ```js + mv oldname.pdf newname.pdf + ``` + + + +### 练习题 5:结合使用 + +1. 复制`/home/user/docs`目录下的`report.md`到`/home/user/temp`,然后将复制过去的文件重命名为`temp_report.md`。 + + ```js + cp report.md /home/hdsd/work_area/temp/temp_report.md + ``` + + + +2. 将`/home/user/docs`目录下所有以`.doc`结尾的文件移动到`/home/user/processed`,并且如果这些文件在目标目录中已经存在,则不覆盖它们。 + + ```js + mv -n *.docx /home/hdsd/work_area/processed + ``` + + + +### 练习题 6:使用通配符 + +1. 复制`/home/user/docs`目录下所有以`.jpg`或`.png`结尾的图片文件到`/home/user/images`目录。 + + ```js + mv *.jpg /home/hdsd/work_area/images + ``` + + + +### 练习题 7:详细输出 + +1. 使用`cp`命令复制文件时,显示详细的复制信息。 + + ```js + cp -rv /home/hdsd/work_area/docs /home/hdsd/work_area/temp + ``` + + + +2. 使用`mv`命令移动文件时,显示详细的移动信息。 + + ```js + mv -v /home/hdsd/work_area/docs /home/hdsd/work_area/processed + ``` + + + +### 练习题 8:更新文件 + +1. 如果`/home/user/backup`目录中已经有一个与`/home/user/docs`目录下的`file1.txt`同名的文件,并且`/home/user/docs/file1.txt`的内容是更新的,请使用`cp`命令将更新的文件复制到备份目录,并覆盖旧文件(假设你知道旧文件可以被安全地覆盖)。 + + ```js + mv -u file1.txt /home/hdsd/work_area/docs + ``` + + \ No newline at end of file -- Gitee From e2d33661701057e70daa5a6472c1135eb49a2b14 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E8=88=92=E6=B1=B6?= <3228916049@qq.com> Date: Sun, 26 May 2024 09:52:23 +0000 Subject: [PATCH 3/4] =?UTF-8?q?=E7=AC=AC=E4=B8=83=E6=AC=A1=E4=BD=9C?= =?UTF-8?q?=E4=B8=9A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 李舒汶 <3228916049@qq.com> --- ...35\345\257\271\350\267\257\345\276\204.md" | 194 ++++++++++++++++++ 1 file changed, 194 insertions(+) create mode 100644 "\346\235\216\350\210\222\346\261\266/20240524 \344\275\234\344\270\232\347\233\270\345\257\271\350\267\257\345\276\204\345\217\212\347\273\235\345\257\271\350\267\257\345\276\204.md" diff --git "a/\346\235\216\350\210\222\346\261\266/20240524 \344\275\234\344\270\232\347\233\270\345\257\271\350\267\257\345\276\204\345\217\212\347\273\235\345\257\271\350\267\257\345\276\204.md" "b/\346\235\216\350\210\222\346\261\266/20240524 \344\275\234\344\270\232\347\233\270\345\257\271\350\267\257\345\276\204\345\217\212\347\273\235\345\257\271\350\267\257\345\276\204.md" new file mode 100644 index 0000000..f530408 --- /dev/null +++ "b/\346\235\216\350\210\222\346\261\266/20240524 \344\275\234\344\270\232\347\233\270\345\257\271\350\267\257\345\276\204\345\217\212\347\273\235\345\257\271\350\267\257\345\276\204.md" @@ -0,0 +1,194 @@ +相关和目录可自行创建后再操作 + +1. 在家目录下建立文件exam.c,将文件exam.c拷贝到/tmp这个目录下,并改名为 shiyan.c + + ```bash + 1.mv exam.c /home/hdsd/tmp //绝对路径 + mv exam.c/ shiyan.c + 2.mv exam.c tmp/ //相对路径 + ``` + + + +2. 在任何目录下回到用户主目录? + + ```bash + cd / + cd ~ + cd /home + ``` + + + +3. 用长格式列出/tmp/test目录下所有的文件包括隐藏文件? + + ```bash + ls -al + ``` + + + +4. /tmp/test2目录下,创建5个文件分别是 1.txt 2.txt 3.txt 4.txt 5.txt,压缩这5个文件,压缩包的名字是hailiang.tar + + ```bash + mkdir test2 + touch 1.txt 2.txt 3.txt 4.txt 5.txt + tar -cf hailiang.tar *.txt + ``` + + + +5. 当前目录,建立文件 file1.txt 并更名为 file2.txt? + + ```bash + touch file1.txt + mv file1.txt file2.txt + ``` + + + +6. 当前目录,用vim建立文件bbbb.txt 并将用户名的加入其中保存退出? + + ```bash + vim bbbb.txt +:wq! + ``` + +7. 将家目录中扩展名为txt、doc和bak的文件全部复制到/tmp/test目录中? + + ```bash + cp *.txt *.bak *.doc /home/hdsd/hrh/tmp/test + + ``` + + + +8. 将文件file1.txt从当前目录移动到家目录的/docs中。 + + ```bash + cp file1.txt /home/hdsd/work_area/docs + + ``` + + + +9. 复制文件file2.txt从当前目录到家目录/backup中。 + + ```bash + cp file2.txt /home/hdsd/hrh/backup + + ``` + + + +10. 将家目录/docs中的所有文件和子目录移动到家目录/archive中。 + + ```bash + cp -r /home/hdsd/hrh/docs /home/hdsd/hrh/archive //绝对路径 + cp -r docs archive/ //相对路径 + ``` + + + +11. 复制家目录/photos及其所有内容到家目录/backup中。 + + ```bash + cp -r /home/hdsd/hrh/photos /home/hdsd/hrh/backup + cp -r photos backup/ + ``` + + + +12. 将文件家目录/docs/report.doc移动到家目录/papers中,并将其重命名为final_report.doc。 + + ```bash + mv report.doc /home/hdsd/hrh/papers final_report.doc + + ``` + + + +13. 在家目录/docs中创建一个名为notes.txt的空文件,并将其复制到目录家目录/backup中。 + + ```bash + cp -r notes.txt /home/hdsd/hrh/backup + cp -r notes.txt ./backup + ``` + + + +14. 复制家目录/images中所有以.jpg结尾的文件到家目录/photos中。 + + ```bash + cp *.jpg /home/hdsd/hrh/backup/photos + cp *.jpg ../photos + ``` + + + +15. 将文件家目录/docs/file1.txt和家目录/docs/file2.txt复制到家目录/backup中。 + + ```bash + cp -r file1.txt file2.txt /home/hdsd/hrh/backup + cp -r file1.txt file2.txt ../backup/ + ``` + + + +16. 将家目录/docs中的所有.txt文件复制到家目录/text_files中。 + + ```bash + cp -r *.txt /home/hdsd/hrh/text_files + cp -r *.txt ../text_files/ + ``` + + + +17. 将家目录/docs中的所有文件移动到家目录/temp中,并且如果文件已存在,则覆盖它们。 + + ```bash + cp -r /home/hdsd/hrh/backup/docs /home/hdsd/hrh/temp + cp -r ../docs ../temp + ``` + + + +18. 将家目录/docs中的所有文件移动到家目录/archive中,并且在移动时显示详细的移动信息。 + + ```bash + mv docs/* archive/ + + ``` + + + +19. 复制家目录/docs中的所有子目录及其内容到家目录/backup中。 + + ```bash + cp -r docs/* abackup/ + ``` + + + +20. 将家目录/docs中的所有文件和子目录移动到家目录/backup中,但排除文件名以"temp_"开头的文件。 + + ``` + ``` + + + +21. 将目录/docs/report.txt移动到家目录/archive中,但如果目标目录中已存在同名文件,则不直接覆盖,先备份同名文件为report.txt_bak。 + + ```bash + mv -b ./docs/report.txt ./archive + ``` + + + +22. 将家目录/docs中所有以.pdf结尾的文件复制到家目录/pdf_files中,并且如果目标目录中已存在同名文件,则忽略它们。 + + ```bash + mv -n ./docs/*.pdf ./pdf_files + ``` + + \ No newline at end of file -- Gitee From b0f9464bd0ac71d2cce51109c82c6f792a0e7d97 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E8=88=92=E6=B1=B6?= <3228916049@qq.com> Date: Tue, 28 May 2024 15:01:58 +0000 Subject: [PATCH 4/4] =?UTF-8?q?=E7=AC=AC=E5=85=AB=E6=AC=A1=E4=BD=9C?= =?UTF-8?q?=E4=B8=9A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 李舒汶 <3228916049@qq.com> --- .../20240527find\345\222\214grep.md" | 88 +++++++++++++++++++ 1 file changed, 88 insertions(+) create mode 100644 "\346\235\216\350\210\222\346\261\266/20240527find\345\222\214grep.md" diff --git "a/\346\235\216\350\210\222\346\261\266/20240527find\345\222\214grep.md" "b/\346\235\216\350\210\222\346\261\266/20240527find\345\222\214grep.md" new file mode 100644 index 0000000..2fbae34 --- /dev/null +++ "b/\346\235\216\350\210\222\346\261\266/20240527find\345\222\214grep.md" @@ -0,0 +1,88 @@ +### 操作题 + +1. **查找当前目录及其子目录中所有扩展名为 `.log` 的文件**: + + ```bash + find ./ -type f -name "*.log" + ``` + +2. **在 `/var/logs` 目录及其子目录中查找所有包含关键字 `error` 的文件,并显示匹配的行**: + + ```bash + grep -r --color=auto -cn "error" ./ + ``` + +3. **在 `/home/user/docs` 目录中查找文件名包含 `report` 的所有文件** + + ```bash + find /home/hdsd/hrh/docs/ -type f -name "*report*" + ``` + +4. **查找 `/etc` 目录中最近7天内修改过的文件**: + ```bash + find /home/hdsd/hrh/etc/ -type f -mtime -7 + ``` + +5. **显示 `/usr/bin` 目录中名为 `python` 的可执行文件的路径**: + + ```bash + find /home/hdsd/hrh/etc/ -type f -mtime -7 + ``` + +6. **查找系统中名为 `ls` 的命令及其手册页位置**: + ```bash + whereis ls + ``` + +7. **查找当前目录中包含关键字 `TODO` 的所有文件,并显示匹配的行和文件名**: + + ```bash + grep -rn "TODO" /home/hdsd/hrh + ``` + +8. **在 `/home/user/projects` 目录中查找所有包含关键字 `function` 的 `.js` 文件**: + ```bash + find ./projects -type f -name "*.js" -exec grep "function" {} + + ``` + +9. **查找并显示当前目录及其子目录中所有空文件**: + + ```bash + find . -type f -empty + ``` + +10. **在 `/var/www` 目录中查找包含关键字 `database` 的所有文件,并只显示文件名**: + + ```bash + grep -r "database" /home/hdsd/hrh/var/www/ + ``` + +### 综合操作题 + +**综合操作题:** + +假设你在一个名为 `/home/user/workspace` 的目录中工作。你需要完成以下任务: + +1. 查找该目录中所有扩展名为 `.conf` 的文件。 +2. 在这些 `.conf` 文件中查找包含关键字 `server` 的行。 +3. 将包含关键字 `server` 的文件名和匹配的行保存到一个名为 `server_lines.txt` 的文件中。 + +**预期命令步骤:** + +1. 查找所有扩展名为 `.conf` 的文件: + + ```bash + find ./workspace -type f -name "*.conf" + ``` + +2. 在这些 `.conf` 文件中查找包含关键字 `server` 的行: + ```bash + find ./workspace -type f -name "*.conf" -exec grep -n "server" ./workspace {} + + ``` + +3. 将结果保存到 `server_lines.txt` 文件中: + ```bash + find ./workspace -type f -name "*.conf" -exec grep -rn "server" ./workspace {} + > server_lines.txt + ``` + +通过这套操作题和综合操作题,你可以全面地了解和应用Linux系统中与文件和内容查询相关的常用命令。 \ No newline at end of file -- Gitee