How can I display the contents of a text file on the command line?

I would like to display the contents of a text file on the command line. The file only contains 5-6 characters. Is there an easy way to do this?

Asked By: Sam Weinberg


You can use following command to display content of a text file.

cat filename 
Answered By: forvaidya

Using cat

Since your file is short, you can use

cat filename

Using less

If you have to view the contents of a longer file, you can use a pager such as

less filename

You can make less behave like cat when invoked on small files and behave
normally otherwise by passing it the -F and -X flags.

less -FX filename

I have an alias for less -FX. You can make one yourself like so:

alias aliasname='less -FX'

If you add the alias to your shell
, you can use it

Using od

If your file contains strange or unprintable characters, you can use
od to examine the characters. For example,

$ cat file
(ÐZ4 ?o=÷jï
$ od -c test
0000000 202 233   ( 320   K   j 357 024   J 017   h   Z   4 240   ?   o
0000020   = 367  n
Answered By: user26112

I always use $ less "your file here" , as it is very simple, provides a built in interactive grep command, and gives you an easy to use interface that you can scroll with the arrow keys.

(It is also included on nearly every *nix system)

Answered By: SG60

Tools for handling text files on unix are basic, everyday-commands:

In unix and linux to print out whole content in file

cat filename.txt


more filename.txt


less filename.txt

For last few lines

tail filename.txt

For first few lines

head filename.txt
Answered By: Abhishek

If its a large file, and you want to search some specific part, you can use

 cat filename | grep text_to_search -ni 

Also you can use more interactive Vim editor (or vi editor if you do not have Vim):

 vim filename
 vi filename

Vim/vi is a great editor, can also be used as a reader in “Normal Mode” or using -R option, it has many features that will help you in browsing through the file.

Answered By: 0xF1

Use cat command to display the content of filename.

cat filename  

Use vim command to edit file.

vim filename
Answered By: Edward Shen

Even though everybody uses cat filename to print a files text to the standard output first purpose is concatenating.
From cat’s man page:

cat – concatenate files and print on the standard output

Now cat is fine for printing files but there are alternatives:

  echo "$(<filename)"
  printf "%s" "$(<filename)"

The ( ) return the value of an expression, in this case the content of filename which then is expanded by $ for echo or printf.


< filename

This does exactly what you want and is easy to remember.

Here is an example that lets you select a file in a menu and then prints it.


select fname in *;
# Don't forget the "" around the second part, else newlines won't be printed
  printf "%s" "$(<$fname)"

For further reading:
BashPitfalls – cat file | sed s/foo/bar/ > file
Bash Reference – Redirecting

Answered By: crunsher

One option is to use more

e.g. more file.txt

However it does not have all the feature added by less.
One simple example is that you can’t scroll back up in the output. Generally it has been superceeded by less – which was named in jest because

less is more

Answered By: Michael Durrant


~$ perl -pe ''  Sonnet_18.txt


~$ raku -pe ''  Sonnet_18.txt

Sample Output:

Shall I compare thee to a summer’s day?
Thou art more lovely and more temperate.
Rough winds do shake the darling buds of May,
And summer’s lease hath all too short a date.
Sometime too hot the eye of heaven shines,
And often is his gold complexion dimmed;
And every fair from fair sometime declines,
By chance or nature’s changing course untrimmed.
But thy eternal summer shall not fade
Nor lose possession of that fair thou ow’st,
Nor shall Death brag thou wand’rest in his shade,
When in eternal lines to time thou grow’st.
 So long as men can breathe or eyes can see,
 So long lives this, and this gives life to thee.

Clearly, cat is going to be the most popular answer to this question, but the code examples above will also provide the desired output (file courtesy of Shakespeare, via Project Gutenberg). However learning basic one-liners using Perl and/or Raku has its merits, simply because you can get an awful lot of work done with them.

Grep through a file, return matching lines:

~$ #Perl:
~$ perl -ne 'print if /eternal/'  Sonnet_18.txt
But thy eternal summer shall not fade
When in eternal lines to time thou grow’st.

~$ #Raku:
~$ raku -ne '.put  if /eternal/'  Sonnet_18.txt
But thy eternal summer shall not fade
When in eternal lines to time thou grow’st.

Substitute one bit of text with another, redirect output to a new file:

~$ #Perl:
~$ perl -pe 's/eternal/forevermore/g'   Sonnet_18.txt > new_sonnet.txt

~$ #Raku:
~$ raku -pe 's:g/eternal/forevermore/'  Sonnet_18.txt > new_sonnet.txt

Answered By: jubilatious1
Categories: Answers Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.