The basic way to open a file in Linux
Opening a file in Linux depends on what kind of file it is and whether you are using a graphical desktop or working from the command line. If you are at a desktop with a file manager (the folder icon that shows your files), you can double-click any file to open it with the default program — the same way you would on Windows or Mac. If you are working in the terminal, you type a command that tells Linux which program to use and which file to open.
The most common command is cat, which displays text file contents directly in the terminal. Type cat filename.txt and press Enter, and the file's contents appear on screen. For larger text files, use less instead — type less filename.txt — which lets you scroll through the file one page at a time using the spacebar. Press q to quit and return to the command prompt.
If you want to edit a file rather than just read it, the most straightforward option is nano, a simple text editor built into most Linux systems. Type nano filename.txt and the file opens in an editor where you can make changes. When you are done, press Ctrl+X, then Y to confirm you want to save, then Enter to keep the filename.
Key Takeaways
- Double-clicking a file in your file manager opens it with the default program for that file type, just like on other operating systems.
- In the terminal, cat filename.txt displays a text file's contents, while less filename.txt lets you scroll through longer files.
- Use nano filename.txt to open and edit a text file directly in the terminal without switching to a separate application.
- You can open files with a specific program by typing the program name first — for example, gedit filename.txt opens the file in the gedit text editor.
- If you are not sure what program a file needs, right-click it in your file manager and choose "Open With" to see available options.
Opening files through the graphical file manager
Most Linux desktops come with a file manager that works like the folder application on Windows or Mac. Click the folder icon in your taskbar or desktop to open it, then navigate to the folder containing your file. Once you find the file, double-click it. Linux will open it with whatever program is set as the default for that file type — a .txt file opens in a text editor, a .pdf opens in a PDF reader, an image file opens in an image viewer, and so on.
If the default program is not what you want, right-click the file instead of double-clicking. A menu appears with an option like "Open With" or "Open With Other Application". Click that option and you will see a list of programs installed on your system that can open that file type. Select the one you want and click it. This one-time choice does not change the default — next time you double-click the same file type, it will still use the original default program.
If you want to change the default program permanently for all files of that type, right-click the file, look for "Properties" or "Get Info", and find the section that says "Opens with" or "Default application". Click the dropdown menu there and choose a different program, then look for a button that says "Set as default" or "Change". The next time you double-click a file of that type, it will use the new program.
Opening files from the terminal with cat, less, and nano
The terminal is the command-line interface where you type instructions instead of clicking. If you are already in the terminal and need to view a file, cat is the fastest option for short files. Type cat myfile.txt and the entire contents appear on screen at once. This works well for files that fit on one screen, but for longer files the text scrolls past too quickly to read.
For longer files, use less instead. Type less myfile.txt and the file opens in a viewer where you control the pace. Press the spacebar to move down one page, press B to move back up one page, or use the arrow keys to scroll line by line. Type a forward slash / followed by a word to search for that word in the file. When you are done reading, press Q to quit and return to the command prompt.
If you need to make changes to the file, nano is the simplest terminal editor. Type nano myfile.txt and the file opens in an editor where you can type and delete just like in any text editor. The bottom of the screen shows keyboard shortcuts — Ctrl+X means hold Control and press X. To save your changes, press Ctrl+X, then press Y when asked if you want to save, then press Enter to confirm the filename.
Opening files with a specific program
Sometimes you want to open a file with a program other than the default, and you want to do it from the terminal. You can specify the program name before the filename. For example, gedit myfile.txt opens the file in gedit, a graphical text editor. Type firefox document.pdf to open a PDF in Firefox, or vlc video.mp4 to open a video file in the VLC media player.
The program name you use depends on what is installed on your system. Common ones include gedit (text editor), libreoffice (word processor and spreadsheet), firefox (web browser), vlc (video player), and gimp (image editor). If you type a program name that is not installed, you will get an error message saying the command was not found.
When you open a file this way from the terminal, the program launches in the background and you can keep using the terminal for other commands. If you want the terminal to wait until you close the program before returning to the command prompt, add an ampersand at the end — for example, gedit myfile.txt & — though this is optional and most people do not bother.
Finding and opening files when you do not know the location
If you know the filename but not where it is stored, the file manager's search function is the quickest way. Open your file manager, look for a search icon (usually a magnifying glass) in the toolbar, click it, and type the filename. The file manager will search your home directory and show you all files matching that name. Double-click the one you want to open it.
From the terminal, use the find command to search for a file by name. Type find ~ -name "myfile.txt" to search your home directory for a file called myfile.txt. The tilde ~ means your home directory. Once you find the file, you can open it with any of the methods described above — just use the full path that find shows you. For example, if find shows /home/username/Documents/myfile.txt, you can type cat /home/username/Documents/myfile.txt to view it.
If you remember only part of the filename, use a wildcard. Type find ~ -name "*report*" to find all files with "report" anywhere in the name. The asterisk * means "any characters". This search takes longer on large systems but will show you every matching file.
Troubleshooting when a file will not open
If you double-click a file and nothing happens, the file type may not have a default program assigned. Right-click the file and choose "Open With" to see what programs are available. If the list is empty or shows only one option that did not work, you may need to install a program that handles that file type. For example, if you have a .docx file and no word processor is installed, you would need to install LibreOffice or another compatible program first.
If you get a "Permission denied" error when trying to open a file from the terminal, the file may have restricted permissions. Type ls -l filename to see the permissions. If the file shows -rw-r--r-- or similar, you should be able to read it. If it shows ----------, the file is locked. Ask the person who owns the file to change the permissions, or if it is your own file, type chmod 644 filename to make it readable.
If a file is corrupted or in an unusual format, the program may refuse to open it or display an error message. Try opening it with a different program — right-click and choose "Open With" to see alternatives. If no program can open it, the file may be damaged beyond repair, or it may be a file type that your system does not support.
Frequently Asked Questions
What is the difference between cat and less?
Cat displays the entire file at once, which is fast for small files but overwhelming for large ones. Less shows one page at a time and lets you control scrolling, making it better for reading longer files. Both only display the file — neither one edits it.
How do I open a file with a program that is not the default?
In the file manager, right-click the file and choose "Open With", then select a different program from the list. From the terminal, type the program name before the filename — for example, gedit myfile.txt instead of just double-clicking.
Can I open a file that is in a different folder without typing the full path?
From the terminal, you need the full path or a relative path from your current location. In the file manager, navigate to the folder first, then double-click the file. You can also use the find command to locate the file and copy its full path, then use that path in your command.
What does "command not found" mean when I try to open a file?
It means the program you typed is not installed on your system. Check the spelling first — for example, it is libreoffice, not libre office. If the spelling is correct, the program is not installed and you will need to install it through your system's package manager before you can use it.
How do I open a file that has spaces in its name?
In the terminal, put the filename in quotes: cat "my file.txt" or nano "my document.txt". Without quotes, the terminal treats each word as a separate argument and the command fails. In the file manager, spaces in filenames work normally — just double-click as usual.