How to Open Files Using Linux Commands: A Practical Guide
Working with files on a Linux system is one of the most fundamental tasks you’ll encounter. Whether you’re editing configuration files, viewing logs, or simply checking document contents, knowing how to open files via the command line can boost your productivity and help you work more efficiently.
In this guide, we’ll explore various Linux commands that allow you to open files for viewing or editing. We will also provide practical examples so you can easily follow along.
Why Use Command Line to Open Files?
- Speed: Switching between graphical interfaces and terminals can be time-consuming.
- Remote Access: When connected to remote servers via SSH, graphical tools are often unavailable.
- Automation: Scripts often require command-line file manipulation.
Let’s dive in!
1. View a File Without Editing
Sometimes you only want to peek at a file’s content without making changes.
Using cat
cat
prints the entire content of a file to the terminal.
cat filename.txt
Example:
cat /var/log/syslog
Note: If the file is very large, this dumps it all at once and can be hard to read.
Using less
less
lets you scroll through files page-by-page, forward and backward.
less filename.txt
Example:
less /var/log/syslog
Use arrow keys or spacebar to navigate. Press q
to quit.
Using more
Similar to less
, but with fewer features:
more filename.txt
2. Edit a File
If you want to modify a file directly from the terminal:
Using nano
A simple text editor that's user-friendly for beginners.
nano filename.txt
Example:
nano notes.txt
Commands are displayed at the bottom. Use Ctrl + O
to save and Ctrl + X
to exit.
Using vim
/vi
Powerful but more complex editors for experienced users.
vim filename.txt
Basic usage:
- Press
i
to enter insert mode (start editing). - After making changes, press
Esc
. - Type
:wq
and press Enter to save and quit. - To quit without saving: type
:q!
and press Enter.
3. Open Files With Specific Applications
Sometimes you may want to open files in GUI apps from the terminal (when working in a desktop environment).
Using xdg-open
This command opens a file with its default application.
xdg-open filename.pdf
Example:
xdg-open report.pdf
This opens the PDF with your default PDF viewer.
Using Application Executables
You can launch apps directly from command line with a file as an argument:
Example: Open an image with Image Viewer (eog
) or GIMP
eog picture.jpg &
gimp picture.jpg &
The ampersand (&
) runs the app in background so your terminal remains usable.
Summary Table
Task | Command | Notes |
---|---|---|
View file content | cat filename | Outputs entire file at once |
Scroll through file | less filename | Navigable pager |
Edit with simple editor | nano filename | Beginner-friendly text editor |
Edit with advanced editor | vim filename | Powerful but has a learning curve |
Open file in GUI app | xdg-open filename | Opens default associated app |
Bonus Tips
-
Use tab completion when typing filenames for speed.
-
Use wildcards like
*.txt
in commands. -
Combine commands like viewing compressed logs:
zless /var/log/syslog.1.gz
Final Words
Mastering these simple yet powerful Linux commands will greatly improve your ability to work within terminal environments — especially when managing servers or performing quick edits remotely. Start practicing these today, and soon opening files on Linux will feel second nature!
If you liked this tutorial, subscribe for more Linux tips and tricks!
If you want me to add anything specific—such as shell scripting examples related to opening files, troubleshooting tips, or comparing graphical vs. command-line methods—just let me know!