Script Valley
Linux Basics Complete Course: From Beginner to System Administrator
Module 3: File Management CommandsLesson 3.2

Reading and Editing Files in Linux | cat, less, nano, grep

cat, less, more, head, tail, nano, vim, grep, reading files in Linux

Reading and Editing Files in Linux

Reading file contents and searching within files are among the most frequent tasks in Linux system administration and development. The Linux terminal commands covered in this lesson allow you to view, search, and edit files quickly and efficiently without a graphical interface.

Viewing Files with cat, less, and more

The cat command concatenates and displays the full contents of one or more files. It is best for short files. For long files, use less, which provides pagination — you can scroll up and down and search with the forward slash key.

cat /etc/hostname
cat file1.txt file2.txt
less /var/log/syslog
more /etc/passwd

Inside less, use the arrow keys or Page Up/Page Down to scroll, / to search forward, ? to search backward, and Q to quit.

Viewing the Beginning and End of Files

The head command shows the first 10 lines of a file by default. The tail command shows the last 10 lines. The -n option lets you specify how many lines to show. The -f option in tail follows the file live, making it ideal for monitoring log files in real time.

head /etc/passwd
head -n 20 /var/log/syslog
tail /var/log/nginx/error.log
tail -f /var/log/syslog

Searching Inside Files with grep

The grep command searches for patterns inside files and prints matching lines. It is one of the most powerful and frequently used Linux commands. The -i flag makes the search case-insensitive, -r searches recursively through directories, and -n shows line numbers.

grep "error" /var/log/syslog
grep -i "warning" /var/log/syslog
grep -rn "TODO" /home/alice/projects/
grep -v "#" /etc/ssh/sshd_config

The -v flag inverts the match, showing all lines that do NOT match the pattern. This is useful for filtering out comments from configuration files.

Editing Files with nano

Nano is a simple, beginner-friendly terminal text editor. Open a file with nano filename. Use Ctrl+O to save (write out) and Ctrl+X to exit. The shortcuts are shown at the bottom of the screen.

nano /etc/hosts
nano ~/notes.txt

Introduction to vim

Vim is a powerful but initially confusing editor. It has two main modes: normal mode for navigation and commands, and insert mode for typing text. Press I to enter insert mode, Esc to return to normal mode, and type :wq to save and quit.

vim config.txt

For most beginners, nano is the better starting point, but learning vim is a valuable long-term investment since it is available on virtually every Linux server.

Up next

Input/Output Redirection and Pipes | Linux Terminal Commands

Sign in to track progress