How to View the Contents of a Text File from the Linux Command Line

What command lets you view the contents of a file has to be one of the most commonly asked questions by newer users of Linux. As with most things related to a traditional Unix environment, there’s more than one way to do things and most of these tips would also work for those using OS X or FreeBSD.

Contents of a Text File from the Linux Command Line
View the Contents of a Text File from the Linux Command Line

That means once you learn the basics of viewing files from the command line, you can find your way around nearly any Unix prompt your find yourself at.

Method 1: Viewing a Regular Text File

The easiest way to view any text file is to type cat followed by the name of the file. If the file is short enough, then you’ll see the entire text just displayed flat on the screen. Otherwise, it will start to scroll up. Fortunately, on modern devices, you can maximize the terminal window to add more space. Even with this, though, you might have things roll straight off the screen.

In that case, type more followed by the name of the file. This will page it, so that you won’t see any more of the file until you push the space bar, thus giving you much more time to read things before they vanish. Once you’re finished, you can push the q key to quit. Typing more -d followed by a file name will get you a little handy instruction line and pushing h will get you a proper help sheet.

You can use the command less followed by the name of a file if you want to be able to have additional control over it. For instance, once you use the less command you can scroll back and forth through a text file by using your cursor keys, the Page Up/Page Down keys as well as the k/j vi key bindings.

If you’ve ever used the man command to look up the manual page of a command line application, then you already know how to use less without even realizing it because most distributions use less to scroll through man pages. Anyone who knows gestures that they use in the man pager should give them a try in less, but keep in mind that you can always push the h key to get a proper help screen too.

Experienced users of vi/vim might find this a bit weird since this part of the h/j/k/l movement quarter, but less doesn’t require you to move side to side anyway so you’ll get used to it fat enough. You can always use Ctrl+N or Ctrl+E to move forward one line while using Ctrl+Y or Ctrl+P to move back. If you notice, then the help screen reads that CR moves forward a single line. This refers to Carriage Return and refers to the Return key on your keyboard.

Type a / before any term to search throughout the document, then type n to move toward the next instance or Shift+N to move toward the previous.

While it isn’t as flexible as grep, it might be useful if you’re already taking a look at a file in less and need to find something.

Method 2: Viewing Special Files

Type less -f followed by a file name to force it to open. You could, say, use sudo less -f /dev/sdb1 to actually peek at the beginning of a partition’s boot record, though you should make sure to take extra caution when doing anything with the sudo account and boot records.

We used it to examine the boot record of a microSDHC card formatted in an Android tablet.

You can maneuver through the file like normal and push the q key when you want to exit. Should you simply have a standard file in some sort of weird encoding that you’d like to explore, type od -c followed by the name of the file. You can either scroll up in the terminal window or use od -c fileName | grep less to pipe it out to less if it happens to be too long. This will give you a chance to view it if you can’t seem to examine it with any other program, including graphical ones. If you invoke od without any switches, then it will print out the file as a stream of octal numerals as something of a last resort.

Method 3: Viewing the Contents of Compressed Files

You’ll sometimes have a zipped up text file, which you can read without having to decompress first. You might be used to the traditional ZIP format, which comes from the MS-DOS ecosystem and therefore archives as well as compresses. If you have text files inside of a ZIP archive, then you would need to inflate it before you read them. However, Unix-based compression algorithms allow you to compress a file in place.

If you had a compressed text file called smallerFile.gz, then you could use zcat smallerFile.gz to view the contents of the file from the command line. You may also wish to type zmore or zless instead of zcat, which work identically to the more and fewer commands but support text files that were compressed via the gzip program.

More Linux distributions are starting to use the xz file format, so if you had a text file that got compressed with this format simply add xz to the front of any file viewing command. Instead of cat, less and more you could use xzcat, xzless and xzmore. Likewise, there are bzcat, bzless and bzmore command line apps for those who find that they have text files compressed using the bzip2 standard.

Note that you won’t be able to read a text file in this manner if it was first put into a tar or cpio archive then compressed. So while you can read smallerFile.gz just fine, reading smallerFile.tar.gz or smallerFile.tgz wouldn’t work the same way.


Kevin Arrows

Kevin Arrows is a highly experienced and knowledgeable technology specialist with over a decade of industry experience. He holds a Microsoft Certified Technology Specialist (MCTS) certification and has a deep passion for staying up-to-date on the latest tech developments. Kevin has written extensively on a wide range of tech-related topics, showcasing his expertise and knowledge in areas such as software development, cybersecurity, and cloud computing. His contributions to the tech field have been widely recognized and respected by his peers, and he is highly regarded for his ability to explain complex technical concepts in a clear and concise manner.