👁️ Listing and Viewing Files
One of the most fundamental skills in Linux is knowing how to list and view files in directories. The terminal is your file explorer, and these commands are your tools. Think of them like opening your file manager on Windows or macOS — but way more powerful and flexible.
ls — Listing Files and Directories
Think of the ls command as the "File Explorer" of the Linux terminal. When you open your file explorer on Windows or macOS, you see folders and files. In Linux, instead of clicking, you type ls to see what's inside a directory.
This shows the list of files and folders in your current directory.
Documents Downloads Pictures Music
That's like opening your home folder and seeing all those directories.
Hidden files in Linux start with a dot (.). To see them:
. .. .bashrc Documents Downloads
- . → current directory
- .. → parent directory
- .bashrc → hidden file
👉 Think of it like turning on "Show hidden files" in Windows Explorer.
To see detailed info about files (permissions, owner, size, date, etc.):
-rw-r--r-- 1 user user 4096 Oct 25 10:15 notes.txt drwxr-xr-x 2 user user 4096 Oct 25 09:50 Documents
Let's decode it:
- -rw-r--r-- → file permissions
- 1 → number of links
- user user → owner and group
- 4096 → file size (bytes)
- Oct 25 10:15 → last modified date
- notes.txt → file name
It's like the "Details View" in Windows.
You can mix options for more info:
→ Shows hidden files + detailed info.
Sort by time (newest first):
Sort by size:
Add color for file types (usually default):
- ls / → lists files in the root directory
- ls ~/Documents → lists files inside the Documents folder
- ls *.txt → shows only .txt files
cat — Viewing the Entire File Content
The cat command (short for concatenate) is like opening a file to see all its content at once — but in the terminal instead of a text editor.
$ cat notes.txt Welcome to Linux class! Today we are learning basic commands.
This displays everything inside notes.txt right on your screen.
You can also show contents of more than one file:
👉 It prints the content of both files one after another. It's like merging them temporarily just for viewing.
You can even create a file with it!
Now type whatever you want inside the file. When you're done, press Ctrl + D (EOF) to save and exit.
💡 Analogy: Think of this like writing a quick sticky note inside your terminal.
To add text to an existing file, use:
Then type your new content and press Ctrl + D again.
✅ Now that line gets added to the end of the file.
Want to merge files into one?
This joins the content of both into a new file called combined.txt.
If you want to see line numbers:
1 Welcome to Linux class!
2 Today we are learning basic commands.
For small files, cat is perfect. But for big files, it scrolls too fast — that's when we use less (next topic 👀).
less — Viewing Large Files Interactively
When a file is too big to view with cat (because it scrolls off the screen instantly), we use less — it lets you scroll up and down, search, and navigate through the file easily. Think of it like opening a long PDF — you can scroll line by line instead of seeing everything at once.
Now you can:
- Scroll down → Press Spacebar or ↓ arrow
- Scroll up → Press b or ↑ arrow
- Exit → Press q
You can search for text while viewing!
- Press / then type your keyword (Example: /Linux)
- Press Enter → it jumps to the next match
- Press n → to find the next occurrence
- Press N → to go back to the previous match
💡 Like pressing Ctrl + F in a text editor!
- G → jump to end of file
- g → jump to start of file
- Spacebar → next page
- b → previous page
- Arrow keys → scroll line by line
You can also view the output of another command with less using a pipe (|).
That means: "List all files in detail and open them in a scrollable view."
📖 Perfect for when you have hundreds of files in a directory.
| Feature | cat | less |
|---|---|---|
| Shows full file at once | ✅ | ❌ |
| Scrollable | ❌ | ✅ |
| Searchable | ❌ | ✅ |
| Handles large files well | ❌ | ✅ |
So yeah, for long logs or big text files, less is your best buddy.
head — Viewing the First Few Lines
The head command is perfect when you only want to see the beginning of a file. Instead of scrolling through the entire file, you peek at the first few lines. Think of it like reading the introduction of a book before deciding to read the whole thing.
By default, head shows the first 10 lines of the file.
If you want a different number of lines, use -n:
✅ This shows only the first 5 lines.
You can use head with pipes to preview output:
This shows only the first 10 entries of a long directory listing. Handy when the folder has hundreds of files — saves scrolling time.
tail — Viewing the Last Few Lines
The tail command is like the opposite of head. Instead of peeking at the beginning of a file, you check the end — very useful for logs or files that are constantly updated. Think of it like reading the last few pages of a diary or report to see the latest updates.
By default, tail shows the last 10 lines of a file.
You can specify how many lines to see:
✅ Shows the last 5 lines of the file.
This is super powerful for monitoring logs:
-f means "follow" → keeps showing new lines as they're added.
Perfect for watching server logs or program output in real-time.
You can pipe output to tail:
Shows only the last 10 entries from a long directory listing.
Real-life analogies
ls = Opening your file manager/explorer to see what's in a folder
cat = Reading a small post-it note — you can see everything in one glance
less = Reading a digital e-book — you can flip pages, jump, or search any word easily
head = Skimming through a newspaper's headlines at the top to see what's important
tail = Monitoring a chat or social media feed — you just watch the latest messages, not all the old ones