Linux文本查看命令及其选项详解(cat,head,tail)

所属分类: 脚本专栏 / linux shell 阅读数: 1976
收藏 0 赞 0 分享

linux系统内置命令可以通过以下两种方式查询:“cat --help” 或者“man cat”。

cat命令的常用选项和官方解释如下:

cat file_name 显示文件全部内容

cat -b file_name 显示文件非空行内容

cat -E file_name 在文件每行末尾显示$,常用于管道功能

cat -n file_name 显示内容和行号

Usage: cat [OPTION]... [FILE]...
Concatenate FILE(s) to standard output.

With no FILE, or when FILE is -, read standard input.

 -A, --show-all      equivalent to -vET
 -b, --number-nonblank  number nonempty output lines, overrides -n
 -e            equivalent to -vE
 -E, --show-ends     display $ at end of each line
 -n, --number       number all output lines
 -s, --squeeze-blank   suppress repeated empty output lines
 -t            equivalent to -vT
 -T, --show-tabs     display TAB characters as ^I
 -u            (ignored)
 -v, --show-nonprinting  use ^ and M- notation, except for LFD and TAB
   --help   display this help and exit
   --version output version information and exit

Examples:
 cat f - g Output f's contents, then standard input, then g's contents.
 cat    Copy standard input to standard output.

GNU coreutils online help: <http://www.gnu.org/software/coreutils/>
Full documentation at: http://www.gnu.org/software/coreutils/cat

head命令及其选项如下:

head -c10 file_name 显示一开始的10个字节

head -c-10 file_name 显示除末尾10个字节之外的内容

head -n10 file_name 显示一开始的10行内容

head -n-10 file_name 显示除末尾的10行之外的内容

Usage: head [OPTION]... [FILE]...
Print the first 10 lines of each FILE to standard output.
With more than one FILE, precede each with a header giving the file name.

With no FILE, or when FILE is -, read standard input.

Mandatory arguments to long options are mandatory for short options too.
 -c, --bytes=[-]NUM    print the first NUM bytes of each file;
               with the leading '-', print all but the last
               NUM bytes of each file
 -n, --lines=[-]NUM    print the first NUM lines instead of the first 10;
               with the leading '-', print all but the last
               NUM lines of each file
 -q, --quiet, --silent  never print headers giving file names
 -v, --verbose      always print headers giving file names
 -z, --zero-terminated  line delimiter is NUL, not newline
   --help   display this help and exit
   --version output version information and exit

NUM may have a multiplier suffix:
b 512, kB 1000, K 1024, MB 1000*1000, M 1024*1024,
GB 1000*1000*1000, G 1024*1024*1024, and so on for T, P, E, Z, Y.

GNU coreutils online help: <http://www.gnu.org/software/coreutils/>
Full documentation at: http://www.gnu.org/software/coreutils/head

tail命令及其选项如下:

tail -c10 file_name 显示末尾的10个字节

tail -c-10 file_name 显示除开头10个字节之外的内容

tail -n10 file_name 显示末尾的10行内容

tail -n-10 file_name 显示除开头的10行之外的内容

Usage: tail [OPTION]... [FILE]...
Print the last 10 lines of each FILE to standard output.
With more than one FILE, precede each with a header giving the file name.

With no FILE, or when FILE is -, read standard input.

Mandatory arguments to long options are mandatory for short options too.
 -c, --bytes=[+]NUM    output the last NUM bytes; or use -c +NUM to
               output starting with byte NUM of each file
 -f, --follow[={name|descriptor}]
              output appended data as the file grows;
               an absent option argument means 'descriptor'
 -F            same as --follow=name --retry
 -n, --lines=[+]NUM    output the last NUM lines, instead of the last 10;
               or use -n +NUM to output starting with line NUM
   --max-unchanged-stats=N
              with --follow=name, reopen a FILE which has not
               changed size after N (default 5) iterations
               to see if it has been unlinked or renamed
               (this is the usual case of rotated log files);
               with inotify, this option is rarely useful
   --pid=PID      with -f, terminate after process ID, PID dies
 -q, --quiet, --silent  never output headers giving file names
   --retry       keep trying to open a file if it is inaccessible
 -s, --sleep-interval=N  with -f, sleep for approximately N seconds
               (default 1.0) between iterations;
               with inotify and --pid=P, check process P at
               least once every N seconds
 -v, --verbose      always output headers giving file names
 -z, --zero-terminated  line delimiter is NUL, not newline
   --help   display this help and exit
   --version output version information and exit

NUM may have a multiplier suffix:
b 512, kB 1000, K 1024, MB 1000*1000, M 1024*1024,
GB 1000*1000*1000, G 1024*1024*1024, and so on for T, P, E, Z, Y.

With --follow (-f), tail defaults to following the file descriptor, which
means that even if a tail'ed file is renamed, tail will continue to track
its end. This default behavior is not desirable when you really want to
track the actual name of the file, not the file descriptor (e.g., log
rotation). Use --follow=name in that case. That causes tail to track the
named file in a way that accommodates renaming, removal and creation.

GNU coreutils online help: <http://www.gnu.org/software/coreutils/>
Full documentation at: http://www.gnu.org/software/coreutils/tail

搭配管道使用更佳

此外,这三个命令常常与管道功能搭配,用于文件内容的操作,例如:

对data.txt中的数据进行排序:cat data.txt | sort

对data.txt中的内容匹配:cat data.txt | grep 'a'

输出data.txt中的非空行数:cat -b data.txt | wc -l

更多精彩内容其他人还在看

linux创建用户useradd命令代码示例

本文通过代码示例给大家介绍了adduser与useradd命令二者的关系 以及使用useradd命令添加用户的方法,需要的朋友参考下吧
收藏 0 赞 0 分享

Linux 中LVS NAT 配置步骤的详解

这篇文章主要介绍了Linux 中LVS NAT 配置步骤的详解的相关资料,这里列出详细的实现步骤,需要的朋友可以参考下
收藏 0 赞 0 分享

Linux中的内核链表实例详解

这篇文章主要介绍了Linux中的内核链表实例详解的相关资料,链表中一般都要进行初始化、插入、删除、显示、释放链表,寻找节点这几个操作,需要的朋友可以参考下
收藏 0 赞 0 分享

Shell中特殊字符的用法总结大全

这篇文章主要给大家总结了关于Shell中特殊字符的相关资料,文中包括分好、&、#、!、$、大于号、单双引号等等一系列特殊字符的用法,通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面来一起看看吧。
收藏 0 赞 0 分享

Shell脚步攻略之管道重定向基础

管道是为了解决进程间通信问题而存在,它可以让两个进程之间的数据进行传递,将一个进程的输出数据传递给另一个进程作为其输入数据
收藏 0 赞 0 分享

Linux shell数组循环的实例详解

这篇文章主要介绍了Linux shell数组循环的实例详解的相关资料,这里举例说明如何实现shell数组循环,需要的朋友可以参考下
收藏 0 赞 0 分享

linux shell内置判断语句

内置判断,成功的时候返回0,不成功返回非零。接下来通过本文重点给大家介绍linux shell内置判断语句,感兴趣的的朋友一起看看吧
收藏 0 赞 0 分享

Linux文件的归档和压缩命令

文件归档命令tar,文件归档有好多好处,方便使用易于管理,接下来通过本文给大家分享linux文件的归档和压缩命令,感兴趣的朋友一起看看吧
收藏 0 赞 0 分享

Linux Shell中curl和wget使用代理IP的方法教程

这篇文章主要给大家介绍了关于在Linux Shell中curl和wget使用代理IP的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面跟着小编来一起学习学习吧。
收藏 0 赞 0 分享

Linux 日常常用指令及应用小结

最近在学习一些基本的Linux指令,在这里总结一下,在搭环境中常用的一些指令,熟悉这些指令就基本能够使用CentOS进行日常操作了
收藏 0 赞 0 分享
查看更多