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

1,175,776

Solution 1

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

Solution 2

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

Solution 3

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

cat filename 

Solution 4

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

Solution 5

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)

Share:
1,175,776

Related videos on Youtube

David
Author by

David

Updated on September 18, 2022

Comments

  • David
    David over 1 year

    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?

    • Admin
      Admin almost 3 years
      Just a reminder, the question is for Linux, for Windows you use type filename command
  • Admin
    Admin almost 11 years
    For more information about cat, run man cat.
  • Admin
    Admin almost 11 years
    +1, also sed -n l file could be useful.
  • ott--
    ott-- almost 11 years
    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.
  • Chris Roth
    Chris Roth almost 11 years
    @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.
  • Chris Roth
    Chris Roth almost 11 years
  • Chris Roth
    Chris Roth almost 11 years
    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.
  • Admin
    Admin almost 11 years
    Does less have any clear advantages over other pager programs like pg, or does it just boil down to personal preference?
  • manatwork
    manatwork almost 11 years
    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”.
  • Don't You Worry Child
    Don't You Worry Child almost 11 years
    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.
  • SG60
    SG60 almost 11 years
    @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
    SG60 over 10 years
    @ott-- I've never had problems with less's alternate screen buffer, as it only does this when the file to show will not fit on the screen in its entirety.
  • X Tian
    X Tian over 8 years
    Your update: "< filename is exactly what you want, ..." is misleading. Overall, although this is an interesting discussion on alternatives, I think cat is simpler.
  • Admin
    Admin over 8 years
    don't forget more and most!
  • jarno
    jarno almost 7 years
    Bare < filename does not display contents of the file, but cat filename does.
  • jarno
    jarno almost 7 years
    You could use tee < filename, too.
  • Yokai
    Yokai over 6 years
    +1 for the echo "$(<filename)" bashism. Not enough of that here.
  • Saman Bayat
    Saman Bayat over 5 years
    That's great idea for using Linux Internal Command with echo "$(<filename)". Thanks ...
  • puerile
    puerile over 3 years
    Nice explanation on why less was the name
  • Admin
    Admin over 2 years
    You can also use the tail command to see the last lines of text.
  • Admin
    Admin almost 2 years
    cat may be simple and easy, but using it like this will not pass Shellcheck.