开源一夏 | 阿里云 ECS 之 Linux 文本操作命令

vim
vim filename 后进入编辑器视图后,可以通过命令操作文档内容,以下是常用的基础操作命令

保存命令

cat
命令描述:cat 命令用于查看内容较少的纯文本文件。
命令格式:cat [选项] [文件]。
命令参数说明

more
命令描述:more 命令从前向后分页显示文件内容。
操作命令

less
命令描述:less 命令可以对文件或其它输出进行分页显示,与 moe 命令相似,但使用 less 可以随意浏览文件,而 more 仅能向前移动,却不能向后移动。
命令格式:less [参数] 文件 。
命令参数说明

操作命令说明

head
命令描述:head 命令用于查看文件开头指定行数的内容。
命令格式:head [参数] [文件]。
命令参数说明

tail
命令描述:tail 命令用于查看文档的后 N 行或持续刷新内容。
命令格式:tail [参数] [文件]。
命令参数说明

stat
命令描述:用来显示文件的详细信息,包括 inode、atime、mtime、ctime 等

wc
命令描述:wc 命令用于统计指定文本的行数、字数、字节数。
命令格式:wc [参数] [文件]。
命令参数说明

file
命令描述: file 命令用于辨识文件类型。
命令格式:file [参数] [文件]。
命令参数说明

diff
命令描述:diff 命令用于比较文件的差异。
grep
命令描述:grep 命令用于查找文件里符合条件的字符串。
grep 全称是 Global Regular Expression Print,表示全局正则表达式版本,它能使用正则表达式搜索文本,并把匹配的行打印出来。
在 Shell 脚本中,grep 通过返回一个状态值来表示搜索的状态:
0:匹配成功。
1:匹配失败。
2:搜索的文件不存在。
命令格式:grep [参数] [正则表达式] [文件]。
命令常用参数说明

sed
命令描述:sed 是一种流编辑器,它是文本处理中非常中的工具,能够完美的配合正则表达式使用。
1. 处理时,把当前处理的行存储在临时缓冲区中,称为模式空间(pattern space)。
2. 接着用 sed 命令处理缓冲区中的内容,处理完成后,把缓冲区的内容送往屏幕。
3. 接着处理下一行,这样不断重复,直到文件末尾。
注意:
sed 命令不会修改原文件,例如删除命令只表示某些行不打印输出,而不是从原文件中删去。
如果要改变源文件,需要使用-i 选项。
命令格式:sed [参数] [动作] [文件]。
参数说明

动作说明

awk
命令描述:和 sed 命令类似,awk 命令也是逐行扫描文件(从第 1 行到最后一行),寻找含有目标文本的行,如果匹配成功,则会在该行上执行用户想要的操作;反之,则不对行做任何处理。
命令格式:awk [参数] [脚本] [文件]。
参数说明

内置变量

cut
命令描述:cut 命令主要用来切割字符串,可以对输入的数据进行切割然后输出。
命令格式:cut [参数] [文件]。
参数说明

tr
命令描述:tr 命令用于对来自标准输入的字符进行替换、压缩和删除。
命令格式:tr [参数] [文本]。
参数说明

版权声明: 本文为 InfoQ 作者【六月的雨在infoQ】的原创文章。
原文链接:【http://xie.infoq.cn/article/96e45e306d39c13443ce13200】。
本文遵守【CC-BY 4.0】协议,转载请保留原文出处及本版权声明。
评论