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.

What are Linux System Commands?

The Linux system commands are essential for exploring files and directories. Whether you’re locating configuration files, checking logs, or browsing directories, these commands make file exploration efficient. This guide introduces beginner-friendly tools to navigate and manage Linux systems via the terminal. (See Chapter 2 for navigation basics.)

Key Linux System Commands for File Exploration

Let’s explore core commands for file exploration in the Linux terminal.

1. ls: Listing Files and Directories

The ls command, introduced in Chapter 2, lists directory contents with versatile options.

  • Basic Usage:
    $ ls
    Output: Documents Downloads Music Pictures
  • Show Hidden Files (start with .):
    $ ls -a
    Output: . .. .bashrc Documents Downloads Music Pictures
  • Long Listing (permissions, size, date):
    $ ls -l
    Output: drwxr-xr-x 2 user user 4096 Oct 1 10:00 Documents
  • Combine Options:
    $ ls -la (detailed view with hidden files)

2. file: Determining File Types

The file command identifies file types, crucial for file exploration.

  • Basic Usage:
    $ file example.txt
    Output: example.txt: ASCII text
  • Binary File:
    $ file /bin/ls
    Output: /bin/ls: ELF 64-bit LSB pie executable...

3. less: Viewing File Contents

The less command displays large files one screen at a time, ideal for logs or configs.

  • Basic Usage:
    $ less example.txt
  • Navigation: Arrow keys to scroll, Space for next page, b for previous, /search_term to search (e.g., /error), q to quit.

4. cat and bat: Displaying File Contents

The cat command displays file contents quickly, while bat enhances it with syntax highlighting.

  • cat Basic Usage:
    $ cat example.txt
    Output: Hello, Linux! This is a sample text file.
  • Concatenate Files:
    $ cat file1.txt file2.txt > combined.txt
  • bat Basic Usage:
    $ bat example.txt
    Output:

    ───────┬────────────────────────────
           │ File: example.txt
    ───────┼────────────────────────────
       1   │ Hello, Linux!
       2   │ This is a sample text file.
    ───────┴────────────────────────────
  • bat Advanced Usage:
    $ bat --style=plain example.txt (no line numbers)
    $ bat --style=changes script.py (Git changes)
    $ man 5 passwd | bat (as pager)

5. head and tail: Viewing File Segments

head and tail show the start or end of files, perfect for quick checks.

  • head (first 10 lines):
    $ head example.txt
  • tail (last 10 lines):
    $ tail example.txt
  • Custom Lines:
    $ head -n 5 example.txt
    $ tail -n 5 example.txt

6. find: Searching for Files

The find command searches files by name, size, or other criteria.

  • Basic Usage:
    $ find /home/user -name "*.txt"
    Output: /home/user/example.txt
  • Search by Size:
    $ find /home/user -size +1M
    Output: /home/user/largefile.zip

7. grep: Searching Inside Files

The grep command, a key part of Linux system commands, searches text within files.

  • Basic Usage:
    $ grep "error" logfile.txt
    Output: 2025-06-04 16:48: error: connection failed
  • Case-Insensitive:
    $ grep -i "error" logfile.txt
  • Recursive Search:
    $ grep -r "error" /home/user
    Output: /home/user/logfile.txt:2025-06-04 16:48: error: connection failed

Modern Tools for Enhanced File Exploration

Modern terminal tools enhance file exploration, building on concepts from Chapter 2 (e.g., dust extends exa’s directory listing, fd and ripgrep improve find and grep):

  • dust and duf: Colorized disk usage summaries.
  • ncdu: Interactive disk usage analyzer.
  • fd: Faster alternative to find.
  • ripgrep (rg): Speedy alternative to grep.

Note: dust may not be available in stock Debian 12 repositories. Enable contrib or non-free repositories or install from source. See the dust GitHub page.

Install on Debian 12:

$ sudo apt install dust
$ sudo apt install duf
$ sudo apt install ncdu
$ sudo apt install fd-find
$ sudo apt install ripgrep    

Example commands and outputs:

  • dust:
    $ dust /home/user
    Output: 1.2G /home/user/Downloads 500M /home/user/Documents
  • duf:
    $ duf
    Output: Filesystem Size Used Avail Use% Mounted on /dev/sda1 100G 60G 40G 60% /
  • ncdu:
    $ ncdu /home/user
    Output: ncdu 1.18 ~ /home/user 1.2G [##########] Downloads 500M [#### ] Documents
  • fd:
    $ fd "*.txt"
    Output: example.txt notes.txt
  • ripgrep:
    $ rg "error" /home/user
    Output: logfile.txt:10:error: connection failed

Practice Exploring the System

Test your skills with these exercises:

  1. $ ls -la: List all files, including hidden ones.
  2. $ file example.txt: Check a file’s type.
  3. $ less /var/log/syslog: View a large file.
  4. $ grep "error" logfile.txt: Search for a word.
  5. Install and try bat or fd.

Pages: 1 2 3