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

目录
相关文章
|
6天前
|
Linux
Linux系统之whereis命令的基本使用
Linux系统之whereis命令的基本使用
50 23
Linux系统之whereis命令的基本使用
|
20天前
|
网络协议 Unix Linux
深入解析:Linux网络配置工具ifconfig与ip命令的全面对比
虽然 `ifconfig`作为一个经典的网络配置工具,简单易用,但其功能已经不能满足现代网络配置的需求。相比之下,`ip`命令不仅功能全面,而且提供了一致且简洁的语法,适用于各种网络配置场景。因此,在实际使用中,推荐逐步过渡到 `ip`命令,以更好地适应现代网络管理需求。
33 11
|
2月前
|
Linux Shell
Linux 10 个“who”命令示例
Linux 10 个“who”命令示例
99 14
Linux 10 个“who”命令示例
|
2月前
|
Ubuntu Linux
Linux 各发行版安装 ping 命令指南
如何在不同 Linux 发行版(Ubuntu/Debian、CentOS/RHEL/Fedora、Arch Linux、openSUSE、Alpine Linux)上安装 `ping` 命令,详细列出各发行版的安装步骤和验证方法,帮助系统管理员和网络工程师快速排查网络问题。
203 20
|
2月前
|
Linux
linux查看目录下的文件夹命令,find查找某个目录,但是不包括这个目录本身?
通过本文的介绍,您应该对如何在 Linux 系统中查看目录下的文件夹以及使用 `find` 命令查找特定目录内容并排除该目录本身有了清晰的理解。掌握这些命令和技巧,可以大大提高日常文件管理和查找操作的效率。 在实际应用中,灵活使用这些命令和参数,可以帮助您快速定位和管理文件和目录,满足各种复杂的文件系统操作需求。
140 8
|
2月前
|
网络协议 Linux 应用服务中间件
kali的常用命令汇总Linux
kali的常用命令汇总linux
128 7
|
3月前
|
监控 网络协议 Linux
Linux netstat 命令详解
Linux netstat 命令详解
|
2天前
|
Java 程序员 开发者
Java社招面试题:一个线程运行时发生异常会怎样?
大家好,我是小米。今天分享一个经典的 Java 面试题:线程运行时发生异常,程序会怎样处理?此问题考察 Java 线程和异常处理机制的理解。线程发生异常,默认会导致线程终止,但可以通过 try-catch 捕获并处理,避免影响其他线程。未捕获的异常可通过 Thread.UncaughtExceptionHandler 处理。线程池中的异常会被自动处理,不影响任务执行。希望这篇文章能帮助你深入理解 Java 线程异常处理机制,为面试做好准备。如果你觉得有帮助,欢迎收藏、转发!
33 14
|
5天前
|
安全 Java 程序员
Java 面试必问!线程构造方法和静态块的执行线程到底是谁?
大家好,我是小米。今天聊聊Java多线程面试题:线程类的构造方法和静态块是由哪个线程调用的?构造方法由创建线程实例的主线程调用,静态块在类加载时由主线程调用。理解这些细节有助于掌握Java多线程机制。下期再见! 简介: 本文通过一个常见的Java多线程面试题,详细讲解了线程类的构造方法和静态块是由哪个线程调用的。构造方法由创建线程实例的主线程调用,静态块在类加载时由主线程调用。理解这些细节对掌握Java多线程编程至关重要。
34 13
|
6天前
|
安全 Java 开发者
【JAVA】封装多线程原理
Java 中的多线程封装旨在简化使用、提高安全性和增强可维护性。通过抽象和隐藏底层细节,提供简洁接口。常见封装方式包括基于 Runnable 和 Callable 接口的任务封装,以及线程池的封装。Runnable 适用于无返回值任务,Callable 支持有返回值任务。线程池(如 ExecutorService)则用于管理和复用线程,减少性能开销。示例代码展示了如何实现这些封装,使多线程编程更加高效和安全。

热门文章

最新文章