From a1e24866f503fea7993fcc25db3112aa0ee419df Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E6=AC=A3?= <12078128+rainbow-meet-rose-die@user.noreply.gitee.com> Date: Thu, 30 May 2024 04:55:37 +0000 Subject: [PATCH] =?UTF-8?q?=E7=AC=94=E8=AE=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 李欣 <12078128+rainbow-meet-rose-die@user.noreply.gitee.com> --- ...07\344\273\266\345\206\205\345\256\271.md" | 298 ++++++++++++++++++ 1 file changed, 298 insertions(+) create mode 100644 "51 \346\235\216\346\254\243/20240530\346\237\245\346\211\276\346\226\207\344\273\266\345\206\205\345\256\271.md" diff --git "a/51 \346\235\216\346\254\243/20240530\346\237\245\346\211\276\346\226\207\344\273\266\345\206\205\345\256\271.md" "b/51 \346\235\216\346\254\243/20240530\346\237\245\346\211\276\346\226\207\344\273\266\345\206\205\345\256\271.md" new file mode 100644 index 0000000..fcdc509 --- /dev/null +++ "b/51 \346\235\216\346\254\243/20240530\346\237\245\346\211\276\346\226\207\344\273\266\345\206\205\345\256\271.md" @@ -0,0 +1,298 @@ +#### 查看文件 + +cat 正向显示整个文件 + +tac 反向显示整个文件 + +共同点:一次性加载/显示整个文件 + +缺点:加载大文件慢,适合查看小文件,一屏可看完 + +more 分页显示,一次性加载整个文件 + +less 分页显示。只加载显示的一页 -N 显示行数 -M 长格式显示 + +head 默认查看文件头部为10行 -n 1查看一行 + +tail 查看文件尾部10行 -n 查看指定行数 -f 实时查看文件添加内容 + +nl 给文件内容加行号,显示全部内容 + +**以下所有操作都在家目录执行:** + +### + +1. **操作题一**:使用 `cat` 命令显示 `/etc/passwd` 文件的内容。 + +```bash +lmd@hecs-288852:~$ cat /etc/passwd +``` + + + +1. **操作题二**:将文件 `/etc/passwd` 的内容复制到 `passwd_bak.txt` 文件中,但不使用 `cp` 命令。 + +```bash +lmd@hecs-288852:~$ cat /etc/passwd > passwd_bak.txt +``` + + + +1. **操作题三**:新建两个文件 `file1.txt` 和 `file2.txt`,分别写一些不同的内容,再将这两个文件的内容合并到一个新的文件 `file3.txt` 中。 + +```bash +lmd@hecs-288852:~$ sudo vim file1.txt +lmd@hecs-288852:~$ sudo vim file2.txt +lmd@hecs-288852:~$ cat file1.txt file2.txt +file1 +file2 +lmd@hecs-288852:~$ cat file1.txt file2.txt > file3.txt +lmd@hecs-288852:~$ cat file3.txt +file1 +file2 +``` + + + +### + +1. 使用命令从尾部开始显示 `bigfile.txt` 文件的内容。 + +```bash +lmd@hecs-288852:~$ tac bigfile.txt +``` + + + +1. 尝试找出 `bigfile.txt` 文件的最后一行内容,要使用 `tac` 命令。 + +```bash +lmd@hecs-288852:~$ tac bigfile.txt | head -n 1 +最后一行,标志着文件的结束。 +``` + + + +1. 查看 `bigfile.txt` 文件的最后5行内容。 + +```bash +lmd@hecs-288852:~$ tail bigfile.txt -n 5 +ERROR: 另一个错误消息,也需要被找到。 + +这里是文件末尾的几行内容,用于测试tail命令。 + +最后一行,标志着文件的结束。 +``` + + + +1. 倒序查看 `bigfile.txt` 文件的最后5行内容。 + +```bash +lmd@hecs-288852:~$ tac bigfile.txt | tail -n 5 +以下是重复的行,用于测试分页和搜索功能。 + +第三行,同样包含了足够多的字符,以便在查看时能够滚动或分页。 +第二行内容,包含了更多的字符来模拟一个大文件。 +这是一行测试文本,用于演示各种文本处理命令。 +``` + + + +### + +1. **操作题一**:使用 `more` 命令查看 `bigfile.txt` 文件的内容,并在查看过程中使用空格键翻页。 + +```bash +lmd@hecs-288852:~$ more bigfile.txt +``` + + + +1. **操作题二**:在 `more` 命令的查看过程中,如何使用回车键来逐行查看文件内容? + +```bash +lmd@hecs-288852:~$ more bigfile.txt +``` + + + +1. **操作题三**:如何在 `more` 命令中搜索`bigfile.txt`特定字符串(例如 "error")并跳转到下一个匹配项? + +```bash +more bigfile.txt +/error +n +``` + + + +### `less` 命令操作题 + +1. **操作题一**:使用 `less` 命令查看 `bigfile.txt` 文件,并快速定位到文件的末尾。 + +```bash +lmd@hecs-288852:~$ less bigfile.txt +shift+G +``` + + + +1. **操作题二**:在 `less` 命令中,如何向上和向下滚动文件内容? + +```bash +j 下 +k 上 +``` + + + +1. **操作题三**:在 `less` 命令中,如何搜索`bigfile.txt`一个特定的函数名(例如 `def my_function`),并查看所有匹配项? + +```bash +/def\s+my_function +``` + + + +### `head` 命令操作题 + +1. **操作题一**:使用 `head` 命令显示 `bigfile.txt` 文件的前5行内容。 + +```bash +lmd@hecs-288852:~$ head bigfile.txt -n 5 +这是一行测试文本,用于演示各种文本处理命令。 +第二行内容,包含了更多的字符来模拟一个大文件。 +第三行,同样包含了足够多的字符,以便在查看时能够滚动或分页。 + +以下是重复的行,用于测试分页和搜索功能。 +``` + + + +1. **操作题二**:将 `bigfile.txt` 的前20行内容保存到 `bigfile_20.txt` 文件中。 + +```bash +lmd@hecs-288852:~$ head bigfile.txt -n 20 > bigfile_20.txt +lmd@hecs-288852:~$ cat bigfile_20.txt +这是一行测试文本,用于演示各种文本处理命令。 +第二行内容,包含了更多的字符来模拟一个大文件。 +第三行,同样包含了足够多的字符,以便在查看时能够滚动或分页。 + +以下是重复的行,用于测试分页和搜索功能。 +ERROR: 这是一个错误消息,需要被找到。 +这是一行普通的文本。 + +以下是一些随机文本,用于填充文件。 +Lorem ipsum dolor sit amet, consectetur adipiscing elit. +Sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. +Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. + +def my_function { + 嘿嘿嘿 +} + + +再次出现的错误消息,用于测试。 +ERROR: 另一个错误消息,也需要被找到。 +``` + + + +1. **操作题三**:如何结合 `head` 和 `grep` 命令来查找 `bigfile.txt` 文件中以 "A" 开头的前10行? + +```bash +lmd@hecs-288852:~$ grep "A" bigfile.txt | head -n 10 +Although challenges may arise, always persevere in pursuit of your goals. +Ancient wisdom teaches us the value of patience and understanding. +Autumn brings with it a sense of calm and reflection, allowing us to appreciate the beauty of nature. +An apple a day keeps the doctor away, a saying that reminds us of the importance of healthy eating. +After a long day, a warm bath and a cup of tea are the perfect way to unwind and relax. +A开头是个错误 +A不是好主意 +Apples are my favorite fruit, especially during the autumn season. +Always remember to be kind to others, as kindness is a virtue that never fades. +As the sun rises, I greet the new day with a smile and a positive mindset. +``` + + + +### `tail` 命令操作题 + +1. **操作题一**:使用 `tail` 命令显示 `bigfile.txt` 文件的最后20行内容。 + +```bash +lmd@hecs-288852:~$ tail bigfile.txt -n 20 +以下是一些随机文本,用于填充文件。 +Lorem ipsum dolor sit amet, consectetur adipiscing elit. +Sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. +Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. + + +Apples are my favorite fruit, especially during the autumn season. +Always remember to be kind to others, as kindness is a virtue that never fades. +As the sun rises, I greet the new day with a smile and a positive mindset. +Artists are the true creators of our world, shaping our perception of beauty and culture. +Adventure awaits those who dare to step out of their comfort zones and explore the unknown. + + + +再次出现的错误消息,用于测试。 +ERROR: 另一个错误消息,也需要被找到。 + +这里是文件末尾的几行内容,用于测试tail命令。 + +最后一行,标志着文件的结束。 +``` + + + +1. **操作题二**:如何实时跟踪一个正在写入的日志文件(如 `bigfile.txt`)的最后10行内容? + +```bash +tail -f -n 10 bigfile.txt +``` + + + +1. **操作题三**:在 `tail` 命令中,如何反向显示文件的最后10行内容(即按从旧到新的顺序显示)? + +```bash +tail -n 10 bigfile.txt | tac +``` + + + +### 综合题 + +**综合题**:假设你有一个非常大的日志文件 `bigfile.txt`,你需要找出所有包含 "ERROR" 字符串的行,并查看这些行及其之前的两行内容。 + +```bash +lmd@hecs-288852:~$ cat bigfile.txt | grep "ERROR" -B2 + +以下是重复的行,用于测试分页和搜索功能。 +ERROR: 这是一个错误消息,需要被找到。 +-- + +再次出现的错误消息,用于测试。 +ERROR: 另一个错误消息,也需要被找到。 +-- + + +ERROR: 这是一个错误消息,需要被找到。 +-- +再次出现的错误消息,用于测试。 +A开头是个错误 +ERROR: 另一个错误消息,也需要被找到。 +-- + +A不是好主意 +ERROR: 这是一个错误消息,需要被找到。 +-- + +再次出现的错误消息,用于测试。 +ERROR: 另一个错误消息,也需要被找到。 +``` + + + -- Gitee