Progress: 0/62 (0%)

👁️ Listing and Viewing Files

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.

Basic usage
ls

This shows the list of files and folders in your current directory.

📌 Example:
Documents  Downloads  Pictures  Music

That's like opening your home folder and seeing all those directories.

Show hidden files (-a)

Hidden files in Linux start with a dot (.). To see them:

ls -a
📌 Example:
.  ..  .bashrc  Documents  Downloads
  • . → current directory
  • .. → parent directory
  • .bashrc → hidden file

👉 Think of it like turning on "Show hidden files" in Windows Explorer.

Long format (-l)

To see detailed info about files (permissions, owner, size, date, etc.):

ls -l
📌 Example output:
-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.

Combine options (-la)

You can mix options for more info:

ls -la

→ Shows hidden files + detailed info.

Sorting and formatting

Sort by time (newest first):

ls -lt

Sort by size:

ls -lS

Add color for file types (usually default):

ls --color
Useful tips
  • 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.

Basic usage
cat filename
📌 Example:
$ cat notes.txt
Welcome to Linux class!
Today we are learning basic commands.

This displays everything inside notes.txt right on your screen.

Viewing multiple files

You can also show contents of more than one file:

cat file1.txt file2.txt

👉 It prints the content of both files one after another. It's like merging them temporarily just for viewing.

Creating a new file with cat

You can even create a file with it!

cat > newfile.txt

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.

Appending to an existing file

To add text to an existing file, use:

cat >> existingfile.txt

Then type your new content and press Ctrl + D again.

✅ Now that line gets added to the end of the file.

Combining (Concatenating) files

Want to merge files into one?

cat file1.txt file2.txt > combined.txt

This joins the content of both into a new file called combined.txt.

Numbering lines (-n)

If you want to see line numbers:

cat -n notes.txt
📌 Output:
     1  Welcome to Linux class!
     2  Today we are learning basic commands.
Useful tip

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.

Basic usage
less filename

Now you can:

  • Scroll down → Press Spacebar or arrow
  • Scroll up → Press b or arrow
  • Exit → Press q
Searching inside a file

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!

Navigate quickly
  • G → jump to end of file
  • g → jump to start of file
  • Spacebar → next page
  • b → previous page
  • Arrow keys → scroll line by line
Combine with other commands (piping)

You can also view the output of another command with less using a pipe (|).

ls -l | less

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.

Why it's better than cat
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.

Basic usage
head filename

By default, head shows the first 10 lines of the file.

Change number of lines (-n)

If you want a different number of lines, use -n:

head -n 5 notes.txt

✅ This shows only the first 5 lines.

Combine with other commands

You can use head with pipes to preview output:

ls -l | head

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.

Basic usage
tail filename

By default, tail shows the last 10 lines of a file.

Change number of lines (-n)

You can specify how many lines to see:

tail -n 5 notes.txt

✅ Shows the last 5 lines of the file.

Follow a file in real-time (-f)

This is super powerful for monitoring logs:

tail -f /var/log/syslog

-f means "follow" → keeps showing new lines as they're added.

Perfect for watching server logs or program output in real-time.

Combine with other commands

You can pipe output to tail:

ls -l | 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