Quick Tip: Using cat

5 March, 2024

Let me know if you’re tired of hearing this: while I don’t believe that a deep (or even shallow) knowledge of the command line is essential for using Linux, knowing a few commands can be helpful.

One command that’s flexible and deceptively powerful is cat. Let’s take a look at how to use it.

Viewing the Contents of a File

One common use of the cat command is to view the contents of a file without opening it in a text editor. Let’s say you’re poking around the command line and want to check out the contents of your .profile file. To do that, enter this command:

cat .profile

This display the entire contents of the file on your screen. You can then scroll through the output to find whatever you need to find. If the file is long, break the view into pages like this:

cat .profile | more

Combining Multiple Files

cat isn’t just useful for viewing files. You can also use it to combine multiple files into a single file.

Let’s say you have three text files: file1.txt, file2.txt, and file3.txt. To merge them into one file, type:

cat file1.txt file2.txt file3.txt > combined.txt

This create a new file called combined.txt that contains the contents of all three files.

Creating a File

You can also use cat to create a file. Let’s say you want to create a new text file called myTaskList.txt. Using cat, you can create the file and add some text to it, like this:

cat > myTaskList.txt
    - [ ] Do something useful

When you’re done, press Enter on your keyboard to save the file.

Scott Nesbitt