文本文件编辑命令

1.cat 用于查看纯文本文件

[yingqikey@xuexi etc]$ cat updatedb.conf 
PRUNE_BIND_MOUNTS = "yes"
PRUNEFS = "9p afs anon_inodefs auto autofs bdev binfmt_misc cgroup cifs coda configfs cpuset debugfs devpts ecryptfs exofs fuse fuse.sshfs fusectl gfs gfs2 hugetlbfs inotifyfs iso9660 jffs2 lustre mqueue ncpfs nfs nfs4 nfsd pipefs proc ramfs rootfs rpc_pipefs securityfs selinuxfs sfs sockfs sysfs tmpfs ubifs udf usbfs"
PRUNENAMES = ".git .hg .svn"
PRUNEPATHS = "/afs /media /mnt /net /sfs /tmp /udev /var/cache/ccache /var/lib/yum/yumdb /var/spool/cups /var/spool/squid /var/tmp"

cat是用来查看文本量较小的纯文本文件,有时会有一点乱无法清晰的辨别那么添加一个 -n 参数会更加直接

[yingqikey@xuexi etc]$ cat -n updatedb.conf 
     1  PRUNE_BIND_MOUNTS = "yes"
     2  PRUNEFS = "9p afs anon_inodefs auto autofs bdev binfmt_misc cgroup cifs coda configfs cpuset debugfs devpts ecryptfs exofs fuse fuse.sshfs fusectl gfs gfs2 hugetlbfs inotifyfs iso9660 jffs2 lustre mqueue ncpfs nfs nfs4 nfsd pipefs proc ramfs rootfs rpc_pipefs securityfs selinuxfs sfs sockfs sysfs tmpfs ubifs udf usbfs"
     3  PRUNENAMES = ".git .hg .svn"
     4  PRUNEPATHS = "/afs /media /mnt /net /sfs /tmp /udev /var/cache/ccache /var/lib/yum/yumdb /var/spool/cups /var/spool/squid /var/tmp"

2.more 用于查看内容较多的纯文本文件,且显示阅读了多少内容,还可按空格键下翻

[yingqikey@xuexi ~]$ more .bash_history 
ls
qu
cd ~
ls
echo yingqike
echo $SHELL
date 
ls
reboot
poweroff
wget man
man wget
wget https://www.linuxprobe.com/docs/LinuxProbe.pdf
sudo root
root
user root
su yingqikey
dasdas
wget https://www.linuxprobe.com/docs/LinuxProbe.pdf
ps
ps -a
ps -x
ps -u
ps -aux
---More--(18%)

3.head 用于查看纯文本文件的前N行 head [参数][文件]

其中指定多少行数的数字就显示多少行
[yingqikey@xuexi ~]$ head -n 20 .bash_history 
ls
qu
cd ~
ls
echo yingqike
echo $SHELL
date 
ls
reboot
poweroff
wget man
man wget
wget https://www.linuxprobe.com/docs/LinuxProbe.pdf
sudo root
root
user root
su yingqikey
dasdas
wget https://www.linuxprobe.com/docs/LinuxProbe.pdf
ps

4.tail 用于查看纯文本文件的后N行或持续刷新 tail [参数][文件名]

其中指定多少行数的数字就显示多少行,此为从后往前看
[yingqikey@xuexi ~]$ tail -n 5 .bash_history 
head men
head man
ls man
men men
加参数 -f 为持续刷新内容,在查看最新的日志文件时很有用 
[yingqikey@xuexi ~]$ tail -f .bash_history 
ls -al
more .bash_logout 
more .bash_history 
head -n 20 .bash_history 
hear man
head men
head man
ls man
men men
set +o history–;export LANG="en_US.UTF-8";export LANGUAGE="en_US.UTF-8";top

5.tr 用于替换文本文件中的字符[查看文件指令] [文件名] | tr [原始字符][目标字符]

小写替换为了大写(注:文件前要加入查看文件的指令,不然无法执行)
[yingqikey@xuexi ~]$ cat .bash_history | tr [l-s] [L-S]
LS
Qu
cd ~
LS

6.wc 用于统计指定文本的行数、字数、字节数
分别对应的参数为 -l 、-w、-c

[yingqikey@xuexi ~]$ wc -l .bash_history
176 .bash_history         统计出输入多少行指令
[yingqikey@xuexi ~]$ wc -w .bash_history
331 .bash_history         统计输入多少字数
[yingqikey@xuexi ~]$ wc -c .bash_history
1856 .bash_history       统计输入多少字节数

7.stat 用于插卡文件具体存储和时间等信息,会显示出三种时间状态

[yingqikey@xuexi ~]$ stat .bash_history
 File:".bash_history"
 Size:1856    Blocks:8  IO Blocks:4096   regular file
Device:fd01h/64769d      Inode:134301725   Links:1
Access:(0600/-rw-------)  Uid:( 1000/yingqikey)   Gid:( 1000/yingqikey)
Access:2020-02-20 21:21:26.024365967 +0800
Modify:2020-02-20 21:21:12.215134594 +0800
Change:2020-02-20 21:21:12.215134594 +0800
Birth:-
          对比理解
[yingqikey@xuexi ~]$ stat .bash_history
  文件:".bash_history"
  大小:1856            块:8          IO 块:4096   普通文件
设备:fd01h/64769d      Inode:134301725   硬链接:1
权限:(0600/-rw-------)  Uid:( 1000/yingqikey)   Gid:( 1000/yingqikey)
最近访问:2020-02-20 21:21:26.024365967 +0800
最近更改:2020-02-20 21:21:12.215134594 +0800
最近改动:2020-02-20 21:21:12.215134594 +0800
创建时间:-

8.cut 用于案“列”提取文本“字符” cut [参数][分割符号][f][区域]

查看用户的数据信息冒号为分割符一个分割符为一个区域
[yingqikey@xuexi ~]$ head -n 2 /etc/passwd
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
分割第一个区域
[yingqikey@xuexi ~]$ cut -d: -f1 /etc/passwd
root
bin
分割第二个区域
[yingqikey@xuexi ~]$ cut -d: -f2 /etc/passwd
x
x
分割第三个区域
[yingqikey@xuexi ~]$ cut -d: -f3 /etc/passwd
0
1
2
3
分割第一和第三区域
[yingqikey@xuexi ~]$ cut -d: -f1,3 /etc/passwd
root:0
bin:1
daemon:2
adm:3

9.diff 用于比较多个文本文件的差异

特别注意对比文件不同加 --brief 是双 -
[yingqikey@xuexi ~]$ diff --brief 123.txt 321.txt
文件 123.txt 和 321.txt 不同
[yingqikey@xuexi ~]$ diff 123.txt 321.txt
2d1
< 星期四
4a4
> 星期四
7d6
< 星期日
加入参数 -c 会对比的更为清晰
[yingqikey@xuexi ~]$ diff -c 123.txt 321.txt
*** 123.txt     2020-02-20 22:21:46.132587864 +0800
--- 321.txt     2020-02-20 22:23:06.760927515 +0800
***************
*** 1,7 ****
  星期一
- 星期四
  星期二
  星期三
  星期五
  星期六
- 星期日
--- 1,6 ----
  星期一
  星期二
  星期三
+ 星期四
  星期五
  星期六
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容