
If you don't have time, you can see multiple files at once. (2) Display multiple files in a single terminal Just type cat with the file name and you can see all content of the file on the terminal screen. This is the most basic example of using the cat command. (1) Basic Usage: display content of a file In this article, we will discuss Linux cat Command with 12 Practical Examples. Click here If you are interested in learning the Top 25 Linux commands. This command is mainly used to display the content of one or multiple files. Redirect output produced by the cat command to a new file IF.Linux cat command is an awesome command to see the content of a file.


Piping the cat command to the more command to deal with large files. Shows the number of lines while omitting the file contents of the File. Here's a quick summary of what I explained in this tutorial: Commandįile contents of the FileB will be overridden by the FileA.įile contents of the FileA will be appended at the end of the FileB. Once you have the perfect output, you can redirect it to a file to save the output: cat Haruki.txt | grep -v '^$' > File That's what you've learned so far Here, the -v flag will filter out the results as per the specified pattern and '^$' is a regular expression that matches the empty lines.Īnd here are the results when I used it over the Haruki.txt: cat Haruki.txt | grep -v '^$' So how would you remove all the empty lines? By piping it to the grep command: cat File | grep -v '^$' You're still left with one blank space:Īs you can see, it works but the results are close to the expectations. To do so, all you have to do is use the -s flag.īut there's one downside of using the -s flag. Left multiple blank lines in your text document? The cat command will fix it for you! You may encounter such scenarios where you want to see the number of lines, and that can be achieved using the -n option: cat -n Fileįor example, here, I used the -n option with the Haruki.txt: 5. Use cat > filename and start adding the text you want and finally save the changes with Ctrl+D. You can use the > to add new lines to an existing file. To do so, you'd have to follow the given command syntax: cat FileA > FileB You use the cat command with the redirection of data flow. But what if there are hundreds or thousands of lines? Think of a scenario where you want to redirect the file content of FileA to the FileB Copy the file contents to a different file That's it! Now, you can use the ls command to show the contents of the current working directory: 2. If you wish to create an empty file, then just press the Ctrl + d without making any changes. Once you do that, there'll be a blinking cursor asking you to write something and finally, you can use Ctrl + d to save the changes. To do so, you'd have to use the cat command by appending the filename to the > as shown: cat > Filenameįor example, here, I created a file named NewFile.txt: cat > NewFile.txt

The cat command has one advantage over the touch command in this case, as you can add text to the file while creating. Most Linux users use the touch command to create new files but the same can be done using the cat command too! So, what will be the output when used without any options? Well, let's have a look: cat Haruki.txtĪs you can see, it printed the whole text file!īut you can do a lot more than just this. South of the Border, West of the Sun (1992)Ĭolorless Tsukuru Tazaki and His Years of Pilgrimage (2013) Hard-Boiled Wonderland and the End of the World (1985) To make things easy, I will be using a text file named Haruki.txt throughout this guide which contains the following text lines: Hear the Wind Sing (1979) Filename is where you'll enter the filename of the file that you want to work with.

are used to modify the default behavior of the cat command such as using the -n option to get numbers for each line.To use the cat command, you'd have to follow the given command syntax: cat Filename(s) And for that reason, it is a go-to choice for users to read files in the terminal without any additional options. But withsingle argument, it prints the file contents. The cat command is used to print the file contents of text files.Īt least, that's what most Linux users use it for and there is nothing wrong with it.Ĭat actually stands for 'concatenate' and was created to merge text files.
