sainture
4/7/2016 - 10:00 AM

Linux commands

Linux commands

cat


It has three related functions with regard to text files: displaying them, combining copies of them and creating new ones.




Reading files




The most common use of cat is to read the contents of files.  All that is necessary to open a text file for viewing on the display monitor is to type the word cat followed by a space and the name of the file and then press the ENTER key. For example, the following will display the contents of a file named file1:





cat file1





The standard output (i.e., default destination of the output) for cat, as is generally the case for other command line (i.e., all-text mode) programs, is the monitor screen. However, it can be redirected from the screen, for example, to another file to be written to that file or to another command to use as the input for that command.


In the following example, the standard output of cat is redirected using the output redirection operator (which is represented by a rightward pointing angular bracket) to file2:





cat file1 > file2





That is, the output from cat is written to file2 instead of being displayed on the monitor screen.





The standard output could instead be redirected using a pipe (represented by a vertical bar) to a filter (i.e., a program that transforms data in some meaningful way) for further processing. For example, if the file is too large for all of the text to fit on the monitor screen simultaneously, as is frequently the case, the text will scroll down the screen at high speed and be very difficult to read. This problem is easily solved by piping the output to the filter less, i.e.,





cat file1 | less





This allows the user to advance the contents of the file one screenful at a time by pressing the space bar and to move backwards by pressing the b key. The user can exit from less by pressing the q key.





Concatenation


The second role of cat is concatenation (i.e., stringing together) of copies of the contents of files. (This is the source of cat's curious name.) Because the concatenation occurs only to the copies, there is no effect on the original files.


For example, the following command will concatenate copies of the contents of the three files file1, file2 and file3:


cat file1 file2 file3


The contents of each file will be displayed on the monitor screen.


This output could just as easily be redirected using the output redirection operator to another file, such as file4, using the following:





cat file1 file2 file3 > file4





File Creation


The third use for cat is file creation. For small files this is often easier than using vi, gedit or other text editors. It is accomplished by typing cat followed by the output redirection operator and the name of the file to be created, then pressing ENTER and finally simultaneously pressing the CONTROL and d keys. For example, a new file named file1 can be created by typing





cat > file1





If a file named file1 already exists, it will be overwritten . Thus the cautious user might prefer to instead use the append operator  in order to prevent unintended erasure. That is,





cat >> file1





Text can be entered at the time of file creation by typing it in after pressing the ENTER key. Any amount of text can be typed, including text on multiple lines.