Are you ready to unleash the power of the Linux command line? If you’re a Linux user, you’re probably already familiar with the cat command in linux. But did you know that it’s not just a simple tool for displaying file contents? It’s a command that can concatenate, create, and even append content to existing files! Yep, you heard it right.
cat can do all of that, and more! So, whether you’re a seasoned Linux user or a newbie, understanding the full potential of the
cat command can take your file management game to the next level. In this article, I’ll show you how to use
cat like a pro with practical examples and tips. Let’s get started!
View File Contents
One practical way to use the
cat command in Linux is to display the contents of a file on the terminal. For example, if you want to view the contents of a text file named
example.txt you can simply type
cat example.txt in the terminal and hit enter. This will display the entire content of the file on the terminal screen.
Did you know you can even combine, aka concatenate, more than 2 files together?
To concatenate two or more files into one, use the
cat command followed by the names of the files you want to concatenate, separated by a space. For example, to concatenate two text files named
file2.txt into a new file named
combined.txt, type the following command and hit enter:
cat file1.txt file2.txt > combined.txt
Create A New File
Yep, that’s right! Just like the touch command, you can easily create empty files. Just like the touch command, just type in the command and the file name you want. One thing is a little different, though. You must use
cat > newfile.txt
Append To Files
>> to tell
cat you want it to be appended. Otherwise, it will be overwritten!
cat newstuff.txt >> existingfile.txt
Alternatively, you can write in-line what you want to append using quotes. Like so…
cat "new cool stuff to add!" >> existingfile.txt
Show Line Numbers
Ever wanted to see the contents of a file BUT, have the lines numbered?
cat can do that too!
Suppose we have a file named
example.txt with the following content:
This is the first line.
This is the second line.
This is the third line.
cat command with the
-n option as follows:
cat -n example.txt
The output will be:
1 This is the first line.
2 This is the second line.
3 This is the third line.
cp command… not sure why you would use cat to copy files. BUT, this is Linux! There is more than one way to achieve the same results. Similar to appending content to an existing file, we could specify the source file and add a new destination file. Like so…
cat source.txt > destination.txt
Pretty straight forward, right?!
Concluding The CAT Command In Linux
cat command is like a Swiss Army knife for Linux users. It’s a simple yet powerful tool that can perform a range of tasks, from displaying the contents of a file to copying files and redirecting output to other commands using pipes. Think of it as the superhero of the command line world, always ready to save the day and simplify your work. With its user-friendly syntax and numerous options, the
cat command is an essential tool for any Linux user, whether you’re a seasoned pro or just starting out. So, next time you need to view a file’s contents, concatenate files, or copy text from one file to another, remember to reach for the trusty
cat command and let it do its magic.