How to read file in linux

How do I open a text file in my terminal?

Want to improve this post? Provide detailed answers to this question, including citations and an explanation of why your answer is correct. Answers without enough detail may be edited or deleted.

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 ?

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

You might want to look into some beginner Ubuntu shell tutorials — They will explain how to do this and related stuff.

16 Answers 16

directly shows a text file in the terminal.

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

cat /home/john/RESULTS.txt less /home/john/RESULTS.txt 

upon entering a command like cat /home/suhail/RESULT.txt I get this cat: /home/suhail/RESULT.txt: No such file or directory

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

@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 , and there’s only one file in the current directory whose name starts with R , it will expand to the name of that file.

Another alternative is vim .

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.

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.

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

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 😉

Читайте также:  Linux установка ssl сертификата

view is a dumb four letter alias for vim -R . If you don’t use any save command like ZZ , :w or 😡 , 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.

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

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

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

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

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

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

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…

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

so that you can navigate through it with directional keys.

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

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.

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

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

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.

Читайте также:  Основные сведения об операционной системе linux

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.

Источник

How To Open A File In Linux Using 3 Different Approaches

How To Open A File In Linux Using 3 Different Approaches

Linux is a widely used open-source operating system that many programmers, IT professionals, and everyday consumers utilize. In Linux, we can display various file formats such as text files, audio files, videos, images, docs, pdf, or any other file contents. How to open a file in Linux is one of the core steps that any Linux user must know.

Before proceeding further, it’s crucial to know that Linux offers a variety of text editors and file managers that may be used to open and edit files. Linux also has a command-line interface that may be used to open files using different commands. In this article, we are going to discuss three different ways through which you can easily open and view a file in Linux.

Opening a Linux file using a File Manager

Linux provides several file managers that can be used to navigate and manage different kinds of files. Some of the popular file managers in Linux are Nautilus, Thunar, and PCManFM. To open a file using a file manager, follow the steps below:

Step 1: Open the file manager by clicking on the File Manager icon in the Application menu.

Step 2: Move to the location (file path) where the source file is already stored.

Step 3: Click on the file to open it. If the file is a text file, it will be opened in the default text editor.

Opening a Linux file using a Text Editor

Linux provides several text editors that can be used to edit text files. Some of the popular text editors in Linux are Nano, Vim, and Emacs. To open a file using a text editor, follow the steps below:

Step 1: Launch the text editor by typing the text editor name in the terminal. For example, to launch Vim, type «vim» in the terminal and press Enter.

Step 2: Scroll down to the location where the file is stored with the help of file navigation commands of the text editor.

Step 3: Open the file by typing the filename and pressing Enter.

Opening a Linux file using Command Line Interface

Linux provides a command-line interface that can be used to open files using various commands. The command will also work with other common file extensions. Following are some useful file commands to open a file from the terminal:

Читайте также:  Установка сервера alt linux

1. The cat Command

The cat command in Linux is a useful utility that is used to concatenate, display and create files in the terminal. The cat command is often used to display the contents of a complete file in the terminal window without actually opening it in a text editor or file manager. It prints the file content to the standard output in the default editor.

To display the content of the original file run the following file command in the terminal:

2. The less Command

The less command in Linux allows you to view the contents of a file one page at a time. Unlike the cat command, which displays the entire contents of a file in the terminal window, the less command is designed to handle large configuration files and allows you to scroll through the contents of an entire file one page at a time. It also supports better handling of binary files.

Run the following command in the terminal:

3. The head Command

The head command in Linux allows you to view the first few lines of a file by default. The head command is commonly used when you want to quickly preview the contents of a file without having to open the entire previous file.

To open a file by using the head command, run the following command in the terminal:

4. The tail Command

The tail command in Linux allows you to view the last few lines of the default file. The tail command is commonly used when you want to monitor the end of a log file or other constantly updating target file type.

Run the following command in the terminal text editor:

5. The more Command

The more command is another useful utility in Linux for opening files. It allows you to view the content in a file one screen at a time, allowing you to scroll through the current file and easily view its contents.

Open the terminal and run the following command:

6. The nl Command

In Linux, the nl command is used to add line numbers to a file. We can use this command in the same way as the cat command, and the only difference is that nl has line numbers enabled by default.

To open and view a file using nl command, run the following in the terminal window:

Conclusion

Opening and viewing a file in Linux is a very easy process. Every Linux user should be familiar with the fundamental process of opening a file. Many text editors, file managers, and command-line tools are available in Linux. The different methods mentioned above make it simple to open files in Linux and start editing them.

We hope now you have a good understanding of how to open a file in Linux. Here are some other suggested reads:

Shivangi Vatsal

I am a storyteller by nature. At Unstop, I tell stories ripe with promise and inspiration, and in life, I voice out the stories of our four-legged furry friends. Providing a prospect of a good life filled with equal opportunities to students and our pawsome buddies helps me sleep better at night. And for those rainy evenings, I turn to my colors.

Источник

Оцените статью
Adblock
detector