文件处理命令
查看文件内容
-
cat 命令
-
作用:查看文件内容
-
语法:
cat xu666.txt -
注意事项:cat命令是将文件的所有内容一次性打印出来,如果文件内容过大,会导致根本看不到有用的信息,或者导致机器卡死。因此cat命令不适合读取大文件,适合阅读内容较少的文件。
xu@xu-ubuntu-desktop01:~/桌面$ cat xu666.txt xu66666666666666666666666666666666666666666666666777777777776 # 读取sysctl.conf文件 root@xu-ubuntu-desktop01:~# cat /etc/sysctl.conf # # /etc/sysctl.conf - Configuration file for setting system variables # See /etc/sysctl.d/ for additional system variables. # See sysctl.conf (5) for information. # #kernel.domainname = example.com ...... -
cat命令可以读取文件,结合重定向符,可以合并多个文件的内容
xu@xu-ubuntu-desktop01:~/桌面$ cat xu.txt hello.txt world.txt > all.txt xu@xu-ubuntu-desktop01:~/桌面$ cat all.txt xu666 Hello world
-
-
more 分屏命令
-
作用:分屏查看文件内容,一般用于读取较多内容文件,比如小说文件等
-
特点:more和cat命令一样,都会一次性将内容打印出来,放入内存中,比较消耗资源,不适合太大的文件
-
语法:
more xu666.txtmore命令会打开一个交互界面,下面是常用的交互命令
回车键 向下移动一行 d 向下移动半页,移动屏幕的一半 space空格 向下移动一页 b 向上移动一页 /字符串 搜索指定字符串 :f 显示当前文件的文件名和行号 q或Q 退出more 上下移动 没有该功能
-
-
less命令
-
作用:和more命令一样,分屏读取文件
-
特点:不是加载整个文件,而是读多少,加载多少,读取大容量很适合。
-
语法:
less xu666.txt和more命令一样,也需要通过指令控制,也有一个交互界面,指令也是一样的。
回车键 向下移动一行 d 向下移动半页,移动屏幕的一半 space空格 向下移动一页 b 向上移动一页 /字符串 搜索指定字符串 :f 显示当前文件的文件名和行号 q或Q 退出more 上下移动 没有该功能
-
-
cat、more、less命令的对比
| cat | more | less | |
|---|---|---|---|
| 特点 | 显示小文件(一屏以内) | 显示大文件(超过一屏) | 显示大文件(超过一屏) |
| 交互命令 | / | 有 | 有 |
| 上下键翻行 | / | / | 有 |
-
head命令
-
作用:显示文件前N行
-
语法:默认显示前10行,可以传数字参数
head carlos666.log# 默认显示 xu@xu-ubuntu-desktop01:~/桌面$ head num.txt carlos 0 carlos 1 carlos 2 carlos 3 carlos 4 carlos 5 carlos 6 carlos 7 carlos 8 carlos 9 # 显示指定行 xu@xu-ubuntu-desktop01:~/桌面$ head -5 num.txt carlos 0 carlos 1 carlos 2 carlos 3 carlos 4
-
-
tail命令(重点)
-
作用:默认显示尾部10行,也可以指定行号,因为是文件结尾,也就是最新的内容,常常用来查看日志,以及持续检测日志文件内容的变化,使用很多,
-
语法:tail [选项] [文件]
-
常用参数
# 默认显示10行 xu@xu-ubuntu-desktop01:~/桌面$ tail num.txt carlos 190 carlos 191 carlos 192 carlos 193 carlos 194 carlos 195 carlos 196 carlos 197 carlos 198 carlos 199 # 显示后5行 xu@xu-ubuntu-desktop01:~/桌面$ tail -5 num.txt carlos 195 carlos 196 carlos 197 carlos 198 carlos 199 # -f 当文件增长时,显示后续添加的数据 # 新增数据 xu@xu-ubuntu-desktop01:~/桌面$ echo "carlos 222" >> num.txt xu@xu-ubuntu-desktop01:~/桌面$ echo "carlos 222" >> num.txt xu@xu-ubuntu-desktop01:~/桌面$ echo "carlos 222" >> num.txt xu@xu-ubuntu-desktop01:~/桌面$ echo "carlos 222" >> num.txt xu@xu-ubuntu-desktop01:~/桌面$ echo "carlos 222" >> num.txt # 持续显示 xu@xu-ubuntu-desktop01:~/桌面$ tail -f num.txt carlos 191 carlos 192 carlos 193 carlos 194 carlos 195 carlos 196 carlos 197 carlos 198 carlos 199 carlos 222 carlos 222 carlos 222 carlos 222 carlos 222 # 可以按ctrl + c 退出-f页面,Linux中的大部分操作都可以使用 ctrl + c 强制退出。
-
统计文件信息
-
wc命令
-
作用:用来统计文件内的信息,一般统计如(行数、单词数、字节数)
-
语法:wc [选项] [文件]
-
参数
-l: 表示lines,行数(以回车/换行符为标准) -w: 表示words,单词数,依照空格来判断单词数 -c: 表示bytes,字节数(空格、回车、换行) -
用法:
# 输入1-50的数进入num.txt文件 xu@xu-ubuntu-desktop01:~/桌面$ seq 50 > num.txt # 查看num.txt文件的行号:50,单词数:50,字节数:141 xu@xu-ubuntu-desktop01:~/桌面$ wc num.txt 50 50 141 num.txt # 验证num.txt的大小:141字节,与wc命令显示的字节数一致 xu@xu-ubuntu-desktop01:~/桌面$ ll -h num.txt -rw-rw-r-- 1 xu xu 141 4月 27 10:44 num.txt # 增加字符,但是不换行 xu@xu-ubuntu-desktop01:~/桌面$ vim num.txt xu@xu-ubuntu-desktop01:~/桌面$ tail num.txt 41 42 43 44 45 46 47 48 49 50 carlos xu@xu-ubuntu-desktop01:~/桌面$ wc num.txt 50 51 148 num.txt # 因为没有换行,所以行数还是50;新增了一个carlos单词,所以,单词数为51;新增了一个空格和carlos一个单词,一共7个字节,141+7=148,所以字节数为148
-
-
du命令
- 作用:查看文件或者目录(会递归显示子目录)占用磁盘大小
- 语法:du [选项] [文件]
- 常见参数
-s: summaries, 只显示汇总的大小,统计文件夹的大小 -h: 表示以高可读性的形式显示,如果不写-h,默认以KB的形式显示文件大小 # 案例: xu@xu-ubuntu-desktop01:~/桌面$ echo carlos{1..5000000} >> num.txt # 使用ll -h 查看文件大小 xu@xu-ubuntu-desktop01:~/桌面$ ll -h 总计 66M drwxr-xr-x 2 xu xu 4.0K 4月 27 13:35 ./ drwxr-x--- 14 xu xu 4.0K 4月 27 10:48 ../ -rw-rw-r-- 1 xu xu 66M 4月 27 13:35 num.txt # 使用du命令,查看文件大小,显示kb单位 xu@xu-ubuntu-desktop01:~/桌面$ du num.txt 67276 num.txt # 友好显示 xu@xu-ubuntu-desktop01:~/桌面$ du -h num.txt 66M num.txt # 显示文件总和 # 第一种方法: xu@xu-ubuntu-desktop01:~/桌面$ ll -h 总计 66M drwxr-xr-x 2 xu xu 4.0K 4月 27 13:35 ./ drwxr-x--- 14 xu xu 4.0K 4月 27 10:48 ../ -rw-rw-r-- 1 xu xu 66M 4月 27 13:35 num.txt # 第二种方法: xu@xu-ubuntu-desktop01:~/桌面$ du -sh . 66M . # kb单位 xu@xu-ubuntu-desktop01:~/桌面$ du -s . 67280 .
- 适用情况
-
- 工作中,经常因为某些文件过大,导致磁盘空间不够
-
- 比如长时间机器运行,生成过多的日志,也需要定期删除,释放磁盘空间
# 查看日志文件大小 xu@xu-ubuntu-desktop01:~/桌面$ sudo du -sh /var/log 77M /var/log # 批量统计文件大小 xu@xu-ubuntu-desktop01:~/桌面$ du -h * 66M num1.txt 66M num2.txt 66M num.txt
-
文件查找
- find命令