TranslateProject/sources/tech/20230627.0 ⭐️⭐️ Using cat Command in Linux.md

252 lines
9.1 KiB
Markdown
Raw Normal View History

[#]: subject: "Using cat Command in Linux"
[#]: via: "https://itsfoss.com/cat-command/"
[#]: author: "Sagar Sharma https://itsfoss.com/author/sagar/"
[#]: collector: "lkxed"
[#]: translator: " "
[#]: reviewer: " "
[#]: publisher: " "
[#]: url: " "
Using cat Command in Linux
======
The cat command is used to print the file contents of text files.
At least, that's what most Linux users use it for and there is nothing wrong with it.
Cat actually stands for 'concatenate' and was created to [merge text files][1]. But withsingle argument, it prints the file contents. And for that reason, it is a go-to choice for users to read files in the terminal without any additional options.
### Using the cat command in Linux
To use the cat command, you'd have to follow the given command syntax:
```
cat [options] Filename(s)
```
Here,
- `[options]` are used to modify the default behavior of the cat command such as using the `-n` option to get numbers for each line.
- `Filename` is where you'll enter the filename of the file that you want to work with.
To make things easy, I will be using a text file named `Haruki.txt` throughout this guide which contains the following text lines:
```
Hear the Wind Sing (1979)
Pinball, 1973 (1980)
A Wild Sheep Chase (1982)
Hard-Boiled Wonderland and the End of the World (1985)
Norwegian Wood (1987)
Dance Dance Dance (1990)
South of the Border, West of the Sun (1992)
The Wind-Up Bird Chronicle (1994)
Sputnik Sweetheart (1999)
Kafka on the Shore (2002)
After Dark (2004)
1Q84 (2009-2010)
Colorless Tsukuru Tazaki and His Years of Pilgrimage (2013)
Men Without Women (2014)
Killing Commendatore (2017)
```
So, what will be the output when used without any options? Well, let's have a look:
```
cat Haruki.txt
```
![use cat command in Linux][2]
As you can see, it printed the whole text file!
But you can do a lot more than just this. Let me show you some practical examples.
#### 1. Create new files
Most Linux users use the touch command to [create new files][3] but the same can be done using the cat command too!
The cat command has one advantage over the touch command in this case, as you can add text to the file while creating. Sounds cool. Isn't it?
To do so, you'd have to use the cat command by appending the filename to the `>` as shown:
```
cat > Filename
```
For example, here, I created a file named `NewFile.txt`:
```
cat > NewFile.txt
```
Once you do that, there'll be a blinking cursor asking you to write something and finally, you can use `Ctrl + d` to save the changes.
**If you wish to create an empty file, then just press the `Ctrl + d` without making any changes.**
![Using cat command][4]
That's it! Now, you can use the ls command to show the [contents of the current working directory][5]:
![use the ls command to list the contents of the current working directory][6]
#### 2. Copy the file contents to a different file
Think of a scenario where you want to redirect the file content of **FileA** to the **FileB**
Sure, you can copy and paste. But what if there are hundreds or thousands of lines?
Simple. You use the cat command with the redirection of data flow. To do so, you'd have to follow the given command syntax:
```
cat FileA > FileB
```
> 🚧 If you use the above syntax to redirect file contents, it will erase the file contents of the FileB and then will redirect the file contents of the FileA.
For example, I will be using two text files FileA and FileB which contains the following:
![check the file contents using the cat command][7]
And now, if I use the redirection from FileA to FileB, it will remove the data of FileB and then redirect the data of FileA:
```
cat FileA > FileB
```
![redirect the file content using the cat command][8]
Similarly, you can do the same with multiple files:
```
cat FileA FileB > FileC
```
![redirect file content of multiple files using the cat command][9]
As you can see, the above command removed the data of FileC and then redirected the data of FileA and FileB.
#### Append the content of one file to another
There are times when you want to append data to the existing data and in that case, you'll have to use the `>>` instead of single `>`:
```
cat FileA >> FileB
```
For example, here, I will be redirecting two files `FileA` and `FileB` to the `FileC`:
```
cat FileA.txt FileB.txt >> FileC.txt
```
![redirect file content without overriding using the cat command][10]
As you can see, it preserved the data of the `FileC.txt` and the data was appended at the end of it.
> 💡 You can use the`>>`to add new lines to an existing file. Use`cat >> filename`and start adding the text you want and finally save the changes with`Ctrl+D`.
#### 4. Show the numbers of line
You may encounter such scenarios where you want to see the number of lines, and that can be achieved using the `-n` option:
```
cat -n File
```
For example, here, I used the `-n` option with the `Haruki.txt`:
![get the number of the lines in the cat command][11]
#### 5. Remove the blank lines
Left multiple blank lines in your text document? The cat command will fix it for you!
To do so, all you have to do is use the `-s` flag.
But there's one downside of using the `-s` flag. You're still left with one blank space:
![remove blank lines with the cat command][12]
As you can see, it works but the results are close to the expectations.
So how would you remove all the empty lines? By piping it to the grep command:
```
cat File | grep -v '^$'
```
Here, the `-v` flag will filter out the results as per `the`specified pattern and `'^$'` is a regular expression that matches the empty lines.
And here are the results when I used it over the `Haruki.txt`:
```
cat Haruki.txt | grep -v '^$'
```
![remove all the blank lines in text files using the cat command piped with grep regular expression][13]
Once you have the perfect output, you can redirect it to a file to save the output:
```
cat Haruki.txt | grep -v '^$' > File
```
![save output of cat command by redirection][14]
### That's what you've learned so far
Here's a quick summary of what I explained in this tutorial:
| Command | Description |
| :- | :- |
| `cat <Filename>` | Prints the file content to the terminal. |
| `cat >File` | Create a new file. |
| `cat FileA > FileB` | File contents of the `FileB` will be overridden by the `FileA`. |
| `cat FileA >> FileB` | File contents of the `FileA` will be appended at the end of the `FileB`. |
| `cat -n File` | Shows the number of lines while omitting the file contents of the File. |
| `cat File | more` | Piping the cat command to the more command to deal with large files. Remember, it won't let you scroll up! |
| `cat File | less` | Piping the cat command to the less command, which is similar to above, but it allows you to scroll both ways. |
| `cat File | grep -v '^$'` | Removes all the empty lines from the file. |
### 🏋It's time to exercise
If you learned something new, executing it with different possibilities is the best way to remember.
And for that purpose, here are some simple exercises you can do with the cat command. They will be super basic as cat too is[one of the most basic commands][15].
For practice purposes, you can [use our text files from GitHub.][16]
- How would you create an empty file using the cat command?
- Redirect output produced by the cat command to a new file `IF.txt`
- Can you redirect three or more file inputs to one file? If yes, then how?
--------------------------------------------------------------------------------
via: https://itsfoss.com/cat-command/
作者:[Sagar Sharma][a]
选题:[lkxed][b]
译者:[译者ID](https://github.com/译者ID)
校对:[校对者ID](https://github.com/校对者ID)
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出
[a]: https://itsfoss.com/author/sagar/
[b]: https://github.com/lkxed/
[1]: https://linuxhandbook.com:443/merge-files/
[2]: https://itsfoss.com/content/images/2023/06/use-cat-command-in-Linux.png
[3]: https://itsfoss.com/create-files/
[4]: https://itsfoss.com/content/images/2023/06/Cat.svg
[5]: https://itsfoss.com/list-directory-content/
[6]: https://itsfoss.com/content/images/2023/06/use-the-ls-command-to-list-the-contents-of-the-current-working-directory.png
[7]: https://itsfoss.com/content/images/2023/06/check-the-file-contents-using-the-cat-command.png
[8]: https://itsfoss.com/content/images/2023/06/redirect-the-file-content-using-the-cat-command.png
[9]: https://itsfoss.com/content/images/2023/06/redirect-file-content-of-multiple-files-using-the-cat-command.png
[10]: https://itsfoss.com/content/images/2023/06/redirect-file-content-without-overriding-using-the-cat-command.png
[11]: https://itsfoss.com/content/images/2023/06/get-the-number-of-the-lines-in-the-cat-command.png
[12]: https://itsfoss.com/content/images/2023/06/remove-blank-lines-with-the-cat-command.png
[13]: https://itsfoss.com/content/images/2023/06/remove-all-the-blank-lines-in-text-files-using-the-cat-command-piped-with-grep-regular-expression.png
[14]: https://itsfoss.com/content/images/2023/06/save-output-of-cat-command-by-redirection.png
[15]: https://learnubuntu.com:443/top-ubuntu-commands/
[16]: https://github.com:443/itsfoss/text-files