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

 更新时间:2020年08月28日 08:36:21   作者:Philbert  
这篇文章主要介绍了Linux文本查看命令及其选项详解(cat,head,tail),本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下

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文本查看命令及其选项详解(cat,head,tail)的文章就介绍到这了,更多相关Linux文本查看命令及其选项内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • shell脚本中/bin/bash的作用引号与括号的使用小结

    shell脚本中/bin/bash的作用引号与括号的使用小结

    shell脚本通常第一句是#!/bin/bash,在很多情况中,如果没有设置好这一行,那么该程序很可能无法执行,#!/bin/bash是指此脚本使用/bin/bash来解释执行,本文给大家介绍shell脚本中/bin/bash的作用,引号与括号的使用,感兴趣的朋友一起看看吧
    2023-12-12
  • shell中set -e的具体使用

    shell中set -e的具体使用

    set -e是一个Shell命令,它用于在脚本运行时自动退出,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2023-06-06
  • Shell脚本实现猜数字游戏

    Shell脚本实现猜数字游戏

    这篇文章主要为大家详细介绍了Shell脚本实现猜数字游戏,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2020-04-04
  • 定时导出mysql本地数据替换远程数据库数据脚本分享

    定时导出mysql本地数据替换远程数据库数据脚本分享

    这篇文章主要介绍了mysql每天定时倒出本地数据,替换远程数据库数据的脚本,需要的朋友可以参考下
    2014-03-03
  • Shell编程之特殊变量和扩展变量详解

    Shell编程之特殊变量和扩展变量详解

    这篇文章主要给大家介绍了关于Shell编程教程之特殊变量和扩展变量的相关资料,文中通过示例代码介绍的非常详细,对大家学习或者使用shell具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2018-11-11
  • linux下mysql如何自动备份shell脚本

    linux下mysql如何自动备份shell脚本

    对任何一个已经上线的网站站点来说,数据备份都是必须的。无论版本更新还是服务器迁移,备份数据的重要性不言而喻。人工备份数据的方式不单耗费大量时间和精力,还灰常不专业。下面小编给大家分享linux下mysql自动备份shell脚本,需要的朋友可以参考下
    2015-09-09
  • 总结的7个shell字符串操作方法和实例分享

    总结的7个shell字符串操作方法和实例分享

    这篇文章主要介绍了总结的7个shell字符串操作方法和实例分享,包括长度获取、查找位置、字符截取、匹配查询等,需要的朋友可以参考下
    2014-07-07
  • 实现android自动化测试部署与运行Shell脚本分享

    实现android自动化测试部署与运行Shell脚本分享

    这篇文章主要介绍了实现android自动化测试部署与运行Shell脚本分享,本文直接给出实现代码,需要的朋友可以参考下
    2015-03-03
  • UNIX sh(Bourne Shell)脚本里面使用数组的两种方法

    UNIX sh(Bourne Shell)脚本里面使用数组的两种方法

    这篇文章主要介绍了UNIX sh(Bourne Shell)脚本里面使用数组的两种方法 ,需要的朋友可以参考下
    2016-12-12
  • Linux文件查找和解压缩命令详解

    Linux文件查找和解压缩命令详解

    这篇文章主要介绍了Linux文件查找和解压缩命令,文件搜索查找包括按照名字搜索,按照文件所属`主用户`搜索,本文给大家介绍的非常详细,感兴趣的朋友跟随小编一起看看吧
    2024-02-02

最新评论