Basic Cat command examples in Linux/Unix
Cat command is a simple command used by every Unix/Linux user. It is one of frequently used command in Linux/Unix Systems. Cat command works on bash, and PowerShell and shell options.
Cat command behaves differently based on the different types of usage.
By using cat command, we can do the following functionalities
1. Read files using the cat command
2. Create files Using cat command
3. Copy files content using cat command.
Cat command works UNIX, Linux as well as Ubuntu terminals
Cat command Syntax
Cat (options) (one or more file name)
Let us see the various cat command examples as below.
Read or view the file using cat command with examples in Linux/UnixThis is a simple command to read the contents of the file and output the file content to Standard output like console
$cat cloud.txt This is test file contains test content.If it is a small file, we can view it using cat command, but file size is large we can use more or less with pipe options in cat command.
How to create a file using Cat command in UnixCat command is a basic command to create files. To create a file demo.txt
$ cat >demo.txt This is test file contains test content. CTRL +DOnce the above command is entered, the new demo.txt file is created and console waits from user to type input and whatever data typed by the user is placed in file.> symbol specifies that data to place in a file typed by a user. You need to type CTRL with typing D to save data to file and exit from the console.
How to merge files using cat command in Linux/UnixCat command also used to merge different files and create a new file
$ cat file1 file2 > output.txt
First file1 data is copied to output.txt and next file2 data is copied to end of file1’s data.
How to view the line numbers of each line using cat commandIn Java programs, if any error happens, Error/Exception thrown with a print stack trace which will give filename as well as line number where the exception has occurred. In that case, to view the java code with line numbers, we can use cat command with option –n
$ cat –n cloud.txt 1 This is test file contains 2 test content.
Append/concatenate data to an existing file using cat command
$cat >>cloud.txtNew data is appending to an existing file
We have to press CTRL+D option to save/append the new data to existing data in a file
How to view file with showing spaces, tabs using cat command with examples.In any text Editors, we have the option to be enabled to show spaces and tabs codes
Cat has also had the option to view spaces and tabs and end of lines using option –T
$ cat –T filename
Cat command optionsBasic cat command has different options, can be used with all flavors of Linux like AIX
-T output spaces, tabs, and End of lines characters in the file
-s suppress the multiple blank lines
-n output number on each line of the file
That’s it from the usage of cat command in the daily life of software development.
Please click +1 if you like this post.