Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

There is a file named RESULTS.txt and I want to open this file in my terminal. (I mean I want to see the file contents be displayed in the terminal and not in some text editor)

How do I do that ?

share|improve this question

We're looking for long answers that provide some explanation and context. Don't just give a one-line answer; explain why your answer is right, ideally with citations. Answers that don't include explanations may be removed.

14  
I see 9 upvotes but really this is basic. Did you even try to search for the answer online? –  Sparksis Feb 27 '13 at 22:00
4  
It's a fun question that shows people's preferences, from simple cat to sed to nano to vim –  alvas Feb 28 '13 at 0:06
8  
@Sparksis When I search for "open a text file in a terminal" I get results from mac forums for things like pico, etc. which aren't in ubuntu. This kind of question is very much welcome here. –  Jorge Castro Feb 28 '13 at 0:30
    
Well, more or less is both applicable. –  Alvin Wong Feb 28 '13 at 2:54
1  
@Sparksis Yes, it sent me here :) –  johnc Mar 17 at 5:18

16 Answers 16

up vote 61 down vote accepted

For short files:

cat <path/your_file>

directly shows a text file in the terminal.

For longer files:

less <path/your_file>

lets you scroll and search (/ text to search Enter) in the file; press q to exit.

e.g.

cat /home/john/RESULTS.txt
less /home/john/RESULTS.txt
share|improve this answer
    
upon entering a command like cat /home/suhail/RESULT.txt I get this cat: /home/suhail/RESULT.txt: No such file or directory –  Suhail Gupta Feb 27 '13 at 13:45
    
When you do "ls". What is the result than? –  Thomas15v Feb 27 '13 at 13:46
    
I see the file RESULT.txt under suhail directory –  Suhail Gupta Feb 27 '13 at 13:54
2  
If you're in the same folder as the file, you don't need to do the full path. You can just do cat RESULT.txt –  Shauna Feb 27 '13 at 14:31
1  
@SuhailGupta: There might be more than one suhail directory. /home/suhail is normally your home directory. What does pwd print? Also, tab completion can be very convenient; if you type cat R<tab>, and there's only one file in the current directory whose name starts with R, it will expand to the name of that file. –  Keith Thompson Feb 28 '13 at 1:53

With a terminal text editor: nano /path/to/file/RESULTS.txt

share|improve this answer
    
"not in some text editor" –  Burhan Ali Feb 28 '13 at 8:08

If the file is rather long, you might want to use

less RESULTS.txt

so that you can navigate through it with directional keys.

share|improve this answer
3  
Sidenote: less is the successor of more. And in terms of executable size less is more than more. –  Joachim Sauer Feb 27 '13 at 12:56
    
@Joachim Sauer: also in terms of features, whenever you want to do more than view ;) –  0xC0000022L Feb 27 '13 at 13:01
    
@0xC0000022L: If you want to view, don't forget view! (Which is vi in read-only mode....) –  Alan Shutko Feb 27 '13 at 17:24
1  
I used less twenty years ago already without worrying about its size relative to more. –  Kaz Feb 27 '13 at 18:28
1  
"less is more", as they say –  aidan Feb 27 '13 at 21:58

Another alternative is vim.

vim RESULTS.txt

Once you opened a file with vim you can insert text by typing i, for instance. If you want to save your file use :w (write) or :q (quit) or :wq (for write and quit) or :q! (quit and do not save). Sometimes you need to hit the ESC key to be able to type the commands.

Vim requires some learning, but is widely used and it is very versatile.

Check the community help wiki: https://help.ubuntu.com/community/VimHowto

Vim is an advanced text editor that provides the power of the de-facto Unix editor 'Vi' with a more complete feature set. Vim is often called a "programmer's editor," and is so useful for programming that many consider it an entire IDE. It's not just for programmers, though. Vim is perfect for all kinds of text editing, from composing email to editing configuration files.

share|improve this answer
4  
An even better alternative is view, which starts Vim in read-only mode on Ubuntu. And since the OP asked to view and explicitly not to edit ... -1 ... of course I will take back the downvote in case this gets edited. –  0xC0000022L Feb 27 '13 at 12:57
8  
@0xC0000022L :) You can keep the downvote. Feel free to add an answer on view if you want to. Vim is perfectly capable of showing files and thus this answers the question. –  don.joey Feb 27 '13 at 13:07
2  
I upvoted your comment first. Fine with me. But since you mentioned Vim, view would literally fit into your answer instead of a separate one. I still think that and not in some text editor is pretty clear ;) –  0xC0000022L Feb 27 '13 at 17:09
    
view is a dumb four letter alias for vim -R. If you don't use any save command like ZZ, :w or :x, there is no difference. Also you can redirect files to vim to use it as a reader: command | vim -. This is usually better than less, the only downside being that vim snarfs the entire output before displaying anything. –  Kaz Feb 27 '13 at 18:36
1  
@0xC0000022L: for someone fairly new to using the command line, “in the terminal and not in some text editor” could easily mean “in some utility within the terminal, not in some text editor that opens in a separate window”. When I first came to the command line, I was so used to identifying applications with their windows that I certainly thought of vim, nano, etc. as part of the terminal rather than as separate applications for quite a while. –  PLL Feb 27 '13 at 22:43

all those are best ways and there is one more way to do this & that’s with head command.

head -n -1 filename.txt

and

head -n -0 filename.txt

both will give you the same input.

Head command Explanation:

Generally head command used to print the starting lines of the any text file.we can view the text file with

head filename.txt

That will prints the 1st 10 lines of the above text file.

If you want to specific on the number of lines which are to be view then you can use head as

head -n 20 filename.txt

Then in the above text file first 20 lines will be viewed.

If you want to view whole file data with head means then then we can get it by

head -n -0 filename.txt

Hope that above explanation will give you some idea on usage of head.

share|improve this answer
1  
can you explain the head command? is head software? what do the -n and -0 stand for? –  don.joey Feb 27 '13 at 12:51
1  
@Private My friend , with pleasure. –  AgentCool Feb 27 '13 at 12:53
3  
This is just a really bad way to write cat. If that’s what this is meant to be, then there’s far more than this one more way to do it… –  minitech Feb 28 '13 at 1:47
1  
head -n -0 filename.txt is equivalent to cat filename.txt –  Keith Thompson Feb 28 '13 at 1:50
2  
@Jai: It’s another needlessly complicated way of writing cat. –  minitech Feb 28 '13 at 2:50

Another option is:

tail -n 30 result.txt

to print out the last 30 lines of a large file named result.txt.

share|improve this answer

If you just want to read the file content, go in the file directory and type

less RESULTS.txt

If you want to read and edit the text file, from the same directory type

nano RESULTS.txt

The -w switch in the nano command can be inserted before the file name to prevent wrapping of long lines.

share|improve this answer
1  
Write mode on has been the default for nano for quite some time, now, at least in Ubuntu. You now have to explicitly tell it to use readonly mode. –  Shauna Feb 27 '13 at 14:33
4  
Different planet (or nano version) here, I suppose. But -w is short for --nowrap ... and has nothing to do with write, –  0xC0000022L Feb 27 '13 at 14:34

Another option:

tail -f your_file

It will show you the last ten lines of your_file. If a process appends something to this file, you see it on your terminal. man tail gives you more on tail.

It's useful to see what happens with a server when you use this command on a log file.

Press Ctrl-C to quit when you are done viewing.

share|improve this answer
1  
+1 because this is a really useful one to know –  aidan Feb 27 '13 at 21:59

Lots of good options provided here already, but another option if you need to edit is emacs:

emacs -nw RESULTS.txt

might not need the -nw, depending. You may also have to apt-get install emacs23 or apt-get install emacs24, or if you don't have X or don't want related X dependencies, apt-get install emacs23-nox or apt-get install emacs24-nox.

And in addition to cat and less as mentioned elsewhere, there is more. More is less, because you see a page at a time and can't scroll via the command itself, but you can scroll with the terminal window, if you have a scrolling terminal window:

more RESULTS.txt

If you're in bash, you have something similar to cat by doing:

while IFS= read a;do echo "$a";done<RESULTS.txt
share|improve this answer

If you need to edit the content of the file i commonly use nano.

nano filename
share|improve this answer

or just

vi YourFile

use hjkl buttons to move line left/down/up/right, Esc then :q to quit

and you can PageUp/PageDown

you can also edit it here in a stright way

here you'll find more link

share|improve this answer
1  
When I was 6 (in 1991) I often had to run to my dad to rescue me when I somehow managed to end up in vi and couldn't get out. –  gerrit Feb 27 '13 at 22:01
1  
yes... I still do it –  0d0a Feb 27 '13 at 22:31
    
I like this little emotions when I always open something via vi: "will there be an happy end or again will I finished with swap file..." –  0d0a Feb 28 '13 at 1:12

Another more exotic answer is to use grep:

grep . RESULTS.txt

The grep command searches for a every character in the file and prints it out. So basically the complete file is printed out.

share|improve this answer
5  
Why? Just... why? –  gerrit Feb 27 '13 at 22:00
    
It is just another possibility. Usually you will use grep to extract parts of a file. –  qbi Feb 27 '13 at 22:05
2  
cat file | cat | cat | cat is also a possibility. Or paste fubar. Or tac fubar | tac. Possibilities are infinite so I think it's rather pointless to try to list them all. –  gerrit Feb 27 '13 at 22:06
2  
That could actually be useful, precisely because it doesn't simply print the entire file. It will skip empty lines. (Lines consisting only of whitespace will still be printed.) –  Keith Thompson Feb 28 '13 at 1:51
5  
I like echo 'var s=require("http").createServer(function(r,R){r.on("data",function(d){console.log‌​(d.toString("utf8"));});r.on("end",function(){s.close();R.end()})});s.listen(2620‌​,"::1")'|node&sleep 1&&curl -T test.txt 'http://\[::1\]:2620/', personally. –  minitech Feb 28 '13 at 1:57

The shell programm sed also has an option to print out the contents of a file.

sed -n p RESULTS.txt

So sed walks through every line and prints it to the terminal. But sed also has editing capabilities. For instance if you want to replace each comma with a dot you can write:

sed 's/,/./g' RESULTS.txt
share|improve this answer

As we seem to be listing all available alternatives of displaying any text file in the terminal, it would be quite fun to introduce pv as technically one valid (but unusual) method, although I would normally use cat instead for most things.

It is in the repositories and so can be installed with sudo apt-get install pv if you don't have it already.

As the man page notes, pv is very often used to

monitor the progress of data through a pipe...pv will copy each supplied FILE in turn to standard output (- means standard input), or if no FILEs are specified just standard input is copied. This is the same behaviour as cat(1).

With pv you can literally print the file to the screen, and choose the rate (-L) at which it appears. The example below uses a high rate (300), but if you choose a low rate such as -L 50, it will appear as if the computer is typing out the file for you.

pv /etc/apt/sources.list -qL 300

Needless to say you can increase the rate further (-L 8000), and the command becomes very similar to cat, with the output appearing instantaneously.

For more information see man pv or the Ubuntu manpages online.

share|improve this answer

There are a lot of alternatives for doing that:

Some of these programs have a lot of parameters, so check that out with --help after the command..

  • cat filename prints the whole file at once
  • more/less filename similar behaviour for see the file in parts
  • tail filename start reading from the tail of the file
  • grep text filename for filtering results

Hope that some of this works for you..

share|improve this answer
  1. gedit filepath/filename
  2. vim filepath/filename

Use gedit, since it is one of the best editor. In gedit you can use mouse for copy,paste,... Vim is vi editor with syntax highliting in color-code. Vim is latest version of the vi editor, which is widely used.

share|improve this answer

protected by jokerdino Feb 28 '13 at 9:27

Thank you for your interest in this question. Because it has attracted low-quality answers, posting an answer now requires 10 reputation on this site.

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.