To display the contents of a text file called data, what command should be used?

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?

To display the contents of a text file called data, what command should be used?

Jeff Schaller

64.7k34 gold badges106 silver badges239 bronze badges

asked Aug 11, 2013 at 3:41

Sam WeinbergSam Weinberg

2,7232 gold badges12 silver badges5 bronze badges

1

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

answered Aug 11, 2013 at 4:03

6

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

To display the contents of a text file called data, what command should be used?

Jeff Schaller

64.7k34 gold badges106 silver badges239 bronze badges

answered Apr 10, 2015 at 13:35

crunshercrunsher

7245 silver badges5 bronze badges

7

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

answered Aug 11, 2013 at 16:55

AbhishekAbhishek

2911 silver badge4 bronze badges

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

cat filename 

answered Aug 11, 2013 at 3:44

forvaidyaforvaidya

2011 silver badge5 bronze badges

1

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 Apr 20, 2015 at 11:17

Michael DurrantMichael Durrant

39.5k68 gold badges155 silver badges229 bronze badges

1

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 Aug 11, 2013 at 14:48

SG60SG60

412 bronze badges

6

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.

answered Aug 12, 2013 at 10:43

To display the contents of a text file called data, what command should be used?

2

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

Use vim command to edit file. vim filename

answered Aug 15, 2013 at 2:04

Edward ShenEdward Shen

8484 silver badges8 bronze badges

Which command should you use to display the content of a file less?

Crack open a terminal window and navigate to a directory containing one or more text files that you want to view. Then run the command less filename , where filename is the name of the file you want to view.

Which of the following commands is used to display the contents of a file called example TXT?

Use the command line to navigate to the Desktop, and then type cat myFile. txt . This will print the contents of the file to your command line. This is the same idea as using the GUI to double-click on the text file to see its contents.

What command can be used to display the last five lines of a text file?

To look at the last few lines of a file, use the tail command. tail works the same way as head: type tail and the filename to see the last 10 lines of that file, or type tail -number filename to see the last number lines of the file. Try using tail to look at the last five lines of your .

Which of the following Linux command is used to display a file page by page on the screen?

The cat command is the simplest way to view the contents of a file. It displays the contents of the file(s) specified on to the output terminal. Sometimes, we might want to number the lines in the output.