Linux/Unix - 六大Cat命令实例及教程

199 阅读1分钟

Linux/Unix中的基本Cat命令示例

Cat命令是每个Unix/Linux用户都使用的一个简单命令。它是Linux/Unix系统中经常使用的命令之一。Cat命令在bash、PowerShell和shell选项上工作。

Cat命令根据不同的使用类型表现出不同的效果。

通过使用cat命令,我们可以实现以下功能

1.使用cat命令读取文件 2.创建文件 使用cat命令 3.使用cat命令复制文件内容。

猫命令适用于UNIX、Linux以及Ubuntu终端。

Cat命令的语法

Cat (options) (one or more file name)

让我们看看下面的各种cat命令的例子。

在Linux/Unix中使用cat命令读取或查看文件的例子

这是一个简单的命令,用于读取文件的内容,并将文件内容输出到标准输出,如控制台

$cat cloud.txt

This is test file contains test content.

```If it is a small file, we can view it using cat command, but file size is large we can use more or less with pipe options in cat command.

## How to create a file using Cat command in Unix

Cat command is a basic command to create files. To create a file demo.txt

$ cat >demo.txt 这是测试文件,包含测试内容。CTRL +D


## How to merge files using cat command in Linux/Unix

Cat command also used to merge different files and create a new file

$ cat file1 file2 > output.txt

First file1 data is copied to output.txt and next file2 data is copied to end of file1’s data.

## How to view the line numbers of each line using cat command

In Java programs, if any error happens, Error/Exception thrown with a print stack trace which will give filename as well as line number where the exception has occurred. In that case, to view the java code with line numbers, we can use cat command with option –n

$ cat -n cloud.txt 1 这是测试文件,包含2个测试内容。


## Append/concatenate data to an existing file using cat command

$ cat "cloud.txt

We have to press CTRL+D option to save/append the new data to existing data in a file

## How to view file with showing spaces, tabs using cat command with examples.

In any text Editors, we have the option to be enabled to show spaces and tabs codes

Cat has also had the option to view spaces and tabs and end of lines using option –T

$ cat -T filename


## Cat command options

Basic cat command has different options, can be used with all flavors of Linux like AIX

\-T output spaces, tabs, and End of lines characters in the file
\-s suppress the multiple blank lines
\-n output number on each line of the file

That’s it from the usage of cat command in the daily life of software development.

Please click +1 if you like this post.