# Guide to Unix/Commands/File Viewing

| Wikibooks Guide to Unix Computing | Edit template |
   General: Introduction | Explanations | Why Unix-like Platforms: Linux | BSD Quick Reference: Commands | Environment Variables | Files | License
   Commands: Getting Help | File System Utilities | Finding Files | Devices | File Viewing | File Editing | Text Processing | File Compression | File Analysing | Multiuser Commands | Self Information | System Information | Process Management | Kernel Commands | Miscellaneous | SW Development |

### cat

cat displays the contents of a file to screen. It can also display multiple files concatenated together, and using the shell, its standard output can be redirected from the screen to the file.
Some useful options are:
Examples
Concatenating multiple files (in this example, the same file twice):
Concatenate the files, but use the shell to redirect the result to a new file.
We can concatenate binary files, too. Some programs limit downloads to 2 GB; this is bad for larger files like some DVD images. Suppose a web site helpfully divides the file "sodalinux.dvd" into three parts, 2 GB or less, for downloading and later concatentation. We combine the files, and use the > shell redirection output to put the DVD image in a file:
If we want to type less, then most shells also allow this:
The -v option is useful for viewing control characters embedded mostly in text. In this example, the file "/usr/share/man/cat1/pax.0" is mostly text but contains control characters which the pagers "less" and "more" use to make text bold. Using cat -v we can see the control characters. Here are the first four lines:
Using "cat" with no arguments makes it copy standard input to standard output. Combined with shell redirection, this makes it easy to write a very short text file. All one needs to know is to press Control-D (^D) to indicate end of input, finish the file, and return to the shell. Here is how to write "example.txt":
If you put "cat" with no arguments in a pipe, it only copies standard input to standard output. This might seem useless. For example, the following two pipes have the same function:
However, "cat" can be used as insulation to make programs think that they are not running on terminals. In the next example, GNU bc does not print its copyright message on startup. We enter one calculation ("3 + 9") and then quit (^D):
Links:
* cat, opengroup.org
* cat man page, man.cat-v.org
* 3.1 cat in GNU Coreutils manual, gnu.org

### tac

tac (cat spelled backwards) works like cat, but reverse the order of the lines (last line is written out first). It does not seem to be part of traditional Unix. When multiple files are given, they are printed out in the order they appear.
Links:
* 3.2 tac: Concatenate and write files in reverse in GNU Coreutils manual, gnu.org

### more

more paginates output. The problem with "cat" is that if a file is too long, then it falls beyond the top of the screen. The job of "more" is to stop and wait when it fills the screen. Most users find it easier to use "less", but on some systems "more" has all of the features of "less".
Keys:
* return read next line
* space bar read next screen
* q quit
Examples: The pager will act like "cat" if the file is short enough.
Links:
* more, opengroup.org

### less

less paginates output. The program is called "less" because of the joke that "less is more", "less" actually has several features which "more" lacks.
Keys:
* h read help. You might forget the other commands, but remember this one!
* j go down one line. The down-arrow key might also work.
* k go up one line. The up-arrow key might also work.
* d go down one-half screen.
* u go up one-half screen.
* f go forward one screen.
* b go back one screen.
* p return to the top of the file.
* q quit the pager.
Number arguments:
* 0 through 9: type a number. The number will be used as the argument N to the next command.
* j go down N lines.
* k go up N lines.
* p jump to the N% position, where 0% is the first line and 100% the last line of the file.
Examples:
Read some file:
Pipe "dmesg" into "less" so that the dmesg does not scroll off the screen:
Links:
* less home page, greenwoodsoftware.com
* less(1) OS X Manual Page at developer.apple.com
* less man page, freebsd.org
* GNU less pages at gnu.org; no user manual apparent
* Wikipedia:less (Unix)

### od

Lets you view binary files by outputting octal representation, hexadecimal representation and more. See also Od.
Examples:
View a file in octal format:
View a file in hex format:
View a file in character format:
Links:
* od, opengroup.org
* od man page, man.cat-v.org
* 3.4 od in GNU Coreutils manual, gnu.org

### head

Outputs 10 lines or other count of lines from the head (top) of a file, files or standard input. See also tail, outputting last lines.
Examples:
* head wordlist.dat
** Outputs first 10 lines of the file.
* head -n 2 wordlist.dat
** Outputs first 2 lines of the file.
* head -2 wordlist.dat
** Outputs first 2 lines of the file. A legacy option no longer supported by POSIX, still supported by GNU head.
* seq 10 | head -n -2
** In GNU head, outputs all but the last 2 lines of the input stream, in this case of 1 ... 10. A GNU extension not supported by POSIX.
* seq 10 | sed 2q
** A sed alternative that avoids head, outputting the first two lines.
Links:
* head, opengroup.org
* 5.1 head in GNU Coreutils manual, gnu.org
* head, freebsd.org
* head (Unix), wikipedia.org

### hexdump

Hexdump is a command-line tool used to show the raw bytes of a file in various ways including hexadecimal, available on Linux, FreeBDS, OS X, and other platforms. Hexdump is not part of traditional Unix systems or GNU commands, and does not seem to be covered by POSIX. A similar tool is #od (POSIX covered) and #xxd.
Examples of hexdump use:
* hexdump myfile
** Outputs hexadecimal of the file content, with no ASCII. Seems to default to outputting 16-bit chunks separated by space, 16 bytes per line.
* hexdump -C myfile
** Outputs bytes in hexadecimal, each byte separated by a space, together with ASCII at a right column.
* hexdump -v -e '"" 1/1 "%02X" " "' myfile
** Outputs the bytes in hexadecimal using capitalized letters, each byte separated by a space. The first "" specifies and empty prefix, while the last " " specifies a single-space suffix.
Links:
* hexdump, man7.org - from util-linux
* hexdump at freebsd.org
* hexdump, opensource.apple.com
* Hex dump, wikipedia.org

### xxd

Outputs hexadecimal content of a file or standard input, or creates a file based on a hex dump. Not covered by POSIX and not part of GNU tools; seems to be part of Vim project, distributed with it.
Examples:
* xxd myfile
** Outputs hexadecimal output together with ASCII at a right column; the default seems to be to output 16-bit space-separated chunks. Does not seem to respect the endianness of the platform in these 16-bit chunks.
* xxd -g 1 myfile
** As above but separating each byte by a space.
* xxd -b myfile.
** Outputs binary output.
Links:
* xxd, linux.die.net
* xxd at freebsd.org
* Hex dump, wikipedia.org
* xxd.c in vim, github.com

### tail

Outputs last 10 lines or other specified number of lines or bytes of the file, files or standard input. Its counterpart is #head. Whether POSIX allows multiple input files is unclear.
Examples:
* tail wordlist.dat
** Outputs last 10 lines.
* tail -n 2 wordlist.dat
** Outputs last 2 lines.
* tail -2 wordlist.dat
** Outputs last 2 lines. A legacy option no longer supported by POSIX, still supported by GNU tail.
* printf hello | tail -c 2
** Outputs two last bytes/characters of the input, and thus, "lo".
* tail file1.txt file2.txt
** In GNU tail, processes multiple files, introducing the output for each file with a header.
The -f option displays the tail, then waits for and displays any new options to the file. This is normally used to watch log files. (The next example has only three lines from tail, but the 80-column terminal was too narrow, so the lines were broken into five lines.)
Links:
* tail, opengroup.org
* tail man page, man.cat-v.org
* 5.2 tail in GNU Coreutils manual, gnu.org
* tail, freebsd.org
* tail (Unix), wikipedia.org