100 Useful Command-Line Utilities

by Oliver; 2014

14. less, zless, more

Based off of An Introduction to the Command-Line (on Unix-like systems) - less and more: less is, as the man pages say, "a filter for paging through text one screenful at a time...which allows backward movement in the file as well as forward movement." This makes it one of the odd unix commands whose name seems to bear no relation to its function. If you have a big file, vanilla cat is not suitable because printing thousands of lines of text to stdout will flood your screen. Instead, use less, the go-to command for viewing files in the terminal:
$ less myfile.txt      # view the file page by page
Another nice thing about less is that it has many Vim-like features, which you can read about on its man page (and this is not a coincidence). For example, if you want to search for the word apple in your file, you just type slash ( / ) followed by apple.

If you have a file with many columns, it's hard to view in the terminal. A neat less flag to solve this problem is -S:
$ less -S myfile.txt    # allow horizontal scrolling
This enables horizontal scrolling instead of having rows messily wrap onto the next line. As we'll discuss below, this flag works particularly well in combination with the column command, which forces the columns of your file to line up nicely:
cat myfile.txt | column -t | less -S
Use zless to less a zipped file:
$ zless myfile.txt.gz    # view a zipped file
I included less and more together because I think about them as a pair, but I told a little white lie in calling more indispensible: you really only need less, which is an improved version of more. Less is more :-)

<PREV   NEXT>