Cat with lines linux

How the cat Command works on Linux

cat , short for concatenate, is used on Linux and Unix-based systems like MacOS for reading the contents of a file, concatenating the contents with other files, and for creating new, concatenated files. It’s also frequently used to copy the contents of files.

The syntax for cat is shown below, where x is the file name, and [OPTIONS] are optional settings which alter how cat works.

Getting the contents of a file using cat on Linux or MacOS

Using the cat command along with one file name, we can get the entire text content of a file. For example, the below command will output the content of my-file.txt into the terminal:

Similarly, we can see the contents of many files by separating them with a space. For example, the below line takes the content of my-file.txt , and my-new-file.txt , merges the content, and shows it in terminal:

cat my-file.txt my-new-file.txt 

Getting the contents of files with line numbers on Linux or MacOS

We can use the option -n to show line numbers. For example, the following command merges our two files, my-file.txt, and my-new-file.txt , and outputs the content with line numbers side by side. This is pretty useful for comparing files.

cat -n my-file.txt my-new-file.txt 

The output will look something like this:

 1 Content from my-file.txt 1 Some more content from my-new-file.txt 

Concatenating two files into a new file on Linux and MacOS

Since concatenate can output the contents of two files, we can use > again to merge two files into a totally new file. The below example takes my-file.txt and my-new-file.txt, merges their content, and puts it into a new file called my-combined-file.txt:

cat my-file.txt my-new-file.txt > my-combined-file.txt 

Putting Content from one file into another with Linux or MacOS

If all we want to do is put the contents of one file at the end of another, we can instead use >> . For example, the below command will take the content from my-file.txt, and put it at the end of my-new-file.txt, thus merging both files into my-new-file.txt:

cat my-file.txt >> my-new-file.txt 

Line Numbers

Note: if you use >> or > with the -n option, the line numbers will also be merged into your new concatenated file!

Читайте также:  Linux cannot find shared library

Creating an empty file on Linux or MacOS with cat

Since it’s so easy to create files with cat , we often use it to make new files. For example, the below code will create a blank file called my-file.txt, as we are concatenating a blank string into it:

How to show nonprintable characters on Linux or MacOS

Some documents or files may contain nonprintable characters. These are used to signal to applications how a file should be formatted — but they can sometimes mess up the format of files. To show nonprintable characters when using cat , we can use the -v option. This will show all nonprintable characters using caret notation, so that we can view them easily.

Nonprintable characters

All options for cat on Linux or MacOS

There are a bunch of other options which help us use cat to get the ouputs we want. We’ve already discussed -n for getting line numbers, and -v for nonprintable characters, but here are the others:

  • -b — numbers only non empty output lines, overriding -n .
  • -E — displays a $ at the end of every line.
  • -s — suppresses repeated, empty lines.
  • -T — displays tabs as ^I , so as to easily discriminate them from spaces.
  • -A — equivalent to writing -vET .

More Tips and Tricks for Linux

Источник

How to Use Linux Cat Command

The Linux cat command is used to print the contents of a text file. With the Linux cat command, you can print the contents of your c, java source file, Linux configuration files etc.

The cat command is available in every Linux distribution out there by default. So, you don’t have to install it separately.

In this article, I am going to show you how to use the Linux cat command. So, let’s get started.

Basic Usage of Linux Cat Command:

The basic and most common use of the Linux cat command is to use it without any command option.

For example, to view to contents of the /etc/hosts directory, run the cat command as follows:

As you can see, the contents of the /etc/hosts configuration file are printed on the screen.

Printing Line Numbers:

Let’s say, you want to print the contents of a Java source file on the terminal. You can use the cat command of course. But the cat command does not show line numbers by default. For a source file or a program, it is essential. Luckily, the cat command has -n option which you can use to display line numbers.

To display the contents along with the line number of the Java source file Welcome.java, run the Linux cat command as follows:

As you can see, the line numbers are displayed.

Numbering Only Non Blank Lines:

If you want to show line numbers for the lines that are not blank only, you can use the -b option of the Linux cat command.

In the previous Java source file Welcome.java, I’ve added some blank lines just to demonstrate how the -b option works.

As you can see, with the -n option, all the lines (including blank lines) are numbered.

With the -b option, only the lines that are not blank are numbered as you can see in the screenshot below.

Читайте также:  Linux and open office

Removing Repeating Empty Lines:

A file you’re trying to view may have lots of empty lines one after the other. This will make the output of cat command very long and annoying.

You can use the -s option of the Linux cat command to remove repeated empty lines as follows:

Printing Tab Characters:

In a source code file of a program, you may have used many tab characters. Fortunately, they are invisible by default. But, if you really need to see all the tab characters you have on your file, then you can use the -T option of the Linux cat command.

Where you might need this feature is when you want to replace all the tab characters with white spaces and you want to make sure that there are not any tab characters left.

To display all the tab characters in our Welcome.java source file, the Linux cat command can be used as follows:

As you can see, the tab characters are displayed as ^I.

Printing End of Line Characters:

If you want to print the EOL (End of Line) character which is represented by $, you can use the -E option of the Linux cat command.

For example, to print the EOL characters of Welcome.java, run the Linux cat command as follows:

As you can see, the EOL characters are printed.

Printing Non-Printing, Tabs, and EOL Characters:

Earlier, you had to use the -v option to print the non-printable characters, use the -T option to print the tab characters, and use the -E option to print the EOL characters. What if you need to print all of these? Well, you can combine all of these options together as follows:

But there is a better solution. The Linux cat command has a -A option that does just the same thing with less typing.

As you can see, the outputs are the same.

So, that’s basically how you use Linux cat command to display text files on Linux. Thanks for reading this article.

About the author

Shahriar Shovon

Freelancer & Linux System Administrator. Also loves Web API development with Node.js and JavaScript. I was born in Bangladesh. I am currently studying Electronics and Communication Engineering at Khulna University of Engineering & Technology (KUET), one of the demanding public engineering universities of Bangladesh.

Источник

Cat Command in Linux

The cat command is one of the most widely used commands in Linux. The name of the cat command comes from its functionality to concatenate files. It can read, concatenate, and write file contents to the standard output. If no file is specified or the input file name is specified as a single hyphen ( — ), it reads from the standard input.

cat is most commonly used to display the contents of one or multiple text files, combine files by appending one file’s contents to the end of another file, and create new files.

In this article, we will show you how to use the cat command through practical examples.

cat Command Syntax #

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

The cat utility expressions take the following form:

  • OPTIONS — cat options . Use cat —help to view all available options.
  • FILE_NAMES — Zero or more file names.
Читайте также:  Linux error no init found

Displaying File Contents #

The most basic and common usage of the cat command is to read the contents of files.

For example, the following command will display the contents of the /etc/issue file on the terminal:

Redirect Contents of File #

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

The following command will copy the contents of file1.txt to file2.txt using the ( > ) operator :

If the file2.txt file doesn’t exist, the command will create it. Otherwise, it will overwrite the file.

Use the ( >> ) operator to append the contents of file1.txt to file2.txt :

Same as before, if the file is not present, it will be created.

To display contents of a file with line numbers, invoke cat with the -n option:

1 DISTRIB_ID=Ubuntu 2 DISTRIB_RELEASE=18.04 3 DISTRIB_CODENAME=bionic 4 DISTRIB_DESCRIPTION="Ubuntu 18.04.1 LTS" 

Suppress Repeated Empty Lines #

Use the -s option to omit the repeated empty output lines:

Display TAB characters #

The -T option allows you to visually distinguish between tabs and spaces.

127.0.0.1^Ilocalhost 127.0.1.1^Iubuntu1804.localdomain 

The TAB characters will be displayed as ^I .

Display End of Lines #

To display the invisible line ending character use the -e argument:

DISTRIB_ID=Ubuntu$ DISTRIB_RELEASE=18.04$ DISTRIB_CODENAME=bionic$ DISTRIB_DESCRIPTION="Ubuntu 18.04.1 LTS"$ 

The Line endings will be displayed as $ .

Concatenating Files #

When passing two or more file names as arguments to the cat command, the contents of the files will be concatenated. cat reads the files in the sequence given in its arguments and displays the file’s contents in the same sequence.

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

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

The following command will concatenate the contents of file1.txt and file2.txt and write them to a new file combinedfile.txt using the ( > ) operator :

cat file1.txt file2.txt > combinedfile.txt

If the combinedfile.txt file doesn’t exist, the command will create it. Otherwise, it will overwrite the file.

To concatenate the contents of file1.txt and file2.txt and append the result to file3.txt to use the ( >> ) operator:

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

If the file is not present, it will be created.

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

Creating Files #

Creating small files with cat it often easier than opening a text editor such as nano , Vim, Sublime Text , or Visual Studio Code .

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

In the following example, we are creating a new file named file1.txt :

If a file named file1.txt is present, it will be overwritten. Use the ‘ >> ’ operator to append the output to an existing file.

Conclusion #

The cat command can display, combine and create new files.

If you have any questions or feedback, feel free to leave a comment.

Источник

Оцените статью
Adblock
detector