开启掘金成长之旅!这是我参与「掘金日新计划 · 12 月更文挑战」的第28天,点击查看活动详情
✨欢迎关注🖱点赞🎀收藏⭐留言✒
🔮本文由京与旧铺原创,csdn首发!
😘系列专栏:java学习
💻首发时间:🎞2022年12月22日🎠
🀄如果觉得博主的文章还不错的话,请三连支持一下博主哦
🎧作者是一个新人,在很多方面还做的不好,欢迎大佬指正,一起学习哦,冲冲冲
🐱💻导航小助手
2.浏览文件相关命令
file 判断文件类型 cat 由第一行开始显示文件内容 tac 从最后一行开始显示,与cat命令刚好相反,毕竟可以看出 tac 是 cat 的倒着写。 nl 显示的时候,顺道输出行号,就是会附加行号。 more 一页一页的显示文件内容 less 与 more 类似,但是比 more 更好的是,他可以往前翻页! head 只看头几行 tail 只看尾巴几行 od 以二进制的方式读取文件内容
2.1file命令
file命令可以判断文件类型
语法:
file 选项 文件
示例:
[wjhw@VM-4-15-centos hello]$ file test.c
test.c: C source, ASCII text
[wjhw@VM-4-15-centos hello]$ file a.out
a.out: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), dynamically linked (uses shared libs), for GNU/Linux 2.6.32, BuildID[sha1]=60e92e86662e43a2137b43170715766098f0e1e5, not stripped
[wjhw@VM-4-15-centos hello]$ mkdir fir
[wjhw@VM-4-15-centos hello]$ file fir
fir: directory
2.2cat/tac/nl命令
cat命令可以由第一行开始显示文件的内容。
语法:
cat 选项 文件
选项与参数: -A :相当于 -vET 的整合选项,可列出一些特殊字符而不是空白而已; -b :列出行号,仅针对非空白行做行号显示,空白行不标行号 -E :将结尾的断行字符 $ 显示出来; -n :打印出行号,连同空白行也会有行号,与 -b 的选项不同; -T :将 [tab] 按键以 ^I 显示出来; -v :列出一些看不到的特殊字符
示例:
[wjhw@VM-4-15-centos hello]$ cat test.c
#include <stdio.h>
int main()
{
printf("hello!\n");
return 0;
}
选项示例:
[wjhw@VM-4-15-centos hello]$ cat -A test.c
#include <stdio.h>$
$
int main()$
{$
printf("hello!\n");$
return 0;$
}$
[wjhw@VM-4-15-centos hello]$ cat -b test.c
1 #include <stdio.h>
2 int main()
3 {
4 printf("hello!\n");
5 return 0;
6 }
[wjhw@VM-4-15-centos hello]$ cat -n test.c
1 #include <stdio.h>
2
3 int main()
4 {
5 printf("hello!\n");
6 return 0;
7 }
tac命令也可以显示文本内容,不过它与cat相反,tac命令显示顺序是从后往前。
示例:
[wjhw@VM-4-15-centos hello]$ tac test.c
}
return 0;
printf("hello!\n");
{
int main()
#include <stdio.h>
nl命令与cat一样,也是从开头输出内容,只不过天然会带上行号。
语法:
nl 选项 文件
选项与参数: -b :指定行号指定的方式,主要有两种: -b a :表示不论是否为空行,也同样列出行号(类似 cat -n); -b t :如果有空行,空的那一行不要列出行号(类似 cat -b,默认); -n :设置行号出现的位置,主要有三种: -n ln :行号在屏幕的最左方显示; -n rn :行号在自己字段的靠左方显示,且不加 0 ; -n rz :行号在自己字段的靠左方显示,且加 0 ;
[wjhw@VM-4-15-centos hello]$ nl -n rn test.c
1 #include <stdio.h>
2 int main()
3 {
4 printf("hello!\n");
5 return 0;
6 }
[wjhw@VM-4-15-centos hello]$ nl -n ln test.c
1 #include <stdio.h>
2 int main()
3 {
4 printf("hello!\n");
5 return 0;
6 }
[wjhw@VM-4-15-centos hello]$ nl -n rz test.c
000001 #include <stdio.h>
000002 int main()
000003 {
000004 printf("hello!\n");
000005 return 0;
000006 }
2.2more/less指令
前面提到的 nl 与 cat, tac 等等,都是一次性的将数据一口气显示到屏幕上面,那有没有可以 进行一页一页翻动的指令啊? 让我们可以一页一页的观察, more 与 less 指令可以做到这一点,用的也比较常见。
more命与从cat命令一样会显示文本文件的内容,但会在显示每页数据之后停下来。具体翻页规则如下: 空格键:翻到下一页 enter键:翻到下一行 b或Ctrl+b:往回翻页,仅支持文件 q:停止more指令 \字符串:向下查找匹配该字符串的内容 :f:显示行数
语法:
more 选项 文件
more命令是不能一行一行往回翻的,但less命令可以,相比于more,less更加灵活
翻页规则: 空格键:翻到下一页 [pagedown]:向下翻动一页; [pageup] :向上翻动一页; /字串 :向下搜寻“字串”的功能; ?字串 :向上搜寻“字串”的功能; g :前进到文件的第一行去; G :前进到文件的最后一行去 (注意大小写); q :离开 less 这个程序;
2.3head/tail命令
head命令可以显示某文件的前几行,tail命令就是显示某文件的最后几行。
常用选项: -n 显示前n行或后n行
语法:
haed/tail -n 文件
示例:
[wjhw@VM-4-15-centos hello]$ head -4 test.c
#include <stdio.h>
int main()
{
[wjhw@VM-4-15-centos hello]$ tail -4 test.c
{
printf("hello!\n");
return 0;
}
其他用法: head -n -m 最后m行不显示,其他的内容均显示。 tail -n +m 前m行不显示,其他内容均显示。
2.4od命令
od命令可以以二进制形式显示文本内容。
语法:
od 选项 文件
选项或参数: -t :后面可以接各种“类型 (TYPE)”的输出,例如: a :利用默认的字符来输出; c :使用 ASCII 字符来输出 d[size] :利用十进制(decimal)来输出数据,每个整数占用 size Bytes ; f[size] :利用浮点数值(floating)来输出数据,每个数占用 size Bytes ; o[size] :利用八进位(octal)来输出数据,每个整数占用 size Bytes ; x[size] :利用十六进制(hexadecimal)来输出数据,每个整数占用 size Bytes ;
示例:
[wjhw@VM-4-15-centos hello]$ od test.c
0000000 064443 061556 072554 062544 036040 072163 064544 027157
0000020 037150 005012 067151 020164 060555 067151 024450 075412
0000040 020012 070040 064562 072156 024146 064042 066145 067554
0000060 056041 021156 035451 020012 071040 072145 071165 020156
0000100 035460 076412 000012
0000105
[wjhw@VM-4-15-centos hello]$ od -t a test.c
0000000 # i n c l u d e sp < s t d i o .
0000020 h > nl nl i n t sp m a i n ( ) nl {
0000040 nl sp sp p r i n t f ( " h e l l o
0000060 ! \ n " ) ; nl sp sp r e t u r n sp
0000100 0 ; nl } nl
0000105
[wjhw@VM-4-15-centos hello]$ od -t c test.c
0000000 # i n c l u d e < s t d i o .
0000020 h > \n \n i n t m a i n ( ) \n {
0000040 \n p r i n t f ( " h e l l o
0000060 ! \ n " ) ; \n r e t u r n
0000100 0 ; \n } \n
0000105
[wjhw@VM-4-15-centos hello]$ od -t x test.c
0000000 636e6923 6564756c 74733c20 2e6f6964
0000020 0a0a3e68 20746e69 6e69616d 7b0a2928
0000040 7020200a 746e6972 68222866 6f6c6c65
0000060 226e5c21 200a3b29 74657220 206e7275
0000100 7d0a3b30 0000000a
0000105