Javatpoint Logo
Javatpoint Logo

Open File in Linux

There are various ways to open a file in a Linux system. It is a fairly straight forward process to view the contents of a file, but if you are a new user, it may bother you. It is not as easy as opening a file in Notepad. From the Linux terminal, you must have some exposures to the Linux basic commands. There are some commands such as cat, ls, that are used to read files from the terminal.

In Linux, we can display various file formats such as text file, audio files, video, image, doc, pdf, or any other file contents.

Open File in Linux

Following are some useful ways to open a file from the terminal:

  1. Open the file using cat command.
  2. Open the file using less command.
  3. Open the file using more command.
  4. Open the file using nl command.
  5. Open the file using gnome-open command.
  6. Open the file using head command.
  7. Open the file using tail command.

1. Open File Using cat Command

This is the most popular and easy way to display the file content. It simply prints the file content to the terminal. It provides many options to make it more specific. To go in-depth with cat command, visit Linux cat.

To display the file content, execute the cat command as follows:

Let's create a file to understand how to open a file. Execute the below command:

The above command will create a text file 'Test.txt.' There are multiple ways to create a file, To learn, visit Linux Create File.

To display the file content of the above file, execute the command as follows:

cat Text.txt

Consider the below output:

Open File in Linux

The cat command is a very useful utility to open a file, but the main issue with it that it displays the file content on the terminal. If we have a big file, then it is not an ideal way to open a file. In such a situation, the less command will be handy as it displays a page at a time.

2. Open File Using less Command

The less command allows us to view one page at a time.

To display the file content, execute the less command as follows:

The above command will display the file content as a page at a time. To view more, scroll the output. Consider the below output:

Open File in Linux

To exit from the less command, press the 'q' key, and you will be back to the terminal. To go in-depth with less command, visit Linux less.

3. Open File Using more Command

The Linux more command is also used to display the file content. As the less command automatically adjusts the height and width of the terminal window, it cuts the content as the width of the terminal.

Use the following keys to scroll the output:

Press ENTER key to scroll down page line by line.

Press the SPACE bar to go to the next page.

Press 'b' key to go to the back page.

Press '/key:' to search the string.

To display the file content by the more command, execute it as follows:

The above command will display the file content as the width of the terminal window. Consider the below output:

Open File in Linux

To go in-depth with the more command, visit Linux more.

4. Open File Using nl Command

The nl command displays the file content with the line number. It is almost the same as the cat command. The major difference between both commands is that it prepends line numbers while displaying the output in the terminal.

To display the file content with nl command, execute the command as follows:

The above command will display the file content with the line number. Consider the below output:

Open File in Linux

5. Open File Using gnome-open Command

For the GNOME-based Linux environment, we can use the gnome-open command to open a file.

If the gnome utility is not installed on our machine, we can install it by using the following command:

Execute the above command and provide the system administrator password to install the GNOME utility.

Now, to display the file content, execute the command as follows:

The above command will open the file with the default file system. Consider the below snap of the file system:

Open File in Linux

There are some other popular Linux environments, such as KDE DESKTOP, OS X UNIX, and more.

For KDE Desktop, execute the command as follows:

or,

For OS X UNIX, execute the command as follows:

We can display the file content by using the head command, but it is slightly different than others. It displays the first part of files via standard input. By default, it displays the first ten lines of the files. It starts reading the file from the head (first line).

To open a file by using the head command, execute the command as follows:

The above command will display the content of the specified file. To go in-depth with the head command, visit Linux Head.

7. Open the file by Using tail Command.

The tail command is similar to the head command. The difference between both commands is that it starts reading the file from the tail (last line). Similar to head command, it also displays the output of the last ten lines by default.

To open the file by using the tail command, execute the command as follows:

The above command will display the content from the last line of the specified file. To go in-depth with the tail command, visit Linux Tail.


Next TopicLinux Edit File





Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA