Examples of cat command in Linux

On Linux operating systems contains many built-in tools, the interaction with which is carried out by entering appropriate commands in the”Terminal” with different arguments. This enables the user to fully control the OS itself, different parameters and available files. One of the popular teams is a cat, and it is to work with content of different file formats. Next, we would like to show some examples of using this command using simple text documents.

Use the cat command in Linux

Today’s command is available for all distributions based on the Linux kernel, and everywhere looks the same way. This used Assembly does not matter. Today’s examples will be carried out on a computer running Ubuntu 18.04, and you will only see the arguments and the principle of their actions.

Preparatory actions

First I would like to devote time to the prior action, since not all users are familiar with the principle of the console. The fact is that when you open a file you either need to specify the exact path to it, or to launch the command from within the directory via the Terminal. Therefore, we suggest to start to review such a guide:

  • Start file Manager and navigate to the folder where you stored the necessary files.
  • Click on one of them, right-click and select “Properties”.
  • In the tab General check the information about the parent folder. Remember this path, because he will need it further.
  • Launch “Terminal” using menu or key combination Ctrl + Alt + T.
  • Write the command cd /home/user/folder, where user is the user name and folder is the folder where are stored objects. For moving along the path corresponds to a standard command cd.
  • This method navigates to the specified directory using the standard console. Further actions will also be done using this folder.

    Viewing content

    One of the main functions of the mentioned commands to view the contents of the various files. All information is displayed in separate lines in the”Terminal”, and the use of cat looks like this:

  • In the console, type cat testfilewhere testfile is the name of the desired file, then press Enter.
  • View the contents of the object.
  • You can open multiple files, this will need to specify their names, e.g. cat testfile testfile1.
  • The lines will be combined and displayed as one.
  • That’s cat without the use of available arguments. If you just write in “Terminal” cat, you will get some sort of console Notepad with the ability to record the desired number of rows and save them by pressing Ctrl + D.

    Line numbering

    Now let’s touch on the reporting team, using different arguments. We must start with the line numbering, and is responsible for this -b.

  • In the console write the cat-b testfilewhere testfile is the name of the desired object.
  • As you can see, were all present not numbered blank lines.
  • You can use this argument with the withdrawal of several files as shown above. In this case, the numbering will continue.
  • If you want to number all lines, including empty ones, have to use the argument -n, and then the team takes the form: cat-n testfile.
  • Removing duplicate blank lines

    It happens that in one document there are many blank lines that occurred in any way. Manually delete them using the editor is not always convenient, so here too you can contact the team at cat, using the argument -s. Then the string takes the form of cat -s testfile (available listing multiple files).

    Adding a $sign

    Sign $ in the command line Linux operating systems means that you have entered the next command will be executed as a standard user, without granting root rights. Sometimes you need to add this character to the end of all lines in the file should use the argument -E. The result is a cat-E testfile (the letter E must be written in upper case).

    Merge multiple files into one new

    Cat allows you to quickly and easily combine several objects into one new one, which will be saved in the same folder, where are all the action. You need to perform the following:

  • In the console write cat testfile testfile1 > testfile2 (the Number of names before you > can be unlimited). After that press Enter.
  • Open the directory using file Manager and start a new file.
  • It is seen that it contains all the rows from all of these documents.
  • Much less common a few arguments, but they must be mentioned:

    • -v — show version reporting utility;
    • -h — displays the help message with basic information;
    • -T — adds a display for the tab in the form of symbols ^I.

    You were familiar with the procedure document editing, which can be useful for combining plain text or configuration files. However, if you are interested in creating new objects, we suggest you contact our other article on the following link.

    Read more: Create and remove files in Linux

    In addition, operating systems on Linux there are still a large number of popular and often used commands, find out more about them in a separate article next.

    Now you know about the standard command cat everything that can be useful when running in “Terminal”. In collaboration with it is not difficult, the main thing — to adhere to the syntax and register of attributes.

    Source: lumpics.ru

    (Visited 34 times, 1 visits today)