How to open a file in Linux Terminal?

This blog post guides you on how to open a file in Linux terminal.

This will lead to:

If you want to open a text-based, audio, video, doc, image, pdf or any kind of file in a terminal of a Linux distros like Ubuntu or Debian, Linux mint or fedora, you can do it simply by following the instructions below.

While there are several ways to achieve this, using the Cat command is the best option.

First open the terminal by pressing Ctrl+Alt+T in Ubuntu, or in Gnome press Alt+F2. Now, Put this command cat <path/filename.txt>  and replace the FILE NAME with yours. if you’re already on the path then you don’t need to describe it again. Following that, the content will be displayed in the Linux terminal. 

Now that you’re familiar with opening files, let’s dive into how to simplify reading files in the CLI mode.

Open a file in an editor

If you don’t want to use the Cat command which will display the content directly in the terminal. Thus, going through big data feels like a real pain. So in case you want to open files in a text-editor to deal with hefty lines of data, also want to perform some actions in there.

Vim is a good option; it comes by default in most linux distros, mainly in Ubuntu.

Put this command vim filename.txt into the terminal.

After you input the above command, the file will be opened up and it will be editable. A few useful commands out there that vim offers to simply the task such as using :w command you can save edits. And wq: command will close the file without saving edits. 

Only view command

Vim also offers view-only content which can be accessed by using this command view filename.txt. Note that this command can only be used by Ubuntu users. 

open files in a terminal

As stated above, there are multiple ways for doing this. If you’re a beginner in Linux at that moment it would be feasible for you to learn some basic commands to help you operate Linux through command shell.

Below are Commands you can use to open files in a terminal.

  • Cat command filename: prints out an entire file’s content.
  • more filename: Similar to cat yet it prints results in multiple screens instead of one.
  • tail filename: Displays a file’s content from bottom.
  • grep text filename: used for filtering a file’s content.
  • gnome-open filename.txt: the command works for Gnome based Linux distros.
  • kde-open <filename.txt>: work with KDE DE (desktop Environment)
  • head -n <file name>: A heads command displays a file’s content from the top. You can substitute the number of lines into that command to read the exact number of lines.

At this point you may have understood some basics, let’s just explore more of these commands to be better at them.

Cat Command

It’s useful to print content in a terminal, however you will still face some downsides with it when dealing with files with large data inside. 

The Cat command can do wonders for you, have a look at its useful commands that turn out playing with files a lot easier. Read more About cat commands

cat > [fileName]Used to create a file.
cat [oldfile] > [newfile]Used to copy and paste content from older to new files.
cat [file1 file2 and so on] > [new file name]Used to merge contents of multiple files into one.
cat -n/cat -b [fileName]Used to show the number of lines.
cat -e [fileName]Used to Identify the end word of each line with the $ character.
cat [fileName] <<EOFUsed to indicate the end of a page.

More command

Although you can view content using cat, it only displays it all over on one screen, so you will need to roll off the entire screen. Thanks to the More command that organized all the content into separate pages. Results are printed in multiple screen-tabs.

List of important More commands to better understand what it can do. Know about The More command

more -numSets the maximum number of lines to show up per page.
more -dMessage displaying at right corner of screen.
more -sIt squeezes blank lines so that only content appears.
more +/string nameUsed to locate strings in a file, it is useful.
more +numUsed to print results of a specific line.

gnome-open Command

If you’re using Gnome DE, many of the above commands might not perform well for you. Anyways, Gnome has its separate command which is gnome-open filename

In the same way, if you’re into KDE DE, then you should try opening files with the following command. kde-open <file name> 

head command

Head command also can be used to print results in the linux terminal. But the main problem with this method is that it shows only the top 10 lines of a file’s content. Anyways, you can take on this by writing a certain number after the head command, for example, head -20 <file name>. Know more about the head commands.

Similarly there is a tail command tail <file name>

Using this command you can view a file’s content from the bottom. And like the head command, you can write the number to patch the result of a specific number of lines.

Scroll to Top