Chapter 3: Exploring System Commands

In this chapter, we’ll cover tools and commands to explore files and directories in detail. You’ll learn how to view file contents, determine file types, and navigate through large files efficiently.


Why Explore The System?

Linux is a treasure trove of files and directories, and knowing how to explore them is a fundamental skill. Whether you’re looking for a specific configuration file, inspecting a log file, or just curious about what’s inside a directory, the commands in this chapter will be your go-to tools.


Key Commands For Exploration

Let’s start with the essential commands for exploring the system.


1. Ls: Listing Files And Directories

We’ve already used ls in the previous chapter, but it’s worth revisiting because it’s so powerful. By default, ls lists the contents of a directory, but it has many options to customize its output.

Basic Usage
$ ls Documents Downloads Music Pictures

Show Hidden Files: Hidden files in Linux start with a dot (.). Use the -a option to see them. 
$ ls -a . .. .bashrc Documents Downloads Music Pictures

Long Listing Format: Use the -l option to see detailed information about files and directories, including permissions, ownership, size, and modification date. 
$ ls -l drwxr-xr-x 2 user user 4096 Oct 1 10:00 Documents drwxr-xr-x 2 user user 4096 Oct 1 10:00 Downloads

Combine Options: You can combine options like -a and -l for a detailed view of all files, including hidden ones. 
$ ls -la


2. File: Determining File Types

The file command tells you what type of file you’re dealing with. This is especially useful when you’re unsure whether a file is a text file, a binary executable, or something else.

Basic Usage
$ file example.txt example.txt: ASCII text

Example with a Binary File
$ file /bin/ls /bin/ls: ELF 64-bit LSB pie executable, x86-64, version 1 (SYSV), dynamically linked, interpreter /lib64/ld-linux-x86-64.so.2, BuildID[sha1]=..., for GNU/Linux 3.2.0, stripped


3. Less: Viewing File Contents

The less command is a pager that lets you view the contents of a file one screen at a time. It’s perfect for reading large files without overwhelming your terminal.

Basic Usage
$ less example.txt

Navigating in less:

  • Use the arrow keys to scroll up and down.
  • Press Space to move forward one page.
  • Press b to move backward one page.
  • Type /search_term to search for a specific word (e.g., /error).
  • Press q to quit.

4. Cat: Concatenating And Displaying Files

The cat command is used to display the entire contents of a file. It’s simple but powerful.

Basic Usage
$ cat example.txt Hello, Linux! This is a sample text file.

Concatenating Files: You can also use cat to combine multiple files. 
$ cat file1.txt file2.txt > combined.txt

Bat: A Modern Alternative to Cat

The bat command is an enhanced version of cat, offering syntax highlighting and additional features that make viewing files more user-friendly.

Basic Usage:

$ bat example.txt
───────┬────────────────────────────
       │ File: example.txt
───────┼────────────────────────────
   1   │ Hello, Linux! 
   2   │ This is a sample text file.
───────┴────────────────────────────

Key Features: - Syntax Highlighting: Automatically highlights code based on file extension - Git Integration: Shows git modifications in the gutter - Line Numbers: Displays line numbers by default - File Headers: Shows clear file boundaries with headers - Theme Support: Comes with multiple color themes

Advanced Usage:

# Show without line numbers
$ bat --style=plain example.txt

# Show with Git changes
$ bat --style=changes script.py

# Use as a pager
$ man 5 passwd | bat

Like cat, bat can also concatenate multiple files:

$ bat file1.txt file2.txt

The bat command is particularly useful when viewing source code or configuration files, as its syntax highlighting makes the content more readable than plain text output from cat. }


5. Head And Tail: Viewing The Beginning Or End Of A File

Sometimes, you only need to see the beginning or end of a file. That’s where head and tail come in.

head: Displays the first 10 lines of a file by default. 
$ head example.txt

tail: Displays the last 10 lines of a file by default. 
$ tail example.txt

Customizing the Number of Lines: Use the -n option to specify how many lines to display. 
$ head -n 5 example.txt $ tail -n 5 example.txt


6. Find: Searching For Files

The find command is a powerful tool for searching files and directories based on various criteria like name, size, or modification date.

Basic Usage
$ find /home/user -name "*.txt" 
This command searches for all .txt files in the /home/user directory.

Search by Size
$ find /home/user -size +1M 
This command finds files larger than 1 MB.


7. Grep: Searching Inside Files

The grep command is used to search for specific text within files. It’s incredibly useful for finding patterns or keywords.

Basic Usage
$ grep "error" logfile.txt 
This command searches for the word “error” in logfile.txt.

Case-Insensitive Search
$ grep -i "error" logfile.txt

Recursive Search
$ grep -r "error" /home/user 
This command searches for “error” in all files under /home/user.


Extra: More System Exploration Tools

Modern tools like dust, duf, ncdu, fd, and ripgrep offer enhanced ways to explore and manage system resources. dust and duf provide colorized summaries of disk usage and mounted filesystems, respectively, making it easier to identify where space is being used. 

ncdu offers an interactive interface for drilling down into directories and deleting files on the fly. For file searching, fd is a faster and more user-friendly alternative to find, while ripgrep (rg) excels at quickly searching text within files. 

By combining these tools, you can efficiently explore your system, track down large directories, and search for specific text patterns, making system management more intuitive for both beginners and experienced users.

# Install dust
sudo apt install dust

# Install duf
$ sudo apt install duf

# Install ncdu
$ sudo apt install ncdu

# Install fd
$ sudo apt install fd-find

# Install ripgrep
$ sudo apt install ripgrep

# Example usage:
$ dust
$ duf
$ ncdu
$ fd "*.txt"
$ rg "search_term"

Visualize disk usage with dust or duf:

$ dust
$ duf

Analyze disk usage interactively with ncdu:

$ ncdu

Search files faster with fd:

$ fd "*.txt"

Replace grep with ripgrep for faster text searches:

$ rg "search_term"

Practice Time!

Let’s put your new skills to the test: 
1. Use ls -la to list all files in your home directory, including hidden ones. 
2. Use file to check the type of a few files. 
3. Use less to view the contents of a large file (e.g., /var/log/syslog). 
4. Use grep to search for a specific word in a text file.


That’s it for Chapter 3! You’ve now learned how to explore your Linux system in detail. In the next chapter, we’ll dive into manipulating files—creating, copying, moving, and deleting them. Until then, practice exploring your system and getting comfortable with these commands.


Prev: Chapter 2 | Next: Chapter 4