Bash Printing the Contents of a File


Example

cat file.txt

will print the contents of a file.

If the file contains non-ASCII characters, you can display those characters symbolically with cat -v. This can be quite useful for situations where control characters would otherwise be invisible.

cat -v unicode.txt

Very often, for interactive use, you are better off using an interactive pager like less or more, though. (less is far more powerful than more and it is advised to use less more often than more.)

less file.txt

To pass the contents of a file as input to a command. An approach usually seen as better (UUOC) is to use redirection.

tr A-Z a-z <file.txt   # as an alternative to cat file.txt | tr A-Z a-z

In case the content needs to be listed backwards from its end the command tac can be used:

tac file.txt

If you want to print the contents with line numbers, then use -n with cat:

cat -n file.txt

To display the contents of a file in a completely unambiguous byte-by-byte form, a hex dump is the standard solution. This is good for very brief snippets of a file, such as when you don't know the precise encoding. The standard hex dump utility is od -cH, though the representation is slightly cumbersome; common replacements include xxd and hexdump.

$ printf 'Hëllö wörld' | xxd
0000000: 48c3 ab6c 6cc3 b620 77c3 b672 6c64       H..ll.. w..rld