Tutorial using the Cat command on the Linux command line

The cat command is one of the most used commands in Linux. The name of the cat command comes from its name (chain) which is used to merge files. The command can read and serialize files, and write content to standard output. If no file is specified or if the input file name is specified as a single dash (-) Then the output will be read from the standard input.

The Cat command is commonly used to display the contents of one or more text files, combine files by appending content from one file to the end of another file, and create new files.

In this tutorial, we’ll show you how to use the paint command with practical examples in everyday life.

Cat . command syntax

Before discussing how to use the cat command, let’s start by reviewing the basic syntax.

The expression for the paint utility is as follows:

cat [OPTIONS] [FILE_NAMES]
  • OPTIONS Paint options. Uses cat --help To see all available options.
  • FILE_NAMES The name of the file or document

View file contents with paint

The most basic and most common use of the cat command is to read the contents of a file.

For example, the following command will display the contents of the file /etc/issue At the station:

cat /etc/issue

Redirect file content

Instead of output to stdout (on the screen), you can point it to the file.

The following command will copy the content file1.txt to me file2.txt using operators (>):

cat file1.txt > file2.txt

Usually you can use the command cp to copy files. If the file file2.txt not found, the cat command will create it. If file2.txt is present, the cat command will overwrite the file.

See also  How to sort du -h command based on file size

Use the operator (>>) to add content file1.txt to me file2.txt :

cat file1.txt >> file2.txt

The same as the previous player, if the file file2.txt Not found, it will be created. The difference is, if file2.txt Exist, then content file1.txt It will be added to file2.txt.

print font number

To display the contents of a file in line numbers, use the arguments -n:

cat -n /etc/lsb-release
1	DISTRIB_ID=Ubuntu
2	DISTRIB_RELEASE=18.04
3	DISTRIB_CODENAME=bionic
4	DISTRIB_DESCRIPTION="Ubuntu 18.04.1 LTS"

Printing line numbers with the Cat . command

Remove blank lines

use arguments -s To get rid of duplicate empty output lines:

cat -s file.txt

Show TAB . characters

use arguments -T To visually distinguish between tabs and spaces.

cat -T /etc/hosts
127.0.0.1^Ilocalhost
127.0.1.1^Iubuntu1804.localdomain

The TAB character will be displayed as ^I.

Show end of lines

To display invisible end of line characters, use the . argument -e:

cat -e /etc/lsb-release
DISTRIB_ID=Ubuntu$
DISTRIB_RELEASE=18.04$
DISTRIB_CODENAME=bionic$
DISTRIB_DESCRIPTION="Ubuntu 18.04.1 LTS"$

The end of the line will be displayed as . $.

Merge files with the Cat أمر command

When two or more file names are passed as arguments to the cat command, the contents of the file are bound. The Cat command reads the files in the order they are given in the media and displays the contents of the file in the same order.

For example, the following command will read the contents file1.txt And the file2.txt And display the result in the terminal:

cat file1.txt file2.txt

You can combine two or more text files and write them to a file.

The following command will collect the contents file1.txt And the file2.txt Write it in a new file gabunganfile.txtusing operators (>):

See also  Various ways to download ISO files on Linux systems

cat file1.txt file2.txt > gabunganfile.txt

If mergefile.txt does not exist, the cat command will create it automatically. Otherwise, the file will be overwritten file1.txt And the file2.txt and append the result to file3.txt to use the (>>The operator or operator:

cat file1.txt file2.txt >> file3.txt

If the file does not exist, the cat command will create it automatically.

When associating files with cat, you can use the same arguments as described in the previous section.

Creating files using the Cat . command

When creating small files, using cat is easier than using Vim, Sublime Text, Visual Studio Code, or any other text editor.

To create a new file, use the cat command followed by a redirection operator (‘>’) and the name of the file you want to create. Press Enter and type the text and once done press CTRL+D to save files.

In the following example, create a file with the name file1.txt:

cat > file1.txt

If the file is named file1.txt already exists, then the cat command will overwrite the file. Use the operator (‘>>‘) to append the output to an existing file

conclusion

For some people, using the command line is the fastest way to manage Linux, the cat command is one of the easiest ways to search the contents of a file without having to open/close a text editor.

Source link

About wahyuway

Check Also

How to install and configure Anaconda on CentOS 7

How to install and configure Anaconda on CentOS 7

Anaconda is the most popular machine learning and data science tool used in large scale …

Leave a Reply