Cloud & DevOpsLinuxUseful Stuff

Basic Bash Commands Explained for Beginners (with Examples)

Introduction

Bash (Bourne Again SHell) is one of the most popular command-line interpreters used in Unix-like systems such as Linux and macOS. It allows users to interact directly with the operating system by typing text commands instead of using graphical interfaces. Understanding basic Bash commands is essential for anyone learning Linux, managing servers, or developing IoT and embedded systems.

In this guide, we’ll cover the most important Bash commands for beginners — including listing files, changing directories, copying, moving, deleting, creating files and folders, and more. Each command is explained with examples and usage tips to help you get hands-on quickly.

What Is Bash?

Bash stands for Bourne Again Shell, an upgraded version of the original Unix shell (sh). It serves as a command processor that executes user commands and scripts. Bash is the default shell for many Linux distributions and is also available on macOS and Windows (via WSL — Windows Subsystem for Linux).

Bash enables:

  • File management
  • System administration
  • Script automation
  • Environment customization

1. Bash ls – List Directory Contents

The ls command lists files and directories in the current directory.

Syntax:

ls [options] [directory]

Common Examples:

  • List all files in the current directory:
    ls
    
  • List with details (permissions, owner, size, timestamp):
    ls -l
    
  • List hidden files:
    ls -a
    
  • List files sorted by modification time:
    ls -lt
    

Tip:

Use ls -lh to view file sizes in a human-readable format (KB, MB).

2. Bash cd – Change Directory

The cd (change directory) command allows you to navigate between directories in the filesystem.

Syntax:

cd [directory]

Examples:

  • Go to a directory named “Documents”:
    cd Documents
    
  • Go up one directory:
    cd ..
    
  • Go to your home directory:
    cd ~
    
  • Go to root directory:
    cd /
    

Tip:

You can use cd - to switch to the previous directory.

3. Bash pwd – Print Working Directory

The pwd command shows the full path of your current working directory.

Syntax:

pwd

Example:

/home/harshvardhan/projects

Use Case:

Helpful when working in nested directories to confirm your current location in the filesystem.

4. Bash echo – Display Text or Variables

The echo command prints text or variable values to the terminal.

Syntax:

echo [string]

Examples:

  • Print a simple message:
    echo "Hello, World!"
    
  • Display a variable value:
    name="Harsh"
    echo "My name is $name"
    
  • Redirect output to a file:
    echo "IoT by HVM Smart Solutions" > info.txt

5. Bash cat – Concatenate and View Files

The cat command displays file content, merges files, or creates new files.

Syntax:

cat [options] [file]

Examples:

  • View file content:
    cat file.txt
    
  • Combine two files:
    cat file1.txt file2.txt > combined.txt
    
  • Create a new file:
    cat > newfile.txt
    

    (Type your content and press Ctrl + D to save.)

6. Bash cp – Copy Files and Directories

The cp command copies files or directories.

Syntax:

cp [options] source destination

Examples:

  • Copy a file:
    cp file.txt backup.txt
    
  • Copy a directory recursively:
    cp -r folder1/ folder2/
    

Tip:

Use -i to get a confirmation prompt before overwriting files.

7. Bash mv – Move or Rename Files

The mv command moves or renames files and directories.

Syntax:

mv [options] source destination

Examples:

  • Rename a file:
    mv oldname.txt newname.txt
    
  • Move a file to another directory:
    mv file.txt /home/user/Documents/
    

Tip:

Like cp, use -i to prevent accidental overwrites.

8. Bash rm – Remove Files or Directories

The rm command deletes files or directories permanently.

Syntax:

rm [options] file

Examples:

  • Remove a single file:
    rm file.txt
    
  • Remove a directory and its contents:
    rm -r foldername/
    
  • Prompt before deletion:
    rm -i important.txt
    

⚠️ Warning: Use rm -rf carefully — it removes everything recursively without confirmation.

9. Bash touch – Create Empty Files or Update Timestamps

The touch command creates new empty files or updates the access/modification time of existing files.

Syntax:

touch [filename]

Examples:

  • Create a new empty file:
    touch newfile.txt
    
  • Update timestamp of an existing file:
    touch existing.txt

10. Bash mkdir – Make Directories

The mkdir command creates new directories.

Syntax:

mkdir [options] directory_name

Examples:

  • Create a directory:
    mkdir projects
    
  • Create nested directories:
    mkdir -p folder1/folder2/folder3
    

Tip:

Use -v for verbose output to confirm creation.

11. Bash man – Access Manual Pages

The man command displays the manual pages of any Linux command, providing syntax, descriptions, and examples.

Syntax:

man [command]

Example:

man ls

Use spacebar to scroll and q to quit the manual.

Tip:

If man pages aren’t installed, you can also try:

<command> --help

12. Bash alias – Create Command Shortcuts

The alias command lets you create shortcuts for long or frequently used commands.

Syntax:

alias name='command'

Examples:

  • Create a shortcut for listing all files:
    alias ll='ls -l'
    
  • Remove an alias:
    unalias ll
    
  • View all current aliases:
    alias
    

Tip:

To make an alias permanent, add it to your ~/.bashrc or ~/.bash_profile file.

Bonus: Combine Commands with && and ;

You can chain multiple commands in a single line.

Examples:

  • Run commands sequentially:
    mkdir test; cd test; touch example.txt
    
  • Run next command only if previous succeeds:
    cd test && echo "Success!"

Explore:

Conclusion

Mastering these basic Bash commands forms the foundation for working efficiently on Linux and Unix systems. Whether you’re an IoT developer configuring devices, a system admin managing servers, or a student learning programming, understanding how to navigate and manipulate files using Bash will save you time and enhance your productivity.

Start with simple commands like ls, cd, and pwd, then move on to file operations (cp, mv, rm) and system utilities (man, alias). With consistent practice, Bash will become one of your most powerful tools for automation and control.

Harshvardhan Mishra

Hi, I'm Harshvardhan Mishra. I am a tech blogger and an IoT Enthusiast. I am eager to learn and explore tech related stuff! also, I wanted to deliver you the same as much as the simpler way with more informative content. I generally appreciate learning by doing, rather than only learning. Thank you for reading my blog! Happy learning! Follow and send tweets me on @harshvardhanrvm. If you want to help support me on my journey, consider sharing my articles.

Leave a Reply

Your email address will not be published. Required fields are marked *