linux文本文件编辑命令

《linux就该这么学》 笔记 (5)
Linux系统中"一切都是文件",而对服务程序进行配置自然也就是编辑程序的配置文件。也就是说,配置要首先快速的找到相应的配置文件,然后熟练的修改文件。
linux系统中有多个用与查看文本内容的命令,每个命令都有自己的特点。

1 cat

cat( concatenate files and print on the standard output)比较适合查看内容较少的纯文本文件,。

[redhat@linuxpronbe ~]$ cat -n .bash_profile
     1	# .bash_profile
     2	
     3	# Get the aliases and functions
     4	if [ -f ~/.bashrc ]; then
     5		. ~/.bashrc
     6	fi
     7	
     8	# User specific environment and startup programs
     9	
    10	PATH=$PATH:$HOME/.local/bin:$HOME/bin
    11	
    12	export PATH

2 more

more(file perusal filter for crt viewing)用于查看内容较多的纯文本文件, 如果要阅读长篇小说或非常长的配置文件,那么cat就不合适了,信息会在屏幕上快速翻滚,导致自己看不清,内容就翻过去了。因此对于长篇的文本内容,推荐使用more, 会在最小面使用百分比的形式来提示已经阅读了多少内容,也可以使用空格或回车翻页。

3 head

head用于查看纯文本文档的前N行, 在阅读文本内容时,有时不需要看完整个文档,如果只想看前几行, 那就用head.

head [option] [file]

[root@linuxpronbe etc]# head ./yum.conf -n 10
[main]
cachedir=/var/cache/yum/$basearch/$releasever
keepcache=0
debuglevel=2
logfile=/var/log/yum.log
exactarch=1
obsoletes=1
gpgcheck=1
plugins=1
installonly_limit=3

4 tail

tail用于查看纯文本文档的后N行或持续刷新内容,用法和head类似。tail命令最强悍的功能是可以持续刷新一个文件的内容,当要实时查看新的日志文件时,这十分有用。

tail [option] [file]

[root@linuxpronbe etc]# tail ./yum.conf -n 10
# download the new metadata and "pay" for it by yum not having correct
# information.
#  It is esp. important, to have correct metadata, for distributions like
# Fedora which don't keep old packages around. If you don't like this checking
# interupting your command line usage, it's much better to have something
# manually check the metadata once an hour (yum-updatesd will do this).
# metadata_expire=90m

# PUT YOUR REPOS HERE OR IN separate files named file.repo
# in /etc/yum.repos.d

5 tr

tr ( translate or delete characters)用来替换文本文件中的字符,有的时候我们想替换文本中的某些字符,手工替换工作量巨大,也不现实,这样可以先使用cat命令读取处理的文本,然后通过管道符把内容传递给tr命令替换即可。

tr [原始字符] [目标字符]

[root@linuxpronbe home]# cat test.txt 
abcdefg
[root@linuxpronbe home]# cat test.txt | tr [a-z] [A-Z]
ABCDEFG

6 wc

wc( print newline, word, and byte counts for each file)命令用于统计指定文本的行数、字数、字节数等。

[root@linuxpronbe ~]# wc -l /etc/passwd 
38 /etc/passwd

7 stat

stat命令用以查看文件的具体储存信息和时间的信息。

[root@linuxpronbe ~]# stat initial-setup-ks.cfg 
  File: ‘initial-setup-ks.cfg’
  Size: 1084      	Blocks: 8          IO Block: 4096   regular file
Device: fd00h/64768d	Inode: 71443981    Links: 1
Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
Context: system_u:object_r:admin_home_t:s0
Access: 2021-07-21 02:53:19.591012757 -0400
Modify: 2021-07-21 02:53:19.592012757 -0400
Change: 2021-07-21 02:53:19.592012757 -0400
 Birth: -

8 cut

cut(remove sections from each line of files)命令用于按“列”提取文本字符,按行提取比按列提取简单,只需要行数即可,但是按列不仅要设置列数(-f), 还要设置间隔符号(-d)。

cut [参数] 文本

示例: 使用cut提取出passwd文件中的用户名信息,
passwd用户信息的每一项是采用冒号间隔的,即提取用(:)为间隔符的第一列内容。

[root@linuxpronbe ~]# head -n 2 /etc/passwd
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
[root@linuxpronbe ~]# cut -d: -f1 /etc/passwd
root
bin
daemon
adm
lp
sync
shutdown

9 diff

diff命令用于比较多个文本文件的差异,不仅可以使用 --brief 参数来确认两个文件是否相同,还可以使用-c来详细比较多个文件的差异之处,这绝对是判断文件是否被篡改的神器。

[root@linuxpronbe home]# cat test.txt
abcdefg
[root@linuxpronbe home]# cat test.txt |tr a A >> test1.txt 
[root@linuxpronbe home]# cat test1.txt 
Abcdefg
[root@linuxpronbe home]# diff --brief test.txt test1.txt 
Files test.txt and test1.txt differ
[root@linuxpronbe home]# diff -c test.txt test1.txt 
*** test.txt	2021-07-22 23:18:56.537831912 -0400
--- test1.txt	2021-07-22 23:19:03.354831623 -0400
***************
*** 1 ****
! abcdefg
--- 1 ----
! Abcdefg
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值