Published on

Basic Terminal Commands: ls, cd, and clear

Authors
  • avatar
    Name
    Hieu Cao
    Twitter

Introduction

The terminal is a powerful tool for developers and system administrators, allowing precise control over the operating system. Among the essential commands for navigation and organization are ls, cd, and clear. These commands help you list files, navigate directories, and maintain a clean workspace in the terminal.

This guide will introduce you to these basic commands, their options, and practical use cases to get you started.

The ls Command: List Directory Contents

The ls command displays the contents of a directory. It is one of the most commonly used commands to check files and directories.

Syntax

ls [options] [directory]

Common Usage

  1. List Files in Current Directory

    ls
    
  2. Detailed List with File Permissions

    ls -l
    
    • Displays file details like permissions, ownership, size, and modification date.
  3. Include Hidden Files

    ls -a
    
    • Lists files and directories, including hidden ones (starting with a dot .).
  4. Combine Options

    ls -la
    
    • Shows a detailed list, including hidden files.

Example

ls -lh
  • -h: Displays file sizes in a human-readable format.

The cd Command: Change Directory

The cd command is used to navigate between directories in the terminal.

Syntax

cd [directory]

Common Usage

  1. Navigate to a Directory

    cd Documents
    
    • Moves to the Documents directory.
  2. Go Back to Parent Directory

    cd ..
    
    • Moves up one directory level.
  3. Go to the Home Directory

    cd ~
    
  4. Navigate to Root Directory

    cd /
    
    • Moves to the root directory.
  5. Navigate Using Absolute Path

    cd /usr/local/bin
    
  6. Navigate Using Relative Path

    cd ../Downloads
    
    • Moves to the Downloads directory from the current directory.

The clear Command: Clean Your Terminal

The clear command is a simple but useful tool to clean up your terminal screen.

Syntax

clear

Usage

  1. Clear the Terminal

    clear
    
    • Removes all previous output and gives you a clean workspace.
  2. Shortcut for Clearing On most terminals, you can also use the keyboard shortcut Ctrl + L to achieve the same result.


Practical Example

Let’s combine these commands to demonstrate a typical terminal workflow:

  1. List Files in the Current Directory
    ls
    
  2. Navigate to a Specific Directory
    cd Projects
    
  3. Check Contents of the New Directory
    ls -l
    
  4. Clear the Terminal
    clear
    

Conclusion

Mastering the ls, cd, and clear commands is the first step in becoming proficient with the terminal. These commands are foundational and will help you navigate and manage your files more effectively. Practice using these commands in your daily workflow, and soon, they’ll become second nature.