Using The Terminal Command ‘cat’ To View The Contents Of Files 7

Terminal

Share
Using The Terminal Command ‘cat’ To View The Contents Of Files

Hey,

When you beging to use Terminal, on your Mac, or on another unix type system you will slowly but surely begin to use more and more files. Most of the time you would like to view the contents of the files to see what they are about and if you need to change them. Although you could use a text editor such as nano or vi, to view the files, today’s command is going to introduce Terminal command called ‘cat’ that allows you to view and open files quickly and simply without having to do anything complicated.

Using cat

Using cat is very simple. Open Terminal and type the following into the command prompt:

cat ./path/to/file.txt

This will then output, all at once, the contents of the file in your Terminal window as shown in the image below.

Cat allows you to view the contents of text files.

Like any Terminal command there are a couple of options which allow you to alter the output of the cat command. For example if you add the option -b as shown in the command below, this will number none blank lanes. If you add the option -v it will output a line number for every line.

cat -v /path/to/file.txt

If you want to view the manual for the cat command, enter the following to view the man page.

man cat

You can view more than one file at a time by simply adding the next file into the command.

cat file1.txt file2.txt

Less is More

If you have a large text file, the output of the cat command can result in a lot of scrolling and a wall of text being presented to you. To get round this we can use the less command to ouput a small amount each time. You use the command as follows:

cat textfile.txt | less

If the file is long enough it will fill you Terminal screen, then you can press either the space or enter key to more forward one line at a time, or the d key to more forward equal to the number of lines present on the screen. The b key does the same as the d key but scrolls backward.

If you want more information on the less command, please refer to the manual page:

man less

The combined commands are simple, however will allow you to stop using a text editor in Terminal to view files. If you have any comments, please leave a comment.


If you want to keep up with the latests post from Mac Tricks And Tips I recommend you subscribe to the RSS Feed.

Where To Next?

  • Subscribe To Mac Tricks And Tips