123

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?

186

Using cat

Since your file is short, you can use cat.

cat filename

Using less

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

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 configuration, you can use it forever.

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
0000023
  • +1, also sed -n l file could be useful. – enzotib Aug 11 '13 at 6:42
  • 1
    Does less have any clear advantages over other pager programs like pg, or does it just boil down to personal preference? – Sam Weinberg Aug 11 '13 at 19:22
  • @SamWeinberg: less has more features than pg. Take a look at the less(1) and pg(1) manpages. There are other pagers as well. Take a look at unix.stackexchange.com/questions/81129/…. less is probably the most widely-used pager but which one you use comes down to personal preference. – user26112 Aug 11 '13 at 19:33
  • don't forget more and most! – Rudolf Olah Nov 2 '15 at 17:16
28

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)"
or
  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.

Update:

< 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.

#!/bin/bash

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

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

  • Your update: "< filename is exactly what you want, ..." is misleading. Overall, although this is an interesting discussion on alternatives, I think cat is simpler. – X Tian Sep 29 '15 at 20:21
  • Bare < filename does not display contents of the file, but cat filename does. – jarno Jun 21 '17 at 19:57
  • You could use tee < filename, too. – jarno Jun 21 '17 at 20:42
  • 2
    +1 for the echo "$(<filename)" bashism. Not enough of that here. – Yokai Jan 11 '18 at 8:13
  • That's great idea for using Linux Internal Command with echo "$(<filename)". Thanks ... – Reza Harasani Sep 25 '18 at 5:52
9

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

cat filename 
  • For more information about cat, run man cat. – user26112 Aug 11 '13 at 3:58
9

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

or

more filename.txt

or

less filename.txt

For last few lines

tail filename.txt

For first few lines

head filename.txt
3

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)

  • less is the overkill-version of more (compare man less with man more), and for me it has two annoying features: 1) it switches to the alternate screen buffer, when less terminates, the file you were just viewing vanishes 2) at EOF you have to explicitly type q (I know, there's an option for this). So one of my first actions in a new environment is setting export PAGER=/bin/more in my profile and use more all the time. – ott-- Aug 11 '13 at 15:57
  • @ott--: 1) Try out the -X flag. 2) Try out the -E flag. less has a more emulation mode. You can enable it by setting the LESS_IS_MORE environmental variable. You can scroll upwards in the more emulation mode. – user26112 Aug 11 '13 at 17:35
  • See also: unix.stackexchange.com/questions/81129/… – user26112 Aug 11 '13 at 17:36
  • Though, in general, I do agree that less is overly-complicated. Its ability to run external commands is a perfect example of its over-complexity. – user26112 Aug 11 '13 at 19:00
  • @EvanTeitelman I've always found the grep functionality extremely useful myself, @ott-- I find that because of it's emulation of more, and it's many additional features, it does the job very well. – SG60 Aug 14 '13 at 14:04
3

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

2

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
Or
 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.

  • Shorter for vim -R is view. But keep in mind that it not likes redirections, as discussed in xargs and vi - “Input is not from a terminal”. – manatwork Aug 12 '13 at 11:26
  • thanks @manatwork for the heads up! I have recently started using Vim and I like it because of its several features. Regarding redirections, I forgot about that, thanks for reminder. As of now, I am working on a remote VM, where I use ssh without GUI interface, therefore, Vim is of great use, when any other GUI editor cannot work, that's why I emphasized Vim here. – Don't You Worry Child Aug 12 '13 at 11:48
1

Use cat command to display the content of filename. cat filename

Use vim command to edit file. vim filename

protected by Community Oct 11 '16 at 10:59

Thank you for your interest in this question. Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).

Would you like to answer one of these unanswered questions instead?

Not the answer you're looking for? Browse other questions tagged or ask your own question.