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文本查看命令及其选项内容请搜索我们以前的文章或继续浏览下面的相关文章希望大家以后多多支持我们!

(0)

相关推荐

  • 一天一个shell命令 linux文本操作系列-tac,rev命令详解

    说明: tac和rev没有必然联系,唯一相同的是,他们是"反派".tac其实是cat的反写,cat正常输出,那tac就倒着输出,不过从行的最后一行输出,那rev呢?rev是每行的最后一个字母到第一个字母倒序输出. 简单实例 tac命令 文件过滤分割与合并 tac命令用于将文件已行为单位的反序输出,即第一行最后显示,最后一行先显示. 语法 tac(选项)(参数) 选项 -a或--append:将内容追加到文件的末尾: -i或--ignore-interrupts:忽略中断信号. 参数 文

  • 详解Linux文本文件与WIN文本文件换行格式转换命令

    详解Linux文本文件与WIN文本文件换行格式转换命令 前言: 有 时在WIN下编辑好的脚本文件上传到LINUX服务器中不能正常执行,开始误认为是LINUX配置问题,后来发现,是WIN与LINUX存储文件时的换行 符标志不同造成的.在DOS使用的换行符为 ^M$,我们称为CR与LF两个符号.而在Linux中,则仅有LF ($) 这个换行符. 可以用如下命令完成格式转换:$dos2unix,$unix2dos.但这两个命令在UBUNTU发行版本中不存在,可通过: $sudo apt-get ins

  • 一天一个shell命令 linux文本内容操作系列-sed命令详解

    说明: sed是stream editor(流编辑器)的缩写.它能够完美匹配正则表达式.sed和awk是文件编辑最重要的两个命令了.尤其涉及到了很多正则表达式的问题,笔者不敢也有点犯怵,试着写写. 实例: 1.替换文件中的字符串 $sed -i 's/text/replace/g' file #如果不加g结尾,则替换每一行的第一个 #如果只是打印,去掉-i 2.忽略前N处匹配,从N+1出开始替换 $sed -i 's/text/replace/2g' file #在g前面加入数字N 3.移除空白

  • linux文本过滤grep基础命令介绍(5)

    在linux中经常需要对文本或输出内容进行过滤,最常用的过滤命令是grep grep [OPTIONS] PATTERN [FILE...] grep按行检索输入的每一行,如果输入行包含模式PATTERN,则输出这一行.这里的PATTERN是正则表达式(参考前一篇,本文将结合grep一同举例). 输出文件/etc/passwd中包含root的行: [root@centos7 temp]# grep root /etc/passwd root:x:0:0:root:/root:/bin/bash

  • linux下/etc/passwd文件的选项说明

    /etc/passwd 文件是一个纯文本文件,每行采用了相同的格式: name:password:uid:gid:comment:home:shell 它们的含义如下: 域 说明 name 用户登录名 password 用户口令.此域中的口令是加密的.当用户登录系统时,系统对输入的口令采取相同的算法,与此域中的内容进行比较.如果此域为空,表明该用户登录时不需要口令. uid 指定用户的 UID.用户登录进系统后,系统通过该值,而不是用户名来识别用户. gid GID.如果系统要对相同的一群人赋予

  • 一天一个shell命令 linux文本内容操作系列-awk命令详解

    简介 awk是一个强大的文本分析工具,相对于grep的查找,sed的编辑,awk在其对数据分析并生成报告时,显得尤为强大.简单来说awk就是把文件逐行的读入,以空格为默认分隔符将每行切片,切开的部分再进行各种分析处理. awk有3个不同版本: awk.nawk和gawk,未作特别说明,一般指gawk,gawk 是 AWK 的 GNU 版本. awk其名称得自于它的创始人 Alfred Aho .Peter Weinberger 和 Brian Kernighan 姓氏的首个字母.实际上 AWK

  • linux shell命令行选项与参数用法详解

    问题描述:在linux shell中如何处理tail -n 10 access.log这样的命令行选项?在bash中,可以用以下三种方式来处理命令行参数,每种方式都有自己的应用场景.1,直接处理,依次对$1,$2,...,$n进行解析,分别手工处理:2,getopts来处理,单个字符选项的情况(如:-n 10 -f file.txt等选项):3,getopt,可以处理单个字符选项,也可以处理长选项long-option(如:--prefix=/home等).总结:小脚本手工处理即可,getopt

  • 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

  • linux软件版本管理命令update-alternatives使用详解

    linux软件版本管理命令update-alternatives使用详解 update-alternatives 命令用于处理linux系统中软件版本的切换,在各个linux发行版中均提供了该命令,命令参数略有区别,但大致是一样的. 1. 注册软件 以jdk为例,安装了jdk以后,先要在update-alternatives工具中注册: # update-alternatives --install /usr/bin/java java /opt/jdk1.8.0_91/bin/java 200

  • Linux下alias命令的用法详解

    1.alias的使用 alias命令用来设置指令的别名.我们可以使用该命令可以将一些较长的命令进行简化. 用alias 短命令='原命令 选项' alias 138ssh= 'ssh -i ~/.ssh/138.pem ec2-user@192.168.21.138' 这下子就可以直接使用138ssh 来代替原来ssh -i ~/.ssh/138.pem ec2-user@192.168.21.138 查看当前所有的alias alias-p 删除一个alias unalias 命令 然后我就开

  • Linux 的cp命令及示例详解

    最近学习linux命令,现在把cp命令整理出来,以便日后查询. 功能: 复制文件或目录 说明: cp指令用于复制文件或目录,如同时指定两个以上的文件或目录,且最后的目的地是一个已经存在的目录,则它会把前面指定的所有文件或目录复制到此目录中.若同时指定多个文件或目录,而最后的目的地并非一个已存在的目录,则会出现错误信息 参数: -a 或 --archive     此参数的效果和同时指定"-dpR"参数相同 -b 或 --backup      删除.覆盖目的文件先备份,备份的文件或目录

  • 详解Linux中zip压缩和unzip解压缩命令及使用详解

    下面给大家介绍下Linux中zip压缩和unzip解压缩命令详解 1.把/home目录下面的mydata目录压缩为mydata.zip zip -r mydata.zip mydata #压缩mydata目录 2.把/home目录下面的mydata.zip解压到mydatabak目录里面 unzip mydata.zip -d mydatabak 3.把/home目录下面的abc文件夹和123.txt压缩成为abc123.zip zip -r abc123.zip abc 123.txt 4.把

  • Linux基础命令之mktemp详解

    mktemp 创建临时文件或者目录,这样的创建方式是安全的.此命令的适用范围:RedHat.RHEL.Ubuntu.CentOS.SUSE.openSUSE.Fedora. 1.语法 mktemp [选项]  [TEMPLATE] 2.选项列表 选项 说明 --version 显示命令版本信息 --help 显示帮助信息 -d | --directory 创建目录 -u | --dry-run 不要创建任何东西,只要打印一个名字(不安全) -q | --quiet 发生错误的时候不显示提示信息

  • Linux链接命令的实例详解

    Linux链接命令的实例详解 一 语法 ln -s [源文件] [目标文件] 命令英文含义:link 功能描述:生成链接文件 选项:-s 创建软链接 二 硬链接特征 原文件和硬链接文件删除其中任何一个都没问题. 三 硬链接实战 [root@localhost ~]# cd test [root@localhost test]# ls bcd [root@localhost test]# ln bcd abc.hard [root@localhost test]# ll total 0 -rw-r

  • Linux tar 命令用法实例详解

    tar 命令可以为linux的文件和目录创建档案.利用 tar,可以为某一特定文件创建档案(备份文件),也可以在档案中改变文件,或者向档案中加入新的文件.tar 最初被用来在磁带上创建档案,现在,用户可以在任何设备上创建档案.利用 tar 命令,可以把一大堆的文件和目录全部打包成一个文件,这对于备份文件或将几个文件组合成为一个文件以便于网络传输是非常有用的. 首先要弄清两个概念:打包和压缩.打包是指将一大堆文件或目录变成一个总的文件:压缩则是将一个大的文件通过一些压缩算法变成一个小文件. 为什么

  • Python执行dos和Linux命令的方法详解

    在实际开发中,有时为了方便,可能需要执行dos命令或者Linux命令.比如说执行某些shell脚本,上传下载一些文件,执行adb命令等跨语言,加压包,解压包等跨操作系统的场景.这样能大大加强多个平台和操作系统之间的关联性. windows: 案例1:弹窗式执行dos命令(与打开cmd执行一模一样) # -*- coding: utf-8 -*- import os # 解决打印时,部分中文乱码问题 os.popen('chcp 65001') # 查看当前路径 command1 = "chdir

  • Linux系统下安装rz/sz命令及使用说明(详解)

    对于经常使用Linux系统的人员来说,少不了将本地的文件上传到服务器或者从服务器上下载文件到本地,rz / sz命令很方便的帮我们实现了这个功能,但是很多Linux系统初始并没有这两个命令. 今天,我们就简单的讲解一下如何安装和使用rz.sz命令. 1.软件安装 root 账号登陆后,依次执行以下命令: cd /tmp wget http://www.ohse.de/uwe/releases/lrzsz-0.12.20.tar.gz tar zxvf lrzsz-0.12.20.tar.gz &

随机推荐