The Cat command in Linux

In this article, please read Tipsmake to learn how to use the cat command useful with examples in Linux.

The general syntax of the Cat command in Linux

cat [OPTION] [FILE] .

Specific uses with the Cat command (with examples)

1. Display the contents of the file

In the example below, the command will display the contents of the / etc / passwd file.

# cat / etc / passwd root: x: 0: 0: root: / root: / bin / bash bin: x: 1: 1: bin: / bin: / sbin / nologin narad: x: 500: 500 :: / home / narad: / bin / bash

2. View the contents of multiple files in Terminal

In the example below, the command will display the contents of the test and test1 files in Terminal.

# cat test test1 Hello everybody Hi world,

3. Create the file using the Cat command

We will create a file called test2 with the following command:

# cat> test2

Wait for user input, enter the desired text and press CTRL + D (hold down Ctrl and enter d) to exit. The text will be written in the test2 file. You can view the contents of the file with the following cat command.

# cat test2 hello everyone, how do you do?

4. Use the Cat command with more and less options

If the file has a large amount of content that does not fit the output terminal and the screen scrolls up very quickly, you can use the more and less parameters with the cat command as follows:

# cat song.txt | more # cat song.txt | less

5. Display line numbers in file

With the -n option, you can see the line number of the song.txt file in the output terminal:

# cat -n song.txt 1 "Heal The World" 2 There's A Place In 3 Your Heart 4 And I Know That It Is Love 5 And This Place Could 6 Be Much 7 Brighter Than Tomorrow 8 And If You Really Try 9 You ' ll Find There's No Need 10 To Cry 11 In This Place You'll Feel 12 There's No Hurt Or Sorrow

6. Display $ at the end of file

In the section below, you can see that with the -e option, $ is displayed at the end of a line and in the space between paragraphs. This option is useful for squeezing multiple lines into one line.

# cat -e test hello everyone, how do you do? $ $ Hey, am fine. $ How's your training going on? $ $

7. Show tab-separated lines in the file

In the output below you can see the space TAB filled with the character ^ I.

# cat -T test hello ^ Ieveryone, how do you do? Hey, ^ Iam fine. ^ I ^ IHow's your training ^ Igoing on? Let's do ^ Isome practice in Linux.

8. Display multiple files at the same time

In the example below, there are 3 test files, test1 and test2. You can view the contents of those files as shown above. Remember to separate each file with semicolon.

# cat test; cat test1; cat test2 This is test file This is test1 file. This is test2 file.

9. Use standard output with the redirect operator

You can redirect the standard output of a file to a new file other than an existing file with the> symbol. Be careful, the existing content of test1 will be overwritten by the content of the test file.

# cat test> test1

10. Append the standard output with the redirect operator

Append the contents of the existing file with the >> symbol. Here, the content of the test file will be added to the end of the test1 file.

# cat test >> test1

11. Standard input redirection with redirect operator

When you use redirection with standard input <, the command uses the test2 file as input to a command and the output will be displayed in a terminal.

# cat 

12. Redirect multiple outputs to a single file

This will create a file called test 3 and all output will be redirected to the newly created file.

# cat test test1 test2> test3

13. Arrange the content of multiple files in a single file

This will create a test4 file and the output of the cat command will be sorted for sorting and the results will be redirected to a newly created file.

# cat test test1 test2 test3 | sort> test4

This article has covered the basic commands that can help you discover the cat command.

Wish you successful application!

4 ★ | 1 Vote

May be interested

  • How to limit access to su command in LinuxHow to limit access to su command in Linux
    if you have added linux to your data center or are just using a single linux machine for your business, you need to make sure it is as secure as possible.
  • 12 things Linux is easier to do in the command line than graphical software12 things Linux is easier to do in the command line than graphical software
    graphical user interfaces (guis) were invented to make life easier for computer users, so it's very common to use them to perform most everyday tasks.
  • 11 df commands in Linux (with example)11 df commands in Linux (with example)
    with the df command in linux, you can easily check your system for details about disk space and file size. these 12 examples of df commands will show you how to do them in detail.
  • How to use the Linux command line on Android with TermuxHow to use the Linux command line on Android with Termux
    android is a very operating system 'capacity with more and more desktop accessibility applications. however, sometimes you want to make some things on android that can be as easy as desktop. fortunately, you can use the termux tool, which builds on the existing infrastructure and provides a command line environment that allows you to install real linux applications on your android device.
  • Instructions for using find command in LinuxInstructions for using find command in Linux
    the find command is one of the most important and handy commands on a linux system. as its name suggests, the command can find files on a linux pc based on a variety of conditions and variables you set.
  • How to use the history command in LinuxHow to use the history command in Linux
    as you spend more and more time in terminal sessions, you will constantly find new commands that make everyday tasks more efficient. the gnu history command is one of them.
  • Instructions for using pstree command on LinuxInstructions for using pstree command on Linux
    pstree is a powerful and useful command to display processes running in linux. like the ps command, it shows all the processes that are currently active on your login system. the main difference is that when running the pstree command, processes are organized into tree sorting instead of lists like using the ps command.
  • The dd command in Linux, How to use the dd commandThe dd command in Linux, How to use the dd command
    dd is a command line utility for unix-like and unix operating systems, with the main purpose of converting and copying files.
  • Use the Top command to see the processes running in LinuxUse the Top command to see the processes running in Linux
    the top command in linux is used to display all processes running in the linux environment. this tutorial shows you how to use the top command through explaining the various parameters available and the content they display.
  • How to use the dmesg command in LinuxHow to use the dmesg command in Linux
    the dmesg command has been used to troubleshoot server and linux desktops for decades. it's time to start using this handy command!