diff --git "a/27\347\216\213\347\224\237\345\274\230/20240525\347\254\224\350\256\260-\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/27\347\216\213\347\224\237\345\274\230/20240525\347\254\224\350\256\260-\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" new file mode 100644 index 0000000000000000000000000000000000000000..d254296b583ccfa02e5e380c73be8251ef1c89cf --- /dev/null +++ "b/27\347\216\213\347\224\237\345\274\230/20240525\347\254\224\350\256\260-\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" @@ -0,0 +1,387 @@ +## 绝对路径和相对路径笔记 +1. 绝对路径 +**定义**:从根目录 `/` 开始的完整路径,用于唯一确定文件或目录的位置。 + +**特点**: + +- 以 `/` 开头 +- 与当前工作目录无关 + +**示例**: +- `/home/user/documents/file.txt` +- `/usr/local/bin` + +#### 2. 相对路径 +**定义**:相对于当前工作目录的路径,用于在当前目录的基础上指向文件或目录。 + +**特点**: + +- 不以 `/` 开头 +- 依赖于当前工作目录 + +**特殊符号**: + +- `.` 表示当前目录 ./ +- `..` 表示上一级目录../ + +相关和目录可自行创建后再操作 + +第一次全部使用绝对路径 + +第二次全部使用相对路径,要求将当前路径设置为/tmp +## 绝对路径 +1. 在家目录下建立文件exam.c,将文件exam.c拷贝到/tmp这个目录下,并改名为 shiyan.c +```html +flfj@hecs-328451:~$ cp -r exam.c /tmp/shiyan.c +flfj@hecs-328451:~$ tree + +``` +2. 在任何目录下回到用户主目录? +```html +flfj@hecs-328451:/tmp$ cd + +``` +3. 用长格式列出/tmp/test目录下所有的文件包括隐藏文件? +```html +flfj@hecs-328451:~$ cd /tmp +flfj@hecs-328451:/tmp$ mkdir test +flfj@hecs-328451:/tmp$ cd test/ +flfj@hecs-328451:/tmp/test$ ls -al + +``` +4. /tmp/test2目录下,创建5个文件分别是 1.txt 2.txt 3.txt 4.txt 5.txt,压缩这5个文件,压缩包的名字是hailiang.tar +```html +flfj@hecs-328451:/tmp$ mkdir test2 +flfj@hecs-328451:/tmp$ cd test2 +flfj@hecs-328451:/tmp/test2$ touch 1.txt 2.txt 3.txt 4.txt 5.txt +flfj@hecs-328451:/tmp/test2$ tar -zcvf hailiang.tar 1.txt 2.txt 3.txt 4.txt 5.txt +1.txt +2.txt +3.txt +4.txt +5.txt + +``` +5. 当前目录,建立文件 file1.txt 并更名为 file2.txt? +```html +flfj@hecs-328451:/tmp/test2$ touch file1.txt +flfj@hecs-328451:/tmp/test2$ mv file1.txt flie2.txt + +``` +6. 当前目录,用vim建立文件bbbb.txt 并将用户名的加入其中保存退出? +```html +flfj@hecs-328451:/tmp/test2$ vim +flfj@hecs-328451:/tmp/test2$ cat bbbb.txt +flfj + +``` +7. 将家目录中扩展名为txt、doc和bak的文件全部复制到/tmp/test目录中?从这开始做 +```html +flfj@hecs-328451:~$ cp -r *.txt *.doc *.bak /tmp/test + +``` +8. 将文件file1.txt从当前目录移动到家目录的/docs中。 +```html +flfj@hecs-328451:/tmp$ cd test2 +flfj@hecs-328451:/tmp/test2$ touch file1.txt +flfj@hecs-328451:/tmp/test2$ cd +flfj@hecs-328451:~$ mkdir docs +flfj@hecs-328451:~$ mv /tmp/test2/file1.txt docs + +``` +9. 复制文件file2.txt从当前目录到家目录/backup中。 +```html +flfj@hecs-328451:~$ mkdir backup +flfj@hecs-328451:~$ cp -r /tmp/test2/file2.txt backup/ + +``` +10. 将家目录/docs中的所有文件和子目录移动到家目录/archive中。 +```html +flfj@hecs-328451:~$ mkdir archive +flfj@hecs-328451:~$ mv docs/file1.txt archive/ + +``` +11. 复制家目录/photos及其所有内容到家目录/backup中。 +```html +flfj@hecs-328451:~$ mkdir photos +flfj@hecs-328451:~$ cd photos/ +flfj@hecs-328451:~/photos$ touch 1.txt b.txt +flfj@hecs-328451:~/photos$ cd +flfj@hecs-328451:~$ cp -r photos/*.txt backup/ + +``` +12. 将文件家目录/docs/report.doc移动到家目录/papers中,并将其重命名为final_report.doc。 +```html +flfj@hecs-328451:~$ cd docs/ +flfj@hecs-328451:~/docs$ touch report.doc +flfj@hecs-328451:~/docs$ cd +flfj@hecs-328451:~$ mkdir papers +flfj@hecs-328451:~$ mv docs/report.doc papers/final_report.doc +flfj@hecs-328451:~$ tree + +``` +13. 在家目录/docs中创建一个名为notes.txt的空文件,并将其复制到目录家目录/backup中。 +```html +flfj@hecs-328451:~$ cd docs/ +flfj@hecs-328451:~/docs$ touch note.txt +flfj@hecs-328451:~/docs$ cd +flfj@hecs-328451:~$ cp -r docs/note.txt backup/ + +``` +14. 复制家目录/images中所有以.jpg结尾的文件到家目录/photos中。 +```html +flfj@hecs-328451:~$ mkdir images +flfj@hecs-328451:~$ cd images/ +flfj@hecs-328451:~/images$ touch a.jpg b.jpg +flfj@hecs-328451:~/images$ ls +a.jpg b.jpg +flfj@hecs-328451:~/images$ cd +flfj@hecs-328451:~$ cp -r images/*.jpg photos/ + +``` +15. 将文件家目录/docs/file1.txt和家目录/docs/file2.txt复制到家目录/backup中。 +```html +flfj@hecs-328451:~$ cd docs/ +flfj@hecs-328451:~/docs$ touch file1.txt file2.txt +flfj@hecs-328451:~/docs$ ls +file1.txt file2.txt note.txt +flfj@hecs-328451:~/docs$ cd +flfj@hecs-328451:~$ cp -r docs/*.txt backup/ + +``` +16. 将家目录/docs中的所有.txt文件复制到家目录/text_files中。 +```html +flfj@hecs-328451:~$ mkdir text_files +flfj@hecs-328451:~$ cp -r docs/*.txt text_files/ + +``` +17. 将家目录/docs中的所有文件移动到家目录/temp中,并且如果文件已存在,则覆盖它们。 +```html +flfj@hecs-328451:~$ mkdir temp +flfj@hecs-328451:~$ mv docs/*.txt temp/ + +``` +18. 将家目录/docs中的所有文件移动到家目录/archive中,并且在移动时显示详细的移动信息。 +```html +flfj@hecs-328451:~$ cd docs/ +flfj@hecs-328451:~/docs$ touch z.txt b.bak +flfj@hecs-328451:~/docs$ cd +flfj@hecs-328451:~$ mv -v docs/*.txt *.bak archive/ +renamed 'docs/z.txt' -> 'archive/z.txt' +renamed '3.bak' -> 'archive/3.bak' + +``` +19. 复制家目录/docs中的所有子目录及其内容到家目录/backup中。 +```html +flfj@hecs-328451:~/docs$ cp -r * /home/flfj/backup/ + +``` +20. 将家目录/docs中的所有文件和子目录移动到家目录/backup中,但排除文件名以"temp_"开头的文件。 +```html +flfj@hecs-328451:~$ mv -f docs/hu backup/ +``` +21. 将目录/docs/report.txt移动到家目录/archive中,但如果目标目录中已存在同名文件,则不直接覆盖,先备份同名文件为report.txt_bak。 +```html +flfj@hecs-328451:~$ cd docs/ +flfj@hecs-328451:~/docs$ touch report.txt +flfj@hecs-328451:~/docs$ cd +flfj@hecs-328451:~$ cd archive/ +flfj@hecs-328451:~/archive$ touch report.txt +flfj@hecs-328451:~/archive$ cd +flfj@hecs-328451:~$ cd docs/ +flfj@hecs-328451:~/docs$ cd +flfj@hecs-328451:~$ mv -b docs/report.txt archive/report.txt_bak +flfj@hecs-328451:~$ tree + +``` +22. 将家目录/docs中所有以.pdf结尾的文件复制到家目录/pdf_files中,并且如果目标目录中已存在同名文件,则忽略它们。 +```html +flfj@hecs-328451:~$ mkdir pdf_files +flfj@hecs-328451:~$ cd docs/ +flfj@hecs-328451:~/docs$ a.pdf b.pdf +-bash: a.pdf: command not found +flfj@hecs-328451:~/docs$ touch a.pdf b.pdf +flfj@hecs-328451:~/docs$ cd +flfj@hecs-328451:~$ cp -r docs/*.pdf pdf_files/ +flfj@hecs-328451:~$ tree + +``` + +## 相对路径 +1. 在家目录下建立文件exam.c,将文件exam.c拷贝到/tmp这个目录下,并改名为 shiyan.c +```html +bbq@hecs-328451:~$ touch exam.c +bbq@hecs-328451:~$ ./tmp +bbq@hecs-328451:/tmp$ cp -r ../home/bbq/exam.c ./shiyan.c +``` +2. 在任何目录下回到用户主目录? +```html +bbq@hecs-328451:/tmp$ cd ../ + +``` +3. 用长格式列出/tmp/test目录下所有的文件包括隐藏文件? +```html +bbq@hecs-328451:/tmp$ mkdir test +bbq@hecs-328451:/tmp$ cd test/ +bbq@hecs-328451:/tmp/test$ ls -al + +``` +4. /tmp/test2目录下,创建5个文件分别是 1.txt 2.txt 3.txt 4.txt 5.txt,压缩这5个文件,压缩包的名字是hailiang.tar +```html +bbq@hecs-328451:/tmp$ mkdir test2 +bbq@hecs-328451:/tmp/test2$ touch 1.txt 2.txt 3.txt 4.txt 5.txt +bbq@hecs-328451:/tmp/test2$ tar -zcvf hailiang.tar 1.txt 2.txt 3.txt 4.txt 5.txt +1.txt +2.txt +3.txt +4.txt +5.txt +bbq@hecs-328451:/tmp/test2$ ls +1.txt 2.txt 3.txt 4.txt 5.txt hailiang.tar + +``` +5. 当前目录,建立文件 file1.txt 并更名为 file2.txt? +```html +bbq@hecs-328451:/tmp/test2$ touch file1.txt +bbq@hecs-328451:/tmp/test2$ mv file1.txt file2.txt + +``` +6. 当前目录,用vim建立文件bbbb.txt 并将用户名的加入其中保存退出? +```html +bbq@hecs-328451:/tmp/test2$ vim + +``` +7. 将家目录中扩展名为txt、doc和bak的文件全部复制到/tmp/test目录中?从这开始做 +```html +bbq@hecs-328451:/tmp$ touch ~/1.txt +bbq@hecs-328451:/tmp$ touch ~/2.doc +bbq@hecs-328451:/tmp$ touch ~/3.bak +bbq@hecs-328451:/tmp$ cp -r ../home/bbq/1.txt ./test +bbq@hecs-328451:/tmp$ cp -r ../home/bbq/2.doc ./test +bbq@hecs-328451:/tmp$ cp -r ../home/bbq/3.bak ./test + + +``` +8. 将文件file1.txt从当前目录移动到家目录的/docs中。 +```html +bbq@hecs-328451:/tmp$ cd test +bbq@hecs-328451:/tmp/test$ touch file1.txt +bbq@hecs-328451:/tmp/test$ mv file1.txt ./../../home/bbq/docs/ + +``` +9. 复制文件file2.txt从当前目录到家目录/backup中。 +```html +bbq@hecs-328451:/tmp$ mkdir ./../home/bbq/backup +bbq@hecs-328451:/tmp$ cd test2 +bbq@hecs-328451:/tmp/test2$ cp -r file2.txt ./../../home/bbq/backup/ +bbq@hecs-328451:/tmp/test2$ ls ~/backup/ +file2.txt + +``` +10. 将家目录/docs中的所有文件和子目录移动到家目录/archive中。 +```html +bbq@hecs-328451:/tmp/test2$ mkdir ./../home/bbq/archive +bbq@hecs-328451:/tmp/test2$ mv ./../../home/bbq/docs/*.txt ./../../home/bbq/archive/ + +``` +11. 复制家目录/photos及其所有内容到家目录/backup中。 +```html +bbq@hecs-328451:/tmp$ mkdir ./../home/bbq/phohos +bbq@hecs-328451:/tmp$ cp -r ./../home/bbq/photos/ ./../home/bbq/backup/ +bbq@hecs-328451:/tmp$ ls ./../home/bbq/backup/ + +``` +12. 将文件家目录/docs/report.doc移动到家目录/papers中,并将其重命名为final_report.doc。 +```html +bbq@hecs-328451:/tmp$ touch ~/docs/report.doc +bbq@hecs-328451:/tmp$ ls ~/docs +bbq@hecs-328451:/tmp$ mv ./../home/bbq/docs/ ./../home/bbq/papers/ + +``` +13. 在家目录/docs中创建一个名为notes.txt的空文件,并将其复制到目录家目录/backup中。 +```html +bbq@hecs-328451:/tmp$ mkdir ./../home/bbq/docs +bbq@hecs-328451:/tmp$ touch ~/docs/notes.txt +bbq@hecs-328451:/tmp$ ls ~/docs +notes.txt +bbq@hecs-328451:/tmp$ cp -r ./../home/bbq/docs/notes.txt ./../home/bbq/backup/ +bbq@hecs-328451:/tmp$ ls ./../home/bbq/backup/ +file2.txt notes.txt photos + +``` +14. 复制家目录/images中所有以.jpg结尾的文件到家目录/photos中。 +```html +bbq@hecs-328451:/tmp$ mkdir ./../home/bbq/images +bbq@hecs-328451:/tmp$ ls ./../home/bbq/ +bbq@hecs-328451:/tmp$ touch ./../home/bbq/images/a.jpg +bbq@hecs-328451:/tmp$ cp -r ./../home/bbq/images/*.jpg ./../home/bbq/photos/ +bbq@hecs-328451:/tmp$ ls ./../home/bbq/photos/ +a.jpg + +``` +15. 将文件家目录/docs/file1.txt和家目录/docs/file2.txt复制到家目录/backup中。 +```html +bbq@hecs-328451:/tmp$ touch ./../home/bbq/docs/file1.txt +bq@hecs-328451:/tmp$ touch ./../home/bbq/docs/file2.txt +bbq@hecs-328451:/tmp$ cp -r ./../home/bbq/docs/*.txt ./../home/bbq/backup/ +bbq@hecs-328451:/tmp$ ls ./../home/bbq/backup/ +file1.txt file2.txt notes.txt photos + +``` +16. 将家目录/docs中的所有.txt文件复制到家目录/text_files中。 +```html +bbq@hecs-328451:/tmp$ mkdir ./../home/bbq/text_files +bbq@hecs-328451:/tmp$ cp -r ./../home/bbq/docs/*.txt ./../home/bbq/text_files/ +bbq@hecs-328451:/tmp$ ls ./../home/bbq/text_files/ +file1.txt file2.txt notes.txt + +``` +17. 将家目录/docs中的所有文件移动到家目录/temp中,并且如果文件已存在,则覆盖它们。 +```html +bbq@hecs-328451:/tmp$ mkdir ./../home/bbq/temp +bbq@hecs-328451:/tmp$ mv ./../home/bbq/docs/* ./../home/bbq/temp/ +bbq@hecs-328451:/tmp$ ls ./../home/bbq/temp/ +file1.txt file2.txt notes.txt + +``` +18. 将家目录/docs中的所有文件移动到家目录/archive中,并且在移动时显示详细的移动信息。 +```html +bbq@hecs-328451:/tmp$ mv -v ./../home/bbq/docs/* ./../home/bbq/archive/ +renamed './../home/bbq/docs/aaa.txt' -> './../home/bbq/archive/aaa.txt' + +``` +19. 复制家目录/docs中的所有子目录及其内容到家目录/backup中。 +```html +bbq@hecs-328451:/tmp$ mkdir ./../home/bbq/docs/as +bbq@hecs-328451:/tmp$ cp -r ./../home/bbq/docs/* ./../home/bbq/backup/ +bbq@hecs-328451:/tmp$ ls ./../home/bbq/docs/ +aaa.txt as +bbq@hecs-328451:/tmp$ ls ./../home/bbq/backup/ +aaa.txt as file1.txt file2.txt notes.txt photos +``` +20. 将家目录/docs中的所有文件和子目录移动到家目录/backup中,但排除文件名以"temp_"开头的文件。 +```html +bbq@hecs-328451:/tmp$ mv ./../home/bbq/docs/* ./../home/bbq/backup/ + +``` +21. 将目录/docs/report.txt移动到家目录/archive中,但如果目标目录中已存在同名文件,则不直接覆盖,先备份同名文件为report.txt_bak。 +```html +bbq@hecs-328451:/tmp$ ls ./../home/bbq/docs/ +report.txt +bbq@hecs-328451:/tmp$ ls ./../home/bbq/archive/ +aaa.txt file1.txt +bbq@hecs-328451:/tmp$ touch ./../home/bbq/archive/report.txt +bbq@hecs-328451:/tmp$ ls ./../home/bbq/archive/ +aaa.txt file1.txt report.txt +bbq@hecs-328451:/tmp$ mv ./../home/bbq/docs/report.txt ./../home/bbq/archive/report.txt_bak +bbq@hecs-328451:/tmp$ ls ./../home/bbq/archive/ +aaa.txt file1.txt report.txt report.txt_bak + +``` +22. 将家目录/docs中所有以.pdf结尾的文件复制到家目录/pdf_files中,并且如果目标目录中已存在同名文件,则忽略它们。 +```html +bbq@hecs-328451:/tmp$ touch ./../home/bbq/docs/qqq.pdf +bbq@hecs-328451:/tmp$ mkdir ./../home/bbq/pdf_files +bbq@hecs-328451:/tmp$ cp -r ./../home/bbq/docs/*.pdf ./../home/bbq/pdf_files/ + +``` \ No newline at end of file diff --git "a/27\347\216\213\347\224\237\345\274\230/20240528\347\254\224\350\256\260-find\344\270\216grep.md" "b/27\347\216\213\347\224\237\345\274\230/20240528\347\254\224\350\256\260-find\344\270\216grep.md" new file mode 100644 index 0000000000000000000000000000000000000000..ec7a73e808352fd6c2e51d2531924fa641867c7e --- /dev/null +++ "b/27\347\216\213\347\224\237\345\274\230/20240528\347\254\224\350\256\260-find\344\270\216grep.md" @@ -0,0 +1,181 @@ +## 笔记 +```html +##查找文件和目录 find命令 +语法:find 要查找的目录路径 条件【类型,名称,大小,时间】(条件可以组成使用) + +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天内修改过的文件 + +find /home/user -name "document.txt" # 在 /home/user 目录及其子目录中查找名为 document.txt 的文件 +find /var/log -type f -name "*.log" -mtime -1 # 查找 /var/log 目录中最近一天内修改过的 .log 文件 + + +## grep 命令 +grep "pattern" filename # 在文件中搜索字符串 +grep -r "pattern" /path # 递归地在目录中搜索字符串 +# 示例: +grep "error" /var/log/syslog # 在 /var/log/syslog 文件中搜索包含 "error" 的行 +grep -r "TODO" /home/user/projects # 在 /home/user/projects 目录及其子目录中搜索包含 "TODO" 的文件 + + +# 忽略大小写查找 +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 +``` + +### 操作题 + +1. **查找当前目录及其子目录中所有扩展名为 `.log` 的文件**: + ```bash + sd@hecs-328451:~$ touch aaa.log + sd@hecs-328451:~$ find -type f -name "*.log" + ./aaa.log + ``` + +2. **在 `/var/logs` 目录及其子目录中查找所有包含关键字 `error` 的文件,并显示匹配的行**: + + ```bash + sd@hecs-328451:~$ vim error.txt + sd@hecs-328451:~$ grep "error" var/logs/error.txt + error + sd@hecs-328451:~/var/logs$ mkdir aaa + sd@hecs-328451:~/var/logs$ ls + sd@hecs-328451:~/var/logs/aaa$ echo "error" >aaa + sd@hecs-328451:~/var/logs/aaa$ ls aaa + sd@hecs-328451:~$ grep -r "error" var/logs/ + var/logs/error.txt:error + var/logs/aaa/aaa:error + + ``` + +3. **在 `/home/user/docs` 目录中查找文件名包含 `report` 的所有文件**: + ```bash + sd@hecs-328451:~$ find /home/sd/docs/ -type f -name '*report*' + /home/sd/docs/report.txt + /home/sd/docs/report.doc + + ``` + +4. **查找 `/etc` 目录中最近7天内修改过的文件**: + ```bash + sd@hecs-328451:~$ find /etc -type f -mtime -7 + + ``` + +5. **显示 `/usr/bin` 目录中名为 `python` 的可执行文件的路径**: + + ```bash + sd@hecs-328451:~$ find /usr/bin -type f -name "python*" + /usr/bin/python3.9 + ``` + +6. **查找系统中名为 `ls` 的命令及其手册页位置**: + ```bash + sd@hecs-328451:~$ whereis ls + ls: /usr/bin/ls /usr/share/man/man1/ls.1.gz + ``` + +7. **查找当前目录中包含关键字 `TODO` 的所有文件,并显示匹配的行和文件名**: + + ```bash + sd@hecs-328451:~$ find . -type f -exec grep -Hn "TODO" {} + + ./aaa.log:1:TODO + ``` + +8. **在 `/home/user/projects` 目录中查找所有包含关键字 `function` 的 `.js` 文件**: + + ```bash + sd@hecs-328451:~$ find /home/sd/projects -type f -name "*.js" -exec grep -l "function" {} + + /home/sd/projects/111.js + ``` + +9. **查找并显示当前目录及其子目录中所有空文件**: + + ```bash + sd@hecs-328451:~$ find . -type f -empty + ./var/logs/error.doc + ./projects/222.js + ./aaa + ./docs/report.txt + ./docs/report.doc + ``` + +10. **在 `/var/www` 目录中查找包含关键字 `database` 的所有文件,并只显示文件名**: + + ```bash + sd@hecs-328451:~$ find var/www -type f -exec grep -l 'database' {} + + var/www/555.bak + var/www/999.doc + ``` + +### 综合操作题 + +**综合操作题:** + +假设你在一个名为 `/home/user/workspace` 的目录中工作。你需要完成以下任务: + +1. 查找该目录中所有扩展名为 `.conf` 的文件。 +2. 在这些 `.conf` 文件中查找包含关键字 `server` 的行。 +3. 将包含关键字 `server` 的文件名和匹配的行保存到一个名为 `server_lines.txt` 的文件中。 + +**预期命令步骤:** + +1. 查找所有扩展名为 `.conf` 的文件: +```html +sd@hecs-328451:~$ mkdir workspace +sd@hecs-328451:~$ cd workspace/ +sd@hecs-328451:~/workspace$ touch 1.conf 2.conf 3.conf 4.pdf 5.txt +sd@hecs-328451:~$ find workspace/ -type f -name "*.conf" +workspace/2.conf +workspace/3.conf +workspace/1.conf +``` + +2. 在这些 `.conf` 文件中查找包含关键字 `server` 的行: +```html +sd@hecs-328451:~$ cd workspace/ +sd@hecs-328451:~/workspace$ echo "server" >1.conf +sd@hecs-328451:~/workspace$ grep -r "server" ./ +./1.conf:server +``` + +3. 将结果保存到 `server_lines.txt` 文件中: +```html +sd@hecs-328451:~/workspace$ grep -i "server" 1.conf >server_lines.txt +sd@hecs-328451:~/workspace$ ls +1.conf 2.conf 3.conf 4.pdf 5.txt server_lines.txt +sd@hecs-328451:~/workspace$ cat server_lines.txt +server +``` + +通过这套操作题和综合操作题,你可以全面地了解和应用Linux系统中与文件和内容查询相关的常用命令。 \ No newline at end of file diff --git "a/27\347\216\213\347\224\237\345\274\230/20240529\347\254\224\350\256\260-\346\226\207\344\273\266\345\206\205\345\256\271\346\237\245\347\234\213.md" "b/27\347\216\213\347\224\237\345\274\230/20240529\347\254\224\350\256\260-\346\226\207\344\273\266\345\206\205\345\256\271\346\237\245\347\234\213.md" new file mode 100644 index 0000000000000000000000000000000000000000..8a44d2c2122fe01e3f7e7a3af2c4661b9c60148e --- /dev/null +++ "b/27\347\216\213\347\224\237\345\274\230/20240529\347\254\224\350\256\260-\346\226\207\344\273\266\345\206\205\345\256\271\346\237\245\347\234\213.md" @@ -0,0 +1,248 @@ +## 笔记 +```html + 1.cat:显示文件内容。 + - 显示文件内容:`cat file.txt` + - 连接多个文件内容:`cat file1.txt file2.txt` + - 使用`-n`选项显示行号:`cat -n file.txt` + + + + 2.tac:与`cat`相反,从文件尾部开始显示内容。 + - 从尾部开始,显示文件内容:`tac /var/log/dpkg.log` + - 从尾部开始,显示多个文件内容:`tac file1.txt file2.txt` + + + + + 3.more:分页显示文件内容。 + - 分页显示文件内容:`more file.txt` + - 使用空格键向下翻页,回车键显示下一行,`q`键退出 + 常用于按页查看大文件,但会先一次性加载整个大文件,导致加载变慢 + + + + 4.less:与`more`类似,但提供了更多的导航和搜索功能。 + **无选项**: + - 直接使用 `less 文件名` 来查看文件内容。 + - **-N 或 --line-numbers**: + - 显示行号。 + - **-M 或 --long-prompt**: + - 显示长提示符,包括文件名和行号,百分比。 + - **-MN** 内容中显示行号,底部显示文件名,行号和百分比 + - 两者可以组合使用 + - **-m 或 -i** + - 搜索时忽略大小写 + - **+行数**: + - 打开文件后立即跳转到指定的行数。 + + + 5.head:显示文件开头的内容(默认10行)。 + - 显示文件开头的10行:`head file.txt` + - 显示多个文件开头的10行:`head file1.txt file2.txt` + - 使用`-n`选项指定行数:`head -n 20 file.txt`(显示前20行) + - 使用`-q`不显示文件名。当使用多个文件作为输入时,该选项可以省略文件名前的标题 + + + 6.tail:显示文件结尾的内容(默认10行) + - 显示文件结尾的10行:`tail file.txt` + - 显示多个文件结尾的10行:`tail file1.txt file2.txt` 加-q不显示文件名 + - 使用`-n`选项指定行数:`tail -n 20 file.txt`(显示最后20行) + - 实时查看文件增长(如日志文件):`tail -f logfile.txt` + + + -7.nl:显示文件内容,并添加行号 + - 为文件内容添加行号:`nl file.txt` + - 使用`-s`选项指定分隔符:`nl -s: file.txt`(使用冒号作为行号分隔符) + + + 8.od:以八进制、十进制、十六进制或其他格式转储文件内容。 + - 显示文件内容的八进制表示:`od -An -t o file.txt` + - 显示文件内容的十六进制表示:`od -An -t x1 file.txt` + - 使用`-c`选项显示字符:`od -c file.txt` + +``` + + + + + + +**以下所有操作都在家目录执行:** + +### + +1. **操作题一**:使用 `cat` 命令显示 `/etc/passwd` 文件的内容。 + + ```ht + hhs@hecs-328451:~$ cat /etc/passwd + ``` + + + +2. **操作题二**:将文件 `/etc/passwd` 的内容复制到 `passwd_bak.txt` 文件中,但不使用 `cp` 命令。 + + ```h + hhs@hecs-328451:~$ cat /etc/passwd > passwd_bak.txt + hhs@hecs-328451:~$ cat passwd_bak.txt + ``` + + + +3. **操作题三**:新建两个文件 `file1.txt` 和 `file2.txt`,分别写一些不同的内容,再将这两个文件的内容合并到一个新的文件 `file3.txt` 中。 + + ```ht + hhs@hecs-328451:~$ touch file1.txt file2.txt + hhs@hecs-328451:~$ vim file1.txt + hhs@hecs-328451:~$ vim file2.txt + hhs@hecs-328451:~$ cat file1.txt file2.txt + 红红火火恍恍惚惚 + 嘿嘿嘿嘿嘿 + hhs@hecs-328451:~$ cat file1.txt file2.txt > file3.txt + hhs@hecs-328451:~$ cat file3.txt + 红红火火恍恍惚惚 + 嘿嘿嘿嘿嘿 + ``` + + + +### + +1. 使用命令从尾部开始显示 `bigfile.txt` 文件的内容。 + + ```h + hhs@hecs-328451:~$ touch bigfile.txt + hhs@hecs-328451:~$ vim bigfile.txt + hhs@hecs-328451:~$ tac bigfile.txt + ``` + + + +2. 尝试找出 `bigfile.txt` 文件的最后一行内容,要使用 `tac` 命令。 + + ```html + hhs@hecs-328451:~$ tac bigfile.txt | head -n 1 + ``` + + + +3. 查看 `bigfile.txt` 文件的最后5行内容。 + + ```h + hhs@hecs-328451:~$ tail -n 5 bigfile.txt + ``` + + + +4. 倒序查看 `bigfile.txt` 文件的最后5行内容。 + + ```html + hhs@hecs-328451:~$ tac bigfile.txt |tail -n 5 + ``` + + + +### + +1. **操作题一**:使用 `more` 命令查看 `bigfile.txt` 文件的内容,并在查看过程中使用空格键翻页。 + + ```html + hhs@hecs-328451:~$ more bigfile.txt + ``` + + + +2. **操作题二**:在 `more` 命令的查看过程中,如何使用回车键来逐行查看文件内容? + + ```ht + hhs@hecs-328451:~$ more bigfile.txt + ``` + + + +3. **操作题三**:如何在 `more` 命令中搜索`bigfile.txt`特定字符串(例如 "error")并跳转到下一个匹配项? + + ```ht + hhs@hecs-328451:~$ more bigfile.txt + /error + n + ``` + + + +### `less` 命令操作题 + +1. **操作题一**:使用 `less` 命令查看 `bigfile.txt` 文件,并快速定位到文件的末尾。 + + ```html + hhs@hecs-328451:~$ less bigfile.txt + 快速定位到文件的末尾:shift+G + ``` + + + +2. **操作题二**:在 `less` 命令中,如何向上和向下滚动文件内容? + + ```ht + 使用上下键、Page Up/Page Down键进行滚动,`/`搜索,`n`下一个匹配,`N`上一个匹配,`q`退出。 + ``` + + + +3. **操作题三**:在 `less` 命令中,如何搜索`bigfile.txt`一个特定的函数名(例如 `def my_function`),并查看所有匹配项? + + ```html + hhs@hecs-328451:~$ less bigfile.txt + /def\s+my_function + \s+ 代表一个或多个空格。 + ``` + + + +### `head` 命令操作题 + +1. **操作题一**:使用 `head` 命令显示 `bigfile.txt` 文件的前5行内容。 + + ```html + hhs@hecs-328451:~$ head -n 5 bigfile.txt + ``` + + + +2. **操作题二**:将 `bigfile.txt` 的前20行内容保存到 `bigfile_20.txt` 文件中。 + + ```ht + hhs@hecs-328451:~$ cat bigfile.txt | head -n 20 > bigfile_20.txt + hhs@hecs-328451:~$ cat bigfile_20.txt + ``` + + + +3. **操作题三**:如何结合 `head` 和 `grep` 命令来查找 `bigfile.txt` 文件中以 "A" 开头的前10行? +```html +hhs@hecs-328451:~$ grep "^A" bigfile.txt | head -10 +``` + +### `tail` 命令操作题 + +1. **操作题一**:使用 `tail` 命令显示 `bigfile.txt` 文件的最后20行内容。 +```html +hhs@hecs-328451:~$ tail -n 20 bigfile.txt + +``` +2. **操作题二**:如何实时跟踪一个正在写入的日志文件(如 `bigfile.txt`)的最后10行内容? +```html +hhs@hecs-328451:~$ tail -f 10 bigfile.txt +再开一个连接窗口用来先写入一点点数据 +回到执行命令查看会发现一直出现最新的数字 +``` +3. **操作题三**:在 `tail` 命令中,如何反向显示文件的最后10行内容(即按从旧到新的顺序显示)? +```html +hhs@hecs-328451:~$ tail bigfile.txt +``` + +### 综合题 + +**综合题**:假设你有一个非常大的日志文件 `bigfile.txt`,你需要找出所有包含 "ERROR" 字符串的行,并查看这些行及其之前的两行内容。 +```html +hhs@hecs-328451:~$ grep -B 2 "ERROR" bigfile.txt +``` diff --git "a/27\347\216\213\347\224\237\345\274\230/20240531\347\254\224\350\256\260-sed\345\221\275\344\273\244\347\224\250\346\263\225.md" "b/27\347\216\213\347\224\237\345\274\230/20240531\347\254\224\350\256\260-sed\345\221\275\344\273\244\347\224\250\346\263\225.md" new file mode 100644 index 0000000000000000000000000000000000000000..ab428a8564a1e3c804534eb5534ba4347b72458f --- /dev/null +++ "b/27\347\216\213\347\224\237\345\274\230/20240531\347\254\224\350\256\260-sed\345\221\275\344\273\244\347\224\250\346\263\225.md" @@ -0,0 +1,407 @@ +## 笔记 +```html +sed [选项] '命令' 文件 + +### 常见选项 +- `-e`:直接在命令行模式中执行多个`sed`命令 +- `-f`:从指定的文件中读取`sed`命令 +- `-i`:直接编辑文件内容 +- `-n`:禁止自动打印模式空间内容 + + +### 常见命令 +- `s/regexp/replacement/`:替换匹配的正则表达式 +- `d`:删除行 +- `p`:打印行 +- `a\`:后面追加文本 append +- `i\`:前面插入文本 insert +- `c\`:替换整行文本 + + ```bash + #在文件 example.txt 中每一行的开头插入 "Hello, " + sed 's/^/Hello, /' example.txt + # 在文件 example.txt 中每一行的末尾追加 "!": + sed 's/$/!/' example.txt + ``` + + #### 1. 文本替换 +将文件`example.txt`中的所有`apple`替换为`orange`: +```bash +sed 's/apple/orange/g' example.txt # g 表示全局,所有 +``` +- 示例内容(`example.txt`): + ``` + I have an apple. + She likes apples. + ``` +- 执行后输出: + ``` + I have an orange. + She likes oranges. + ``` + +#### 2. 只替换第一个匹配项 +仅替换每行中的第一个`apple`: +```bash +sed 's/apple/orange/' example.txt # 每行一次 +``` +- 示例内容(`example.txt`): + ``` + apple apple apple + apple apple apple + ``` +- 执行后输出: + ``` + orange apple apple + orange apple apple + ``` + + + + 其它一些用法 + + ```bash + #在文件 example.txt 中每一行的开头插入 "Hello, " + sed 's/^/Hello, /' example.txt + # 在文件 example.txt 中每一行的末尾追加 "!": + sed 's/$/!/' example.txt + ``` + + + +#### 3. 指定行进行替换 +只替换第2行的`apple`: +```bash +sed '2s/apple/orange/' example.txt +# s前加行号 +sed '2s/apple/orange/g' example.txt +# 如果后面还加了g 就是该行的所有被替换 +``` +- 示例内容(`example.txt`): + ``` + apple apple apple + apple apple apple + ``` +- 执行后输出: + ``` + apple apple apple + orange apple apple + ``` + +#### 4. 删除行 +删除包含`apple`的行: +```bash +sed '/apple/d' example.txt +``` +- 示例内容(`example.txt`): + ``` + I have an apple. + She likes apples. + This is a test. + ``` +- 执行后输出: + ``` + This is a test. + ``` + +#### 5. 打印行 print => p +打印包含`apple`的行: +```bash +sed -n '/apple/p' example.txt +``` +- 示例内容(`example.txt`): + ``` + I have an apple. + She likes apples. + This is a test. + ``` +- 执行后输出: + ``` + I have an apple. + She likes apples. + ``` + +#### 6. 插入文本 a行后追加一行, i行前追加一行 +在第2行后插入一行文本`This is inserted.`: +```bash +sed '2a\This is inserted.' example.txt +``` +- 示例内容(`example.txt`): + ``` + Line 1 + Line 2 + Line 3 + ``` +- 执行后输出: + ``` + Line 1 + Line 2 + This is inserted. + Line 3 + ``` + + **以下命令什么效果:** + ```bash + sed '2i\This is inserted.' example.txt + ``` + +#### 7. 替换整行 c +将第2行替换为`This is replaced.`: +```bash +sed '2c\This is replaced.' example.txt +``` +- 示例内容(`example.txt`): + ``` + Line 1 + Line 2 + Line 3 + ``` +- 执行后输出: + ``` + Line 1 + This is replaced. + Line 3 + ``` + +#### 8. ;结合多个命令 +删除包含`apple`的行并将`banana`替换为`grape`: +```bash +sed '/apple/d; s/banana/grape/g' example.txt +``` +- 示例内容(`example.txt`): + ``` + I have an apple. + I have a banana. + She likes apples. + This is a banana. + ``` +- 执行后输出: + ``` + I have a grape. + This is a grape. + ``` + +#### 9. 直接修改文件 +将文件`example.txt`中所有`apple`替换为`orange`并保存更改: +```bash +sed -i 's/apple/orange/g' example.txt +``` +- 示例内容(`example.txt`): + ``` + I have an apple. + She likes apples. + ``` + +## 练习1 + +### 操作题 1 + +1. 使用 `sed` 将文件 `example.txt` 中所有的 "apple" 替换为 "banana",并将结果输出到标准输出。 + + ```ht + hhs@hecs-328451:~$ sed 's/apple/banana/g' example.txt + I have an banana. + She likes bananas. + This is a test. + ``` + + + +2. 使用 `sed` 删除文件 `data.csv` 中所有以字母 "A" 开头的行,并将结果保存到新文件 `clean_data.csv` 中。 + + ```html + hhs@hecs-328451:~$ sed '/^A/d' data.csv >clean_data.csv + ``` + + + +3. 使用 `sed` 在文件 `report.txt` 的每一行开头插入文本 "Line:",并将结果覆盖原始文件。 + + ```html + hhs@hecs-328451:~$ sed -i 's/^/Line /' report.txt + hhs@hecs-328451:~$ cat report.txt + ``` + + + +### 操作题 2 + +1. 使用 `sed` 将文件 `config.ini` 中所有以 "#" 开头的行(注释行)删除,然后将结果输出到标准输出。 + + ```html + hhs@hecs-328451:~$ sed '/^#/d' config.ini + ``` + + + +2. 使用 `sed` 在文件 `story.txt` 中每一行的末尾追加文本 " - The End",并将结果保存到新文件 `story_end.txt` 中。 +```html +hhs@hecs-328451:~$ sed 's/$/- The End/' story.txt > story_end.txt +``` + +3. 使用 `sed` 将文件 `data.log` 中第10行至第20行的内容输出到标准输出。 +```html +hhs@hecs-328451:~$ sed -n '10,20p' data.log +``` + +### 操作题 3 + +1. 使用 `sed` 找到文件 `emails.txt` 中所有包含 "@example.com" 的邮箱地址,并将结果输出到标准输出。 + +```html +hhs@hecs-328451:~$ sed -n '/@example\.com$/p' emails.txt +``` +2. 使用 `sed` 删除文件 `notes.txt` 中的空白行,并将结果保存到新文件 `clean_notes.txt` 中。 +```html +hhs@hecs-328451:~$ sed '/^$/d' notes.txt > clean_note.txt +``` +3. 使用 `sed` 将文件 `book.txt` 中所有的 "color" 替换为 "colour",并将结果输出到标准输出。 +```html +hhs@hecs-328451:~$ sed 's/color/colour/g' book.txt +``` + +## 练习2 + +### exam_text.txt 文件内容: + +``` +This is a text file for practice. +It contains some words like dog, cat, and bird. +There are also numbers like 123 and 456. +# heihei +We will use sed to manipulate this file. +``` + +#### 修改操作 + +1. 使用 `sed` 将文件 `exam_text.txt` 中所有的 "dog" 替换为 "cat",并将结果输出到标准输出。 + +``` +hhs@hecs-328451:~$ sed 's/dog/cat/g' exam_text.txt +``` + +1. 使用 `sed` 将文件 `exam_text.txt` 中所有的 "123" 替换为 "OneTwoThree",并将结果保存到新文件 `updated_exam_text.txt` 中。 + +``` +hhs@hecs-328451:~$ sed 's/123/OneTwoThree/g' exam_text.txt > updated_exam_text.txt +hhs@hecs-328451:~$ cat updated_exam_text.txt +This is a text file for practice. +It contains some words like dog, cat, and bird. +There are also numbers like OneTwoThree and 456. +# heihei +We will use sed to manipulate this file. +``` + +#### 删除操作 + +1. 使用 `sed` 删除文件 `exam_text.txt` 中所有以 "#" 开头的注释行,并将结果输出到标准输出。 + +``` +hhs@hecs-328451:~$ sed '/^#/d' exam_text.txt +``` + +1. 使用 `sed` 删除文件 `exam_text.txt` 中所有包含 "words" 的行,并将结果保存到新文件 `clean_exam_text.txt` 中。 + +``` +hhs@hecs-328451:~$ sed '/words/d' exam_text.txt > clean_exam_text.txt + +``` + +#### 插入操作 + +1. 使用 `sed` 在文件 `exam_text.txt` 的第2行插入一行 "Welcome to sed manipulation",并将结果保存到新文件 `updated_exam_text.txt` 中。 + +``` +hhs@hecs-328451:~$ sed -i '2i Welcome to sed manipulation' updated_exam_text.txt +``` + +1. 使用 `sed` 在文件 `exam_text.txt` 的`numbers`所在行插入一行 "This is a new line",并将结果输出到标准输出。 + +``` +hhs@hecs-328451:~$ sed '/number/a "This is a new line"' exam_text.txt +``` + +#### 追加操作 + +1. 使用 `sed` 在文件 `exam_text.txt` 的末尾追加一行 "End of practice",并将结果保存到新文件 `updated_exam_text.txt` 中。 + +``` +hhs@hecs-328451:~$ sed -i '$aEnd of practice' updated_exam_text.txt +``` + +1. 使用 `sed` 在文件 `exam_text.txt` 的每一行末尾追加内容 " - 2024-05-31",并将结果输出到标准输出。 + +``` +hhs@hecs-328451:~$ sed 's/$/ - 2024-05-31/' exam_text.txt +``` + +#### 整行替换操作 + +1. 使用 `sed` 将文件 `exam_text.txt` 中所有以字母 "W" 开头的行替换为 "Not Available",并将结果输出到标准输出。 + +``` +hhs@hecs-328451:~$ sed 's/^W.*/Not Available/' exam_text.txt +``` + +1. 使用 `sed` 将文件 `exam_text.txt` 中所有包含 "words" 的行替换为 "Replaced",并将结果保存到新文件 `updated_exam_text.txt` 中。 + +``` +hhs@hecs-328451:~$ sed -i '/words/c\Replace' updated_exam_text.txt + +``` + +#### 多命令操作 + +1. 使用 `sed` 删除文件 `exam_text.txt` 中`dog`所在行,`file`换成`文件`,并将结果输出到标准输出。 + +``` +hhs@hecs-328451:~$ sed -e '/dog/d' -e 's/file/文件/g' exam_text.txt + +``` + +1. 使用 `sed` 将文件 `exam_text.txt` 中,删除空白行并将所有以 "It" 开头的行替换为 "This", + +``` +hhs@hecs-328451:~$ sed -e '/^$/d' -e 's/^It/This/g' exam_text.txt +``` + +#### 脚本文件操作 + +1. 创建一个 `sed` 脚本文件 `replace_apple_with_orange.sed`,实现将文件 `exam_text.txt` 中所有 "apple" 替换为 "orange" 的功能,并将结果输出到标准输出。 + +``` +hhs@hecs-328451:~$ nano replace_apple_with_orange.sed +hhs@hecs-328451:~$ sed -f replace_apple_with_orange.sed exam_text.txt + +``` + +运行脚本: + +``` +This is a text file for practice. +It contains some words like dog, cat, and bird. +There are also numbers like 123 and 456. +# heihei orange +We will use sed to manipulate this file. +``` + +1. 创建一个 `sed` 脚本文件 `remove_blank_lines.sed`,实现删除文件 `exam_text.txt` 中所有空白行的功能,并将结果保存到新文件 `cleaned_exam_text.txt` 中。 + +``` +hhs@hecs-328451:~$ sed -f remove_blank_lines.sed exam_text.txt > cleaned_exam_text +hhs@hecs-328451:~$ cat cleaned_exam_text + +``` + +运行脚本: + +``` +This is a text file for practice. +It contains some words like dog, cat, and bird. +There are also numbers like 123 and 456. +# heihei apple +We will use sed to manipulate this file. + +``` + +现在题目已经更新了,你可以根据需要使用 `-e` 选项执行多个编辑命令。如果有任何问题,请随时告诉我。 \ No newline at end of file diff --git "a/27\347\216\213\347\224\237\345\274\230/20240604\347\254\224\350\256\260-awk\345\221\275\344\273\244\347\232\204\347\224\250\346\263\225.md" "b/27\347\216\213\347\224\237\345\274\230/20240604\347\254\224\350\256\260-awk\345\221\275\344\273\244\347\232\204\347\224\250\346\263\225.md" new file mode 100644 index 0000000000000000000000000000000000000000..f0ea2e830ac1f50589c7d5980396bf91faa4b33f --- /dev/null +++ "b/27\347\216\213\347\224\237\345\274\230/20240604\347\254\224\350\256\260-awk\345\221\275\344\273\244\347\232\204\347\224\250\346\263\225.md" @@ -0,0 +1,66 @@ +## 笔记 +1.awk是一种编程语言 +2.awk[选项] '脚本'文件 + +3.选项: + - -F 指定原文件的分隔符 + - -f 执行脚本文件 + - -v 自定义一个类变量 + +4.脚本-语句块 + - 开始语句块 + 1.模式:BEGIN + 2.动作:{FS=","},设置原文件分隔符 + {print"------"} + {name="xx"} + - 通用语句块 + - 结尾语句块 + +5.内置的变量 + - NR:Numbers of Rows + 总行数(在具体的模式中表示当前行号) + - NF:Numbers of files + 总行列-$NF-最后一列 + - $0:表示一整行 + - $1,$2... - $数字-数字表示第几列 + - FS=原文分隔符 + - OFS=输出的分隔符 +## 作业: + +1. 只显示/etc/passwd的账户 + + ```html + mmm@hecs-328451:~$ awk -F':' '{print $1}' /etc/passwd + ``` +2. 只显示/etc/passwd的账户和对应的shell,并在第一行上添加列名用户制表符shell,最后一行添加---------------- + + ```html + mmm@hecs-328451:~$ awk -F':' 'BEGIN{print "用户","shell"}{print $1 "," $7}END{print "-----------"}' /etc/passwd + ``` +3. 搜索/etc/passwd有关键字root的所有行 + ```html + mmm@hecs-328451:~$ awk -F':' '/root/' /etc/passwd + ``` +4. 统计/etc/passwd文件中,每行的行号,每列的列数,对应的完整行内容以制表符分隔 +```html +mmm@hecs-328451:~$ awk -F':' '{print NR,NF,$0}' /etc/passwd | sed 's/ /\\t/g' +``` + +5. 输出/etc/passwd文件中以nologin结尾的行 +```html +mmm@hecs-328451:~$ awk -F':' '$7~/nologin$/' /etc/passwd +``` + +6. 输出/etc/passwd文件中uid字段小于100的行 + +```html +mmm@hecs-328451:~$ awk -F':' '$3<100 {print}' /etc/passwd +``` +7. /etc/passwd文件中gid字段大于200的,输出该行第一、第四字段,第一,第四字段并以制表符分隔 +```html +mmm@hecs-328451:~$ awk -F':' 'BEGIN{OFS="\t"}$4>200{print $1,$4}' /etc/passwd +``` +8. 输出/etc/passwd文件中uid字段大于等于100的行 +```html +mmm@hecs-328451:~$ awk -F':' '$3>=100{print}' /etc/passwd +``` \ No newline at end of file diff --git "a/27\347\216\213\347\224\237\345\274\230/20240605\347\254\224\350\256\260-vim\347\232\204\345\221\275\344\273\244\344\275\277\347\224\250.md" "b/27\347\216\213\347\224\237\345\274\230/20240605\347\254\224\350\256\260-vim\347\232\204\345\221\275\344\273\244\344\275\277\347\224\250.md" new file mode 100644 index 0000000000000000000000000000000000000000..f50aebfddb8935d09ea542901af8395ba99bdd03 --- /dev/null +++ "b/27\347\216\213\347\224\237\345\274\230/20240605\347\254\224\350\256\260-vim\347\232\204\345\221\275\344\273\244\344\275\277\347\224\250.md" @@ -0,0 +1,100 @@ +# vim + +```html +命令模式 +i -- 切换到输入模式,在光标当前位置开始输入文本。 +x -- 删除当前光标所在处的字符。 +: -- 切换到底线命令模式,以在最底一行输入命令。 +a -- 进入插入模式,在光标下一个位置开始输入文本。 +o:在当前行的下方插入一个新行,并进入插入模式。 +O -- 在当前行的上方插入一个新行,并进入插入模式。 +dd -- 剪切当前行。 +yy -- 复制当前行。 +p(小写) -- 粘贴剪贴板内容到光标下方。 +P(大写)-- 粘贴剪贴板内容到光标上方。 +u -- 撤销上一次操作。 +Ctrl + r -- 重做上一次撤销的操作。 +:w -- 保存文件。 +:q -- 退出 Vim 编辑器。 +:q! -- 强制退出Vim 编辑器,不保存修改。 + +``` + +```html +输入模式 +字符按键以及Shift组合,输入字符 +ENTER,回车键,换行 +BACK SPACE,退格键,删除光标前一个字符 +DEL,删除键,删除光标后一个字符 +方向键,在文本中移动光标 +HOME/END,移动光标到行首/行尾 +Page Up/Page Down,上/下翻页 +Insert,切换光标为输入/替换模式,光标将变成竖线/下划线 +ESC,退出输入模式,切换到命令模式 +``` + +```html +底线命令模式 +w:保存文件。 +:q:退出 Vim 编辑器。 +:wq:保存文件并退出 Vim 编辑器。 +:q!:强制退出Vim编辑器,不保存修改。 + +``` + +```html +set nu 显示行号,设定之后,会在每一行的前缀显示该行的行号 +:set nonu 与 set nu 相反,为取消行号! +``` + +1. vi 编辑器有几种模式? + + ```html + 三种(命令模式,输入模式,命令行模式) + ``` + + + +2. 如何进入 vi 编辑器的插入模式 + + ```html + 在命令模式下按下i就进入输入模式,使用Esc键可以返回普通模式 + ``` + + + +3. 如何进入 vi 编辑器的可视化模式 + + ```html + 1.字符模式,在命令模式中按v键进入 + 2.行模式,在命令模式中按V键进入 + 3.块模式,在命令模式中按Ctrl+ v键进入 + ``` + + + +4. 在 vi 编辑器中如何复制一行 + + ```ht + yy 复制整行到粘贴板 + ``` + + + +5. 在 vi 编辑器中如何进行粘贴 + + ```ht + 1.p 在当前位置后粘贴 + 2.P 在当前位置前粘贴 + ``` + + + +6. 如何删除从 3 行到 15 行的所有数据 + + ```html + 按一下ESC键,确保退出编辑模式 + :3,15d + ``` + + \ No newline at end of file diff --git "a/27\347\216\213\347\224\237\345\274\230/20240607\347\254\224\350\256\260-SSL\350\257\201\344\271\246\347\232\204\350\216\267\345\217\226\344\270\216\351\203\250\347\275\262.md" "b/27\347\216\213\347\224\237\345\274\230/20240607\347\254\224\350\256\260-SSL\350\257\201\344\271\246\347\232\204\350\216\267\345\217\226\344\270\216\351\203\250\347\275\262.md" new file mode 100644 index 0000000000000000000000000000000000000000..07256373f58cc485c4de0ef1fee1304d72c20053 --- /dev/null +++ "b/27\347\216\213\347\224\237\345\274\230/20240607\347\254\224\350\256\260-SSL\350\257\201\344\271\246\347\232\204\350\216\267\345\217\226\344\270\216\351\203\250\347\275\262.md" @@ -0,0 +1,67 @@ +## SSL证书的获取与部署 + +```html +一、ssl证书的获取: + +购买证书 +有免费版,到时间就需要申请 +(每个阿里云账号一年内均可申请20个免费SSL证书) +“SSL证书”–“免费证书”–“立即购买” +创建证书 +SSL证书”–“免费证书”–“创建证书 +申请证书 +证书申请表单填写 +解析域名 +如果是自动DNS验证,直接点击验证即可;如果是手动DNS验证,需要到域名注册商处,为域名添加TXT记录类型的域名解析 +下载证书包(两个文件) +阿里云SSL证书支持多种服务器类型,如:Tomcat、Apache、Nginx、IIS、JKS、其他及根证书下载 +根据服务器类型,选择对应的SSL证书下载即可 +``` + +```html +二、ssl证书的部署: + +在nginx里的/etc/nginx/conf.d路径下创建文件夹 + +在命令终端里,把本地下载好的证书包,上传到nginx里 + +找到装载配置信息的文件夹,并编辑它 + +更改域名(默认有两个,一个带www的) +更改两个证书文件的路径(文件在事先准备好的文件里) +更改root html(映射),因为默认进入nginx页面 + +``` + +```html +server { + #HTTPS的默认访问端口443。 + listen 443 ssl; + + #填写证书绑定的域名 + server_name ; + + #填写证书文件绝对路径 + ssl_certificate cert/.pem; + #填写证书私钥文件绝对路径 + ssl_certificate_key cert/.key; + #表示优先使用服务端加密套件。默认开启 + ssl_prefer_server_ciphers on; + location / { + root html#更改四 + index index.html index.htm; + } +} +``` + +```html +检测信息更改是否成功: +nginx -t +# 会出现is ok +# 否则弹出报错的地方 +``` + +```html +重新启动 Nginx 服务,使配置文件生效。可以运行以下命令来重新启动 Nginx 服务: +nginx -s reload # 会出现started +``` \ No newline at end of file diff --git "a/27\347\216\213\347\224\237\345\274\230/20240611\347\254\224\350\256\260-\345\210\232\345\256\211\350\243\205\345\256\214Debian7\346\255\245\351\252\244.md" "b/27\347\216\213\347\224\237\345\274\230/20240611\347\254\224\350\256\260-\345\210\232\345\256\211\350\243\205\345\256\214Debian7\346\255\245\351\252\244.md" new file mode 100644 index 0000000000000000000000000000000000000000..b869bf0cf3b37749a496e6d0abcb866a5142bfae --- /dev/null +++ "b/27\347\216\213\347\224\237\345\274\230/20240611\347\254\224\350\256\260-\345\210\232\345\256\211\350\243\205\345\256\214Debian7\346\255\245\351\252\244.md" @@ -0,0 +1,88 @@ +# 刚安装完Debian7步骤 + +```html +1.设置国内软件源镜像:/etc//apt/sources.list +2.更新软件包列表:apt update +3.更新系统:apt upgrade -y +4.安装vim:apt install vim -y +5.编辑网卡配置,设置静态IP: +vim /etc/network/interfaces + +重新启动网络服务: +sudo systemctl restart networking +``` + +```html +常用命令; + +1.time(用于测量命令的执行时间) +- real:总的时间(墙上时钟时间)。 +- user:用户态 CPU 时间。 +- sys:内核态 CPU 时间。 + +2.date(显示或设置系统日期和时间) +- %Y:四位数的年份。 +- %m:两位数的月份。 +- %:两位数的日期。 + +3.timedatectl(查看和设置系统时间和日期,时区和 NTP(网络时间协议)设置) +- NTP:网络时间协议,用于同步时间。 +- RTC:实时时钟。 + +4.reboot(重新启动系统) +- systemd:系统和服务管理器。 + +5.poweroff(关闭系统电源) +- halt:停止系统所有的 CPU 功能。 + +6.wget(从网络上下载文件) +- URL:统一资源定位符。 +- HTTP/HTTPS:超文本传输协议。 + +7.curl(从网络上获取或发送数据) +- URL:统一资源定位符。 +- GET/POST:HTTP 请求方法。 + +8.ps(查看当前运行的进程) +- PID:进程标识符。 +- TTY:终端类型。 + +9.kill(向进程发送信号(通常用于终止进程)) +- SIGTERM:请求中止进程。 +- SIGKILL:强制终止进程。 + +10.ip(显示和操作网络接口和路由) +- address:IP 地址。 +- route:路由信息。 + +11.ss(显示套接字统计信息。旧版是netstat) +- TCP:传输控制协议。 +- UDP:用户数据报协议。 + +12.uname(显示系统信息) +- kernel:操作系统内核。 +- OS:操作系统。 + +13.uptime(显示系统运行时间和负载) +- load average:系统平均负载 + +14.who(显示当前登录用户信息) +- login:用户登录信息。 +- TTY:终端类型。 + +15.last(显示系统上最近的登录信息) +- wtmp:记录登录和注销事件的文件。 + +16.ping(测试网络连通性) +- ICMP:互联网控制消息协议。 +- echo request:回显请求。 + +17.traceroute(显示到达网络主机的路径) +- hop:从一个网络节点到另一个的跳转。 +- TTL:生存时间。 + +18.history(显示命令历史记录) +- bash history:记录用户输入的命令历史。 + +``` + diff --git "a/27\347\216\213\347\224\237\345\274\230/20240612\347\254\224\350\256\260-\347\211\271\346\256\212\347\254\246\345\217\267.md" "b/27\347\216\213\347\224\237\345\274\230/20240612\347\254\224\350\256\260-\347\211\271\346\256\212\347\254\246\345\217\267.md" new file mode 100644 index 0000000000000000000000000000000000000000..1e7938cef7a644afc1fce928b152a14d8e053f46 --- /dev/null +++ "b/27\347\216\213\347\224\237\345\274\230/20240612\347\254\224\350\256\260-\347\211\271\346\256\212\347\254\246\345\217\267.md" @@ -0,0 +1,53 @@ +# 特殊符号 + +```htm +重定向: +>(覆盖重定向):将输出重定向到文件(覆盖) +>>(输出追加重定向):将标准输出追加到文件末尾 +<(输入重定向):将文件内容作为标准输入 +2>:将错误输出重定向到文件 +2>>:将错误输出追加到文件 +&>:将标准和错误都输出到重定向 +&>>:将标准和错误输出追加到重定向 +|:将左边的输出右边的输入 +<>:先输入,后输出 +2> /dev/null:将错误信息丢掉不显示 +``` + +```html +0:标准输入(stdin):键盘输入 +1:标准输出(stdout):直接显示在屏幕,不包含错误信息 +2:标准错误(stderr):只包含错误信息 +``` + +```html +1.<< 是什么功能?here document + 答:<<(双小于号,后跟分界线) + 它允许直接在命令行或脚本中定义一大段文本 + 然后将其作为命令的标准输入。分界线可以是 + 任意不包含空格的字符串,通常是一个单词或者 + 一对引号 + +2.()把命令包括起来,是干什么功能? + 答:命令组。 + 括号中的命令将会新开一个子shell顺序执行, + 所以括号中的变量不能够被脚本余下的部分使用 + 括号中多个命令之间用分号隔开,最后一个命令 + 可以没有分号,各命令和括号之间不必有空格 +``` + +```ht +1.tee是一个在Unix和类Unix操作系统上非常有用的命令行实用程序。 + 它的作用是从标准输入中读取数据,并同时将数据输出到标准输出和 + 一个或多个文件中。 + +2.选项: + -a:追加写入文件而不是覆盖 + -i:忽略中断信号 + --help:显示帮助信息和选项列表 + --version:显示版本信息 + +3.tee命令是一个灵活而强大的工具,可以用于在命令行中处理数据流 + 同时向多个文件写入数据 +``` + diff --git "a/27\347\216\213\347\224\237\345\274\230/20240614\347\254\224\350\256\260-\347\224\250\346\210\267\344\270\216\346\235\203\351\231\220.md" "b/27\347\216\213\347\224\237\345\274\230/20240614\347\254\224\350\256\260-\347\224\250\346\210\267\344\270\216\346\235\203\351\231\220.md" new file mode 100644 index 0000000000000000000000000000000000000000..c5b949f3e985cb1b3253f8b0a8f6bc13a58f7da8 --- /dev/null +++ "b/27\347\216\213\347\224\237\345\274\230/20240614\347\254\224\350\256\260-\347\224\250\346\210\267\344\270\216\346\235\203\351\231\220.md" @@ -0,0 +1,312 @@ +# 用户与权限 +一、用户与用户组 +```html + +1.用户管理 + - 添加用户 + (1).useradd 原生底层命令 + (2).adduser 加强版命令 + + - 删除用户 + (1).userdel -r 删除用户和家目录和邮件目录 + + - 修改用户 + (1).usermod + (2).usermod -G xxx 将xxx用户修改为.... + (3).usermod -aG xxx 给xxx用户追加一个附加组 + + - 查看用户信息 + (1).id 用户名 + (2).passwd + + - passwd+用户名:给这个用户名设置密码 + + +2.用户组管理 + - 添加用户组 + (1).groupadd + + - 删除用户组 + (1).groupdel + + - 修改用户组 + (1).groupmod +``` + +二、文件与目录和权限 +```html +1.基本权限 + (1).读:read,简写r:对于文件,是可以读取文件内容 + 对于目录,是可以浏览目录信息 + + (2).写:write,简写w:对于文件,是可以修改文件的内容 + 对于目录,可以在里面创建,修改,删除文件或目录 + + (3).执行:execute,简写x.对于文件,执行该文件,对于 + 目录,是可以进入该目录 + +2.权限的表达方式 + (1).字符:r w x + + (2).数字:4 读 r + 2 写 w + 1 执行 x + +3.权限管理的命令 + (1).修改权限 + - chmod + 数字权限表示 + 文件名 + - 例:chmod 777 1.txt + + - chmod + 字符权限表示 + 文件名 + - 例:chmod u=rx,g=wx 1.txt + + (2). + - u:拥有者 + - g:拥有者所在的组 + - o:除以上两者外的其他人 + - a:所有人 + - +:不改变原有权限基础上添加新权限 + - —:不改变原有权限基础上删除某权限 + - =:覆盖所有旧权限 + +4.修改拥有者 + (1).chown + +5.修改拥有者的组 + (1).chgrp + +6.通用的一个常用的选项 -R 递归设置权限 + +``` + + + + + +### 练习 + +### 权限管理练习 + +1. 创建/guanli 目录,在/guanli下创建zonghe 和 jishu 两个目录(一条命令) + +```html + hhs@hecs-328451:~$ sudo mkdir -p /guanli/{zonghe,jishu} + ``` + + + +2. 添加组帐号zonghe、caiwu、jishu,GID号分别设置为2001、2002、2003 + + ```html + hhs@hecs-328451:~$ sudo groupadd -g 2001 zonghe + hhs@hecs-328451:~$ sudo groupadd -g 2002 caiwu + hhs@hecs-328451:~$ sudo groupadd -g 2003 jishu + hhs@hecs-328451:~$ cat /etc/group + ``` + + + +3. 创建jerry、kylin、tsengia、obama用户,其中的kylin用户帐号在2020年12月30日后失效 + + ```html + hhs@hecs-328451:~$ sudo useradd jerry + hhs@hecs-328451:~$ sudo useradd -e 2020-12-30 kylin + hhs@hecs-328451:~$ sudo useradd tsengia + hhs@hecs-328451:~$ sudo useradd obama + hhs@hecs-328451:~$ cat /etc/passwd +``` + + + +4. 将jerry、kylin、tsengia、obama等用户添加到zonghe组内 + +```html +hhs@hecs-328451:~$ sudo usermod -aG zonghe jerry +hhs@hecs-328451:~$ sudo usermod -aG zonghe kylin +hhs@hecs-328451:~$ sudo usermod -aG zonghe tsengia +hhs@hecs-328451:~$ sudo usermod -aG zonghe obama +hhs@hecs-328451:~$ cat /etc/group +``` + + + +5. 创建handy、cucci用户,其中cucci帐号的登录Shell设置为“/sbin/nologin” + +```html +hhs@hecs-328451:~$ sudo useradd handy +hhs@hecs-328451:~$ sudo useradd -s /sbin/nologin cucci +hhs@hecs-328451:~$ cat /etc/passwd +``` + + + +6. 将handy、cucci等用户添加到jishu组内 + +```html +hhs@hecs-328451:~$ sudo usermod -aG jishu handy +hhs@hecs-328451:~$ sudo usermod -aG jishu cucci +hhs@hecs-328451:~$ cat /etc/group +``` + + + +7. 将上述的所有用户均要求加入到guanli组内 + +```html +hhs@hecs-328451:~$ sudo groupadd guanli +hhs@hecs-328451:~$ sudo gpasswd -M jerry,kylin,tsengia,obama,handy,cucci guanli +``` + + + +8. 将zonghe组内的obama用户删除 + + ```html +hhs@hecs-328451:~$ sudo gpasswd -d obama zonghe +Removing user obama from group zonghe +``` + + + +9. 为jerry用户设置密码为“123456”(使用普通方法)为cucci用户设置密码为“redhat” + +```html +hhs@hecs-328451:~$ sudo passwd jerry +New password: +Retype new password: +passwd: password updated successfully + ``` + ```html +hhs@hecs-328451:~$ sudo passwd cucci +New password: +Retype new password: +passwd: password updated successfully + + ``` + + + +10. 将jerry用户锁定,并查看锁定状态 + +```html +hhs@hecs-328451:~$ sudo passwd -l jerry +passwd: password expiry information changed. +hhs@hecs-328451:~$ sudo passwd -S jerry +jerry L 06/14/2024 0 99999 7 -1 + + ``` + + + +11. 打开两个xshell窗口,通过(who 或者 w)命令查看连接状态,并通过fuser杀掉其中一个 + +```html +hhs@hecs-328451:~$ sudo apt-get update +hhs@hecs-328451:~$ sudo apt-get install psmisc +hhs@hecs-328451:~$ who +hhs@hecs-328451:~$ fuser -k /dev/pts/1 +/dev/pts/1: 256429 256473 + +``` + + + +12. 查看cucci用户,属于那些组,并查看其详细信息 + + ```html +hhs@hecs-328451:~$ groups cucci + +hhs@hecs-328451:~$ sudo apt-get update +hhs@hecs-328451:~$ sudo apt-get install finger +hhs@hecs-328451:~$ finger cucci + ``` + + + +13. 手工创建账号student(预留) + +```html +hhs@hecs-328451:~$ sudo useradd student +hhs@hecs-328451:~$ sudo passwd student + + ``` + + + +14. 设置权限及归属:/guanli目录属组设为guanli, /guanli/zonghe目录的属组设为zonghe /guanli/jishu目录的属组设为jishu,设置3个目录都是禁止其他用户访问的权限 + ```html +hhs@hecs-328451:~$ sudo chown .guanli /guanli +hhs@hecs-328451:~$ sudo chown .zonghe /guanli/zonghe/ +hhs@hecs-328451:~$ sudo chown .jishu /guanli/jishu/ +hhs@hecs-328451:~$ sudo chmod -R a-r /guanli +hhs@hecs-328451:~$ ls -l /guanli/ + ``` + + + +15. 建立公共目录/ceshi允许技术组内的所有用户读取、写入、执行文件, 禁止其他用户读、写、执行 + +```html +hhs@hecs-328451:~$ sudo mkdir /ceshi +hhs@hecs-328451:~$ sudo chown .jishu /ceshi/ +hhs@hecs-328451:~$ sudo chmod 770 /ceshi/ +hhs@hecs-328451:~$ ls -ld /ceshi +drwxrwx--- 2 root jishu 4096 Jun 14 18:36 /ceshi + + ``` + + + +16. 清除jerry用户密码 + + ```html +hhs@hecs-328451:~$ sudo passwd -d jerry +passwd: password expiry information changed. + ``` + + + +17. 锁定cucci用户密码并查看状态 + + ```html +hhs@hecs-328451:~$ sudo passwd -l cucci +passwd: password expiry information changed. +hhs@hecs-328451:~$ sudo passwd -S cucci +cucci L 06/14/2024 0 99999 7 -1 + ``` + + + +18. 修改obama用户的UID为8888 + ```html +hhs@hecs-328451:~$ id obama +uid=1020(obama) gid=1020(obama) groups=1020(obama),2004(guanli) +hhs@hecs-328451:~$ sudo usermod -u 8888 obama +hhs@hecs-328451:~$ id obama +uid=8888(obama) gid=1020(obama) groups=1020(obama),2004(guanli) + ``` + + + +19. 通过passwd命令修改kylin用户的最长密码使用期限为60天 + +```html +hhs@hecs-328451:~$ sudo passwd -x 60 kylin +hhs@hecs-328451:~$ sudo cat /etc/shadow |grep "kylin" +kylin:!:19888:0:60:7::18626: +``` + + + + + +20. 通过id groups等命令查看用户handy信息 + +```html +hhs@hecs-328451:~$ sudo id handy +uid=1021(handy) gid=1021(handy) groups=1021(handy),2003(jishu),2004(guanli) +hhs@hecs-328451:~$ sudo groups handy +handy : handy jishu guanli +``` + + \ No newline at end of file diff --git "a/27\347\216\213\347\224\237\345\274\230/20240618\347\254\224\350\256\260-\347\216\257\345\242\203\345\217\230\351\207\217.md" "b/27\347\216\213\347\224\237\345\274\230/20240618\347\254\224\350\256\260-\347\216\257\345\242\203\345\217\230\351\207\217.md" new file mode 100644 index 0000000000000000000000000000000000000000..61ddde675add21ace1079f1f7c7ae3c9369bf0da --- /dev/null +++ "b/27\347\216\213\347\224\237\345\274\230/20240618\347\254\224\350\256\260-\347\216\257\345\242\203\345\217\230\351\207\217.md" @@ -0,0 +1,175 @@ +# 环境变量 + +```html +概念:就是提供一系列可供特殊环境的全局变量 +``` + +```html +1.按生命周期分: + (1).永久环境变量,写在指定类型的配置文件,永不失效 ~/.bashrc/etc/profile.d/xxx.sh + (2).临时环境变量,临时使用export命令设置的环境变量,退出终端即失效 + +2.按作用域分 + (1).系统级环境变量,对所有用户生效 /etc/profile.d/xxx.sh + (2).用户级环境变量,只对当前用户生效 ~/.bashrc +``` + +```html +3.常用系统级环境变量 + **系统级** +(1).PATH 可执行文件的搜索路径 +(2).LANG 定义系统的语言环境和字符集, export LANG=zh_CN.utf8,编辑文件不乱码 +(3).LANGUAGE 用于设置消息语言的优先级 ,提示语言为中文 +(4).EDITOR 默认文本编辑器 editor + +**用户级** +(1).HOME`:当前用户的主目录。 +(2).USER:当前用户名。 +(3).UID:当前用户的用户ID。 +(4).SHELL:当前用户的默认Shell。 +``` + +```html +4.系统启用 + (1)./etc/environment 加载系统级的环境变量,不管有无登录 + (2)./etc/profile 系统被登录,就加载,任何人登录 + (3).~/.profile 具体哪个人登录后,加载自己目录下的文件 + (4).~/.bashrc 加载对就的shell的配置文件,类似.zshrc .xshrc + +``` + +```html +5.一个命令被执行经过几步 + (1).先判断是不是绝对路径:是就执行,不是就下一步 + (2).判断是不是别名,是别名就执行别名对应的命令,不是就下一步 + (3).从环境变量PATH值对应的路径中找,有没有这个命令,找到了就从那个路径去执行,找不到就下一步 + (4).报错了,找不到该命令 + + +``` + +## 练习 + +### 练习题 1: 显示当前所有的环境变量 + +* 使用`printenv`或`env`命令来显示所有的环境变量。 + + +```bash +printenv或env +``` +### 练习题 2: 显示`HOME`环境变量的值 + +* 使用`echo`命令和`$`符号来显示`HOME`环境变量的值。 + + +```bash +echo $HOME +/home/hhs +``` +### 练习题 3: 临时设置一个新的环境变量 + +* 设置一个名为`MY_AGE`的环境变量,并将其值设置为`18`。 + + +```bash +hhs@hecs-328451:~$ export MY_AGE=18 +hhs@hecs-328451:~$ echo $MY_AGE +18 +``` +### 练习题 4: 显示新设置的环境变量 + +* 使用`echo`命令来显示`MY_AGE`的值。 + +```bash +echo $MY_AGE +``` +### 练习题 5: 在新的shell会话中检查环境变量 + +* 打开一个新的终端窗口或标签页,并尝试显示`MY_AGE`的值。你会看到什么?为什么? +```bash +hhs@hecs-328451:~$ echo $MY_AGE + +hhs@hecs-328451:~$ +会看到为空。因为这种设置是临时的,仅适用于当前会话。一旦关闭shell或注销用户,设置的环境变量将不再有效。 +``` + +### 练习题 6: 修改`PATH`环境变量 + +* 将`你当前用户的家目录`添加到你的`PATH`环境变量的末尾位置 + + +```bash +export PATH="$PATH:/home" +source ~/.bashrc +``` +将`/tmp`添加到你的`PATH`环境变量的开始位置,(注意:这可能会覆盖其他路径中的同名命令,所以请谨慎操作)。 + +```bash +export PATH=/tmp:$PATH +hhs@hecs-328451:~$ echo $PATH +/tmp:/home/hhs/bin:/home/hhs/bin:/home/hhs/bin:/usr/local/bin:/usr/bin:/bin:/usr/local/games:/usr/games + +``` + +### 练习题 7: 验证`PATH`的修改 + +* 使用`echo`命令显示`PATH`的值,并确认`前面添加的目录`已经被添加到对应位置。 + + +```bash +echo PATH +/tmp:/usr/local/bin:/usr/bin:/bin:/usr/local/games:/usr/games:/home/hhs:/home + +``` +### 练习题 8: 永久设置环境变量 + +* 在你的shell配置文件中(如`~/.bashrc`、`~/.bash_profile`、`~/.zshrc`等,取决于你使用的shell和配置)添加一行来永久设置`MY_NAME`,值设置为`奥德彪`。 + +例如,对于bash shell,你可以使用: + + +```bash +export MY_NAME="奥德彪" +echo $MY_NAME +``` +如何让`MY_NAME`生效,并验证 + +``` +hhs@hecs-328451:~$ source ~/.bashrc +奥德彪 +``` + +### 练习题 9: 清理 + +* 清除你之前设置的`MY_AGE`和`PATH`的修改(如果你不想永久保留它们)。 + + +```bash +unset MY_AGE +unset PATH +``` +### 练习题 10: 修改默认器 + +* 使用`EDITOR`变量,修改你默认的编辑器为nano。 + + +```bash +export EDITOR=nano +``` + +### 练习题 11: 修改语言 + +* 使用`LANG`变量,让你的文件支持中文和utf8编码来避免乱码。 + +```bash +export LANG=zh_CN.UTF-8 +``` + +- 使用`LANGUAGE`变量,让你的命令提示为中文 + +```bash +export LANGUAGE=zh_CN +``` + + diff --git "a/27\347\216\213\347\224\237\345\274\230/20240619\347\254\224\350\256\260-\350\277\233\347\250\213\347\256\241\347\220\206.md" "b/27\347\216\213\347\224\237\345\274\230/20240619\347\254\224\350\256\260-\350\277\233\347\250\213\347\256\241\347\220\206.md" new file mode 100644 index 0000000000000000000000000000000000000000..80763cc21867b51aa47b2267a6ba47c8d6a0f0da --- /dev/null +++ "b/27\347\216\213\347\224\237\345\274\230/20240619\347\254\224\350\256\260-\350\277\233\347\250\213\347\256\241\347\220\206.md" @@ -0,0 +1,33 @@ +# 进程管理 + +```html +1.ps - ef | grep - 列出需要进程 + ps -aux - 显示进程信息,如无终端的(x)和针对用户(u)的进程:如USER,PID,%CPU等 + ps -e --forest 以树形结构显示进程关系 +2.pstree +3.top:实时显示系统中各个进程的资源占用情况 +4.htop:比top更高级的进程查看器,提供颜色编码和更直观的界面 +5.pkill:根据进程名杀死进程 +6.pgrep:查看匹配条件的进程。例如,pgrep -f httpd可以查找命令行中包含httpd的进程 +7.jobs:显示当前shell会话中的作业状态 +8.bg:将一个暂停(stopped)的作业放在后台执行 +9.fg:将后台作业调至前台并继续执行 +10.nohup:在用户注销后继续运行命令 +``` + +```html +服务管理 +1.systemctl + - 启动 systemctl start 服务名 + - 停止 systemctl stop 服务名 + - 重启 systemctl restart 服务名 + - 查看 systemctl status 服务名 + - 重新加载配置文件 systemctl reload 服务名 + +2.service + - 启动 service 服务名 start + - 停止 service 服务名 stop + - 重启 service 服务名 restart + - 查看 service 服务名 status +``` + diff --git "a/27\347\216\213\347\224\237\345\274\230/\347\233\270\345\257\271\350\267\257\345\276\204\347\273\235\345\257\271\350\267\257\345\276\204.md" "b/27\347\216\213\347\224\237\345\274\230/\347\233\270\345\257\271\350\267\257\345\276\204\347\273\235\345\257\271\350\267\257\345\276\204.md" new file mode 100644 index 0000000000000000000000000000000000000000..8e3fb770a805ba7d6ef166103bb57e1ea97bfbe6 --- /dev/null +++ "b/27\347\216\213\347\224\237\345\274\230/\347\233\270\345\257\271\350\267\257\345\276\204\347\273\235\345\257\271\350\267\257\345\276\204.md" @@ -0,0 +1,9 @@ +- 将目录/docs/report.txt移动到家目录/archive中,但如果目标目录中已存在同名文件,则不直接覆盖,先备份同名文件为report.txt_bak。 + +```sh +mv -b docs/report.txt archive/report.txt_bak +mv -bS _bak docs/report.txt archive/ +``` + +- 将家目录/docs中所有以.pdf结尾的文件复制到家目录/pdf_files中,并且如果目标目录中已存在同名文件,则忽略它们。 + \ No newline at end of file