Terminal-based file access in Linux streamlines workflows for developers, system administrators, and anyone working in environments without a graphical interface. Using the right commands not only speeds up file access but also provides greater control and flexibility, especially when dealing with configuration files, logs, or remote systems where a GUI is unavailable.
Open or View Files Using the Terminal
Efficient file management in Linux often starts with the terminal. The following methods cover the most effective and widely used commands for opening and viewing files directly from the command line.
View File Contents with cat
Step 1: Use the cat
command to display the full content of a text file in the terminal. This method is best for short files because it outputs everything at once, which can overwhelm the terminal for larger files.
cat filename.txt
This command prints the entire file to the terminal. Replace filename.txt
with your actual file name.
cat
with very large files, as it will flood your terminal with output.View Large Files with less
or more
Step 1: For lengthy files, use less
to scroll through content one page at a time. This command allows you to navigate with arrow keys and search within the file. Press q
to quit.
less filename.txt
Step 2: Alternatively, use more
for basic paging through the file. It's similar to less
but with fewer navigation features.
more filename.txt
Both commands are ideal for viewing logs, configuration files, or documents too long for a single screen.
Display Specific File Sections with head
and tail
Step 1: Use head
to show the first few lines of a file. By default, it displays the first 10 lines. Adjust the number using the -n
option.
head filename.txt
head -n 5 filename.txt
Step 2: Use tail
to display the last lines of a file, which is especially useful for monitoring log files. The -f
option lets you follow new lines as they are added.
tail filename.txt
tail -f filename.txt
These commands help you quickly preview file beginnings or track live updates.
Open Files with Line Numbers Using nl
Step 1: To display a file’s content with line numbers, use the nl
command. This is especially helpful when referencing specific lines in code or logs.
nl filename.txt
This command prints each line prefixed with its line number, making it easier to discuss or debug file content.
Edit or Open Files in Terminal Text Editors
Sometimes you need to modify a file, not just view it. Terminal-based editors provide powerful ways to open and edit files directly from the command line.
Step 1: Use nano
for a straightforward, user-friendly editing experience. Open a file with:
nano filename.txt
Navigation and editing in nano
are intuitive, with shortcuts displayed at the bottom. Save changes with Ctrl + O
and exit with Ctrl + X
.
Step 2: For advanced editing, use vim
or emacs
. Open a file in Vim with:
vim filename.txt
These editors provide extensive features for power users, including syntax highlighting, search, and scripting support. If you only want to view a file in Vim without editing, use view filename.txt
(read-only mode).
Open Files with the Default Application Using xdg-open
To open any file type (text, PDF, image, video) with its default system application from the terminal, use xdg-open
. This command works in most Linux desktop environments and is especially useful for non-text files.
xdg-open filename.pdf
xdg-open image.jpg
This approach launches the appropriate graphical application for the file type, just as if you had double-clicked the file in a file manager.
gnome-open
can be used, while KDE users can use kde-open
or kioclient exec
for similar functionality.View Files with Alternative Terminal Commands
Linux offers additional commands for specialized file viewing:
grep
: Filter and display lines matching a pattern within a file.sed
: Print and optionally edit file content line by line.strings
: Extract and display human-readable text from binary files.pv
: Output file content at a controlled rate, useful for monitoring or demonstrations.
These commands provide flexibility for searching, transforming, or analyzing file content directly from the terminal.
Open Media Files from the Terminal
Media files such as images, audio, and video can also be opened from the terminal. While you won’t view the media inline in the terminal window, you can launch the default or a specific application to handle the file.
Step 1: Use xdg-open
for the simplest approach:
xdg-open myvideo.mp4
xdg-open photo.png
Step 2: Launch media files with a specific application, such as VLC for videos or Eye of GNOME (eog) for images:
vlc myaudio.mp3
eog image.jpg
For terminal-only environments without a graphical interface, use frame buffer tools like mplayer -vo fbdev2
for videos or fbi
for images.
Open Files with Administrative Permissions
System configuration files often require elevated privileges to view or edit. Use sudo
with your preferred editor to open these files safely:
sudo nano /etc/hosts
sudo vim /etc/fstab
Always exercise caution when editing system files, as incorrect changes can disrupt system functionality.
Mastering these terminal commands for opening and viewing files in Linux leads to faster workflows, improved troubleshooting, and greater flexibility—whether you're working locally or on a remote server.
Member discussion