Linux 终端命令之文件浏览(1) cat

简介: Linux 终端命令之文件浏览(1) cat


Linux 文件浏览命令

cat, more, less, head, tail,此五个文件浏览类的命令皆为外部命令。

hann@HannYang:~$ which cat
/usr/bin/cat
hann@HannYang:~$ which more
/usr/bin/more
hann@HannYang:~$ which less
/usr/bin/less
hann@HannYang:~$ which head
/usr/bin/head
hann@HannYang:~$ which tail
/usr/bin/tail

(1) cats

英文帮助

NAME

      cat - concatenate files and print on the standard output

SYNOPSIS

      cat [OPTION]... [FILE]...

DESCRIPTION

      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.

AUTHOR

      Written by Torbjorn Granlund and Richard M. Stallman.

REPORTING BUGS

      GNU coreutils online help:

      Report cat translation bugs to

COPYRIGHT

      Copyright   ©   2018   Free   Software   Foundation,  Inc.   License  GPLv3+:  GNU  GPL  version  3  or  later

      .

      This is free software: you are free to change and redistribute it.  There is NO WARRANTY, to the  extent  per‐mitted by law.


中文注解

cat  连接文件并在标准输出上打印

可以用它来显示文件内容,创建文件,还可以用它来显示控制字符。

cat命令的一般形式为:

cat [options] filename1 ... filename2 ...

帮助
hann@HannYang:~$ cat --help
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: <https://www.gnu.org/software/coreutils/>
Report cat translation bugs to <https://translationproject.org/team/>
Full documentation at: <https://www.gnu.org/software/coreutils/cat>
or available locally via: info '(coreutils) cat invocation'
版本
hann@HannYang:~$ cat --version
cat (GNU coreutils) 8.30
Copyright (C) 2018 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <https://gnu.org/licenses/gpl.html>.
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
Written by Torbjorn Granlund and Richard M. Stallman.
注释

1. 如果希望显示名为myfile的文件,可以用:

$ cat myfile

2. 在使用cat命令时要注意,它不会在文件分页符处停下来;它会一下显示完整个文件。如果希望每次显示一页,可以使用more命令或把cat命令的输出通过管道传递到另外一个具有分页功能的命令中,如:

$ cat myfile | more

上面这个写法与 $ more myfile 等效

3. 参数 -n 或 -number 可以自动在每一行的显示内容前标注上行号,如:

hann@HannYang:~$ cat -n cmd1.txt
     1  alias - Define or display aliases.
     2  alias: alias [-p] [name[=value] ... ]
     3  bg - Move jobs to the background.
     4  bg: bg [job_spec ...]
     5  bind - Set Readline key bindings and variables.
     6  ......

4. 如果希望显示myfile1、myfile2、myfile3这三个文件,可以用:

$ cat myfile1 myfile2 myfile3

5. 如果希望创建一个名为bigfile的文件,该文件包含上述三个文件的内容,可以把上面命令的输出重定向到新文件中,这就是cat连接文件的功能

$ cat myfile1 myfile2 myfile3 > bigfile

6. 有一点要提醒的是,如果在敲入了cat以后就直接按回车,该命令会等你输入字符。也是说帮助中说的“With no FILE, or when FILE is -, read standard input.”,没有文件时则从标准输入(默认设备就是键盘)中读取内容。如果你本来就是要输入一些字符,那么它除了会在你输入时在屏幕上显示以外,还会再回显这些内容;最后按结束输入即可。

hann@HannYang:~$ cat
123456789
123456789
abcdefg
abcdefg

7. 如果希望创建一个新文件,并向其中输入一些内容,只需使用cat命令把标准输出重定向到该文件中,这时cat命令的输入是标准输入—键盘,你输入一些文字,输入完毕后按结束输入。这就是一个非常简单的文字编辑器!

hann@HannYang:~$ cat > myfile
123456789
abcdefg
hann@HannYang:~$ cat myfile
123456789
abcdefg

上面这个功能,与Dos命令中的copy con file类似:

C:\Users\boyso>copy con myfile

123456

abcdef

^Z

已复制         1 个文件。

C:\Users\boyso>type myfile

123456

abcdef

Dos系统是用退出输出的。

8.  连接多个文件时,还能用键盘输入一些内容。即是前面第6点中提到的“or when FILE is -” ,也就当文件参数有“-”减号时,就从键盘输入内容直到按结束输入。

例如以下操作,连接文件aa,bb成cc,并且两个文件间用"==========="隔开:

hann@HannYang:~$ cat aa
abcd
efg
hann@HannYang:~$ cat bb
123456
7890
hann@HannYang:~$ cat aa - bb > cc
===========
hann@HannYang:~$ cat cc
abcd
efg
===========
123456
7890

目录
打赏
0
0
0
0
74
分享
相关文章
【Linux】进程IO|系统调用|open|write|文件描述符fd|封装|理解一切皆文件
本文详细介绍了Linux中的进程IO与系统调用,包括 `open`、`write`、`read`和 `close`函数及其用法,解释了文件描述符(fd)的概念,并深入探讨了Linux中的“一切皆文件”思想。这种设计极大地简化了系统编程,使得处理不同类型的IO设备变得更加一致和简单。通过本文的学习,您应该能够更好地理解和应用Linux中的进程IO操作,提高系统编程的效率和能力。
62 34
|
4天前
|
Linux文件与目录的日常
目录的切换 一般使用(”pwd“)显示当前所在的目录 比如:当前目录是在home下面的,与用户名相同的文件夹,可以使用(”cd“)命令来切换目录; 进入下载目录(”cd home/a/下载“)这种从给目录开头的一长串路经”叫做绝对路径“; 进入图片目录(”cd .. /图片/“)".."代表当前路径的上级路径,相对于当前的目录而言的”叫做相对路径“,(”.“)代表当前路径; 如果,想快速切换,上一个所在目录可以(”cd - / cd..“); 如果,想快速切换,追原始的目录可以(”cd --“); 查看目录及文件
32 14
FastExcel:开源的 JAVA 解析 Excel 工具,集成 AI 通过自然语言处理 Excel 文件,完全兼容 EasyExcel
FastExcel 是一款基于 Java 的高性能 Excel 处理工具,专注于优化大规模数据处理,提供简洁易用的 API 和流式操作能力,支持从 EasyExcel 无缝迁移。
268 9
FastExcel:开源的 JAVA 解析 Excel 工具,集成 AI 通过自然语言处理 Excel 文件,完全兼容 EasyExcel
|
2月前
|
golang编译成Linux可运行文件
本文介绍了如何在 Linux 上编译和运行 Golang 程序,涵盖了本地编译和交叉编译的步骤。通过这些步骤,您可以轻松地将 Golang 程序编译成适合 Linux 平台的可执行文件,并在目标服务器上运行。掌握这些技巧,可以提高开发和部署 Golang 应用的效率。
301 14
|
2月前
|
linux积累-core文件是干啥的
核心文件是Linux系统在程序崩溃时生成的重要调试文件,通过分析核心文件,开发者可以找到程序崩溃的原因并进行调试和修复。本文详细介绍了核心文件的生成、配置、查看和分析方法
168 6
|
2月前
|
linux之core文件如何查看和调试
通过设置和生成 core 文件,可以在程序崩溃时获取详细的调试信息。结合 GDB 等调试工具,可以深入分析 core 文件,找到程序崩溃的具体原因,并进行相应的修复。掌握这些调试技巧,对于提高程序的稳定性和可靠性具有重要意义。
466 6
|
9月前
|
linux(十四)文件和目录相关命令cat和more
linux(十四)文件和目录相关命令cat和more
93 0
百度搜索:蓝易云【Linux 命令 - cat 和 tail】
总结: 通过cat命令,可以查看文件的内容、连接文件以及创建文件。而tail命令则主要用于查看文件的末尾内容,可以灵活设置显示行数,还能实时监视文件的变化。这两个命令在Linux系统中是非常有用的文件处理工具。
149 0
Linux命令教程:使用cat命令查看和处理文件
Linux命令教程:使用cat命令查看和处理文件
1099 0
【Linux命令200例】cat命令的妙用和实战技巧
cat命令是一个常用的`Linux`命令,用于连接文件并打印到标准输出。`cat`命令可以同时处理多个文件,并且可以将内容追加到指定文件中。
293 0
【Linux命令200例】cat命令的妙用和实战技巧
AI助理

你好,我是AI助理

可以解答问题、推荐解决方案等