May 02 2007
LCoTD: File Viewing
If you're new here, you may want to subscribe to my RSS feed. Thanks for visiting!
Today I’ll be covering commands related to file viewing. These commands are cat, less, head, tail, nl, od, xxd, gv, and xdvi|
|
In Linux, you’ll encounter various types of files to view: plain text, Postscript, binary data, and more. Here I’ll explain how to view them.
cat [options] [files]
The simplest viewer is cat, which just copies its files to standard output, concatenating them (hence the name, cat). Note that large files will likely scroll off screen, so consider using less if you plan to view the output. That being said, cat is particularly useful for sending a set of files into a shell pipeline. cat can also manipulate its output in small ways, optionally displaying nonprinting characters, prepending line numbers (though nl is more powerful for this purpose), and eliminating whitespace.
Useful options
-E: Print newlines as $.
-v: Print other nonprinting characters in a human-readable format.
-n: Prepend line numbers to every line.
-b: Prepend line numbers to nonblank lines.
-s: Squeeze each sequence of blank lines into a single blank line.
cat is part of coreutils and lives in /bin.
less [options] [files]
Use less to view text one page at a time (or one window or screenful at a time). It’s great for text files, or as the final command in a shell pipeline with length output.
$ command1 | command2 | command3 | command4 | lessWhile running less, type h for a help message describing all its features. Here are some useful keystrokes for paging through files.
| Keystroke | Meaning |
| h, H | View a help page |
| Spacebar, f, ^V, ^F | Move forward one screenful. |
| Enter | Move forward one line. |
| b, ^B, ESC-b | Move backward one screenful |
| / | Enter search mode. Follow it with a regular expression and press Enter, and less will look for the first line matching it. |
| ? | Same as /, but it searches backward in the file. |
| n | Repeat your most recent search forward. |
| N | Repeat your most recent search backward. |
| v | Edit the current file with your default text editor (the value of the environment variable VISUAL, or if not defined, EDITOR, or if not defined, vi |
| < | Jump to beginning of file. |
| > | Jump to end of file. |
| :n | Jump to next file. |
| :p | Jump to previous file |
less has a mind-boggling number of features; I’m presenting only the most common. The manpage (man cat) is suggested.
Useful options
-m: Print a more verbose prompt, displaying the percentage of the file displayed so far.
-N: Prepend line numbers to the output.
-r: Display control characters literally; normally less converts them to a human-readable format.
-s: Squeeze multiple, adjacent blank lines into a single blank line.
-S: Truncate long lines to the width of the screen, instead of wrapping.
less lives in /usr/bin
head [options] [files]
The head command prints the first 10 lines of a file: great for previewing the the contents.
$ head myfile
$ head * | lessThis will preview all files in the current directory.
Useful options
-c N: Print the first N bytes of the file.
-q: Quiet mode: when processing more than one file, don’t print a banner above each file. Normally head prints a banner containing the filename.
tail [options] [files]
The tail command prints the last 10 lines of a file, and does other tricks as well. It is part of coreutils and lives in /usr/bin
$ tail myfileUseful options
+N: Print all lines except the first N.
-c N: Print the last N bytes of the file.
-f: Keep the file open, and whenever lines are appended to the file, print them. This is extremely useful. Add the –retry option if the files doesn’t exist yet but you want to wait for it to exist.
-q: Quiet mode: when processing more than one file, don’t print a banner above each file. Normally tail prints a banner containing the filename.
Pages: 1 2
If you found this article useful and use StumbleUpon, please give it a
thumbs up so more people can read it! Thank you!
Please take the time to check out this thread and leave a comment letting me know what you would like to see from this site. It's still relatively young and trying to find its way- you can make design pitstop the resource you always wanted!
