+5 votes
307 views
How to use cat Linux command

in Linux / Unix by (552k points)
reopened | 307 views

1 Answer

+3 votes
Best answer

1. How to display the contents of a file with cat command in Linux
2. How to view the contents of various files using cat LInux
3. How to create a new file with the cat command
4. How to display the number of lines in a file with cat command
5. How to display the $ sign at the end of each line with cat command
6. How to display the lines separated by tabulation with cat command
7. How to deploy several files simultaneously with cat command
8. How to make use of the standard output with a redirection operator with cat command
9. How to append a standard output with a redirection operator with cat command
10. How to redirect multiple files in one with cat command
11. How to sort the contents of several files in a single file with cat command
12. How to add a standard output to a new file with cat command

Since UNIX-based operating systems are one of the most functional alternatives for both home users, small, medium or large corporations, it is ideal to know in detail the way in which they are used globally, as a rule, all UNIX systems, In this case we specifically talk about Linux , it is the use of commands, and although it is true that today most distributions provide us with a graphical option, the essence of Linux is the development and execution of the commands..

There are hundreds of commands each with a special function and one of the most used for its versatility and practicality is the cat command which will analyze TechnoWikis in detail.

What is the cat command in Linux
The cat (concatenate) command, is a command that has been developed to basically execute three functions associated with text files, these are:
  • Be able to view them
  • Make a combination with copies of them
  • Create new files

Its use syntax is:

 cat [options] [file] [-] [file] 
The various options we have when using the cat command are the following:
Display all the information in a file or folder, it is the equivalent of -vET
 -A, --show-all 

It is the number of output lines not empty
 -b, --number-nonblank 

It is the equivalent of -vE
 -and 

This option displays the $ sign at the end of each line.
 -E, --show-ends 

Display the number of each line at the exit
 -n, --number (number) 

Its function is to suppress repeated empty output lines
 -s, --squeeze-blank 

It is the equivalent of -vT
 -t 

Show TAB characters as ^ I
 -T, --show-tabs 

It makes use of the notation ^ and M, except LFD and TAB
 -v, --show-nonprinting 

Display the help of the cat command
 --help 

Display the version of cat used in Linux
 --version 

Now we will learn how to use this command in Linux..


1. How to display the contents of a file with cat command in Linux


It is one of the most practical uses in which the cat command comes into action in Linux, because thanks to it it will be possible to see the contents of a file created by us or a system file.
Step 1

For example we can see the contents of a text file called TechnoWikis:

image

Step 2

We can also see the contents of a system file like / etc / passwd:
 cat / etc / passwd 
image

2. How to view the contents of various files using cat LInux


We may want to visualize in the terminal the content of not only one file but several simultaneously, this is possible with cat using the following syntax:
 cat file1 file2 ... 
image
How to make use of the More (less) or Less (Less) options
These options are ideal when the file to be displayed is extensive, so it is necessary to use the Av Pag or Re Pag keys to access all its contents.

In this case we can use any of the following options as appropriate:

 cat file | more cat file | them 
image

3. How to create a new file with the cat command

Step 1

The cat command includes within its characteristics the possibility of creating a new file, for this we must execute the following syntax:
 cat> archive 
In this case we have created a new file called TechnoWikis2 which we see that is automatically created in the directory where we are located, in this example the desktop: image
Step 2

We can see that by using the cat command we will be located in the text, there we can enter the desired text and then press the Ctrl + D keys to exit and save the text:

+ D Ctrl + D

image

4. How to display the number of lines in a file with cat command


This option is ideal to know in detail the number of lines integrated in a file, for this we must use the -n parameter like this:
 cat -n TechnoWikis1 
image

We can display the line number with its respective associated text.


5. How to display the $ sign at the end of each line with cat command


This option is useful if we want to compress several lines in a single line, this is responsible for displaying the sign $ at the end of the line and also in the space that shows $ in case there is any space between the paragraphs.
For use we must use the -e parameter as follows:
 cat -e TechnoWikis 
image

6. How to display the lines separated by tabulation with cat command


For this option the spaces detected through tabulation will be filled with "^ I", for this we must use the –T parameter like this:
 cat -T TechnoWikis 
image

7. How to deploy several files simultaneously with cat command


We can display the contents of multiple files with the cat command, for this we must use the semicolon to separate each file, in this case we will use the following line:
 TechnoWikis cat; cat TechnoWikis2; cat TechnoWikis1; 
image

8. How to make use of the standard output with a redirection operator with cat command


Thanks to this option it will be possible to redirect the standard output of a file to a new file to be created, otherwise, to an existing file with the symbol> (greater than) but we must keep in mind that the content of the initial file will overwrite the existing content in the destination file.

For this example we will redirect the contents of the TechnoWikis file to a new file called TechnoWikis3:

 cat TechnoWikis> TechnoWikis3 
image

This will create the TechnoWikis3 file with the contents of the TechnoWikis file..


9. How to append a standard output with a redirection operator with cat command


This allows to append in the existing file through the symbol >> (double greater than), this will cause the content of the file to be added to the end of the destination file.
 cat TechnoWikis >> TechnoWikis2 
image

10. How to redirect multiple files in one with cat command

Step 1

This allows us to redirect the content of one or more files in a new or existing one, for this we execute:
 cat TechnoWikis TechnoWikis1 TechnoWikis2> TechnoWikis4 
In this case we will redirect all the content of these three files into a new one called TechnoWikis4: image
Step 2

When accessing the TechnoWikis4 file we can see the content of the 3 files, there it depends on organizing the content in the best possible way:

image


11. How to sort the contents of several files in a single file with cat command

Step 1

If we want the content redirected to a new or existing file to be sorted, we must use the sort parameter like this:
 cat TechnoWikis TechnoWikis1 TechnoWikis2 | sort> TechnoWikis4 
image
Step 3

This will create the organized file in alphabetical order:

image


12. How to add a standard output to a new file with cat command


Using the echo command it will be possible to add new text to a new file, for this we will use the following example:
 echo "TechnoWikis Internet" | cat - TechnoWikis2 
image

The new line will be entered at the top of the selected file.

We can see how thanks to the cat command the options to manage our files are wide and fully functional allowing us to perform various actions on these as required but always maintaining the integrity of the text hosted there in order to not cause negative effects on the control of these, This way we can make use of each of the cat parameters effectively.


by (3.5m points)
edited

Related questions

+3 votes
1 answer
asked Oct 20, 2020 in Linux / Unix by backtothefuture (552k points) | 253 views
+4 votes
1 answer
asked Sep 22, 2020 in Linux / Unix by backtothefuture (552k points) | 344 views
+3 votes
1 answer
+4 votes
1 answer
asked Jul 29, 2020 in Linux / Unix by backtothefuture (552k points) | 470 views
+4 votes
1 answer
asked Jun 12, 2020 in Linux / Unix by backtothefuture (552k points) | 334 views
Sponsored articles cost $40 per post. You can contact us via Feedback

Most popular questions within the last 30 days

10,659 questions
10,791 answers
510 comments
3 users