Posted on
Getting Started

Basic Bash Commands For Beginners Explained

Author
  • User
    Linux Bash
    Posts by this author
    Posts by this author

Here’s a list of basic Bash commands for beginners, organized by common use cases, with explanations and examples:


1. Navigation and Directory Management

  • pwd (Print Working Directory):
    Displays the current directory.

    pwd
    
  • ls (List):
    Lists files and directories in the current location.

    ls
    ls -l    # Detailed view
    ls -a    # Includes hidden files
    
  • cd (Change Directory):
    Changes the current directory.

    cd /path/to/directory
    cd ~    # Go to the home directory
    cd ..   # Go up one directory
    

2. File and Directory Operations

  • touch:
    Creates an empty file.

    touch filename.txt
    
  • mkdir (Make Directory):
    Creates a new directory.

    mkdir my_folder
    mkdir -p folder/subfolder   # Creates nested directories
    
  • cp (Copy):
    Copies files or directories.

    cp file.txt copy.txt
    cp -r folder/ copy_folder/   # Copy directories recursively
    
  • mv (Move/Rename):
    Moves or renames files or directories.

    mv oldname.txt newname.txt
    mv file.txt /path/to/destination
    
  • rm (Remove):
    Deletes files or directories.

    rm file.txt
    rm -r folder/    # Deletes directories recursively
    

3. Viewing and Editing Files

  • cat:
    Displays the contents of a file.

    cat file.txt
    
  • less:
    Views a file one page at a time.

    less file.txt
    
  • nano:
    Opens a file in a simple text editor.

    nano file.txt
    
  • head and tail:
    Displays the beginning or end of a file.

    head file.txt
    tail file.txt
    tail -n 20 file.txt    # Show last 20 lines
    

4. Searching and Filtering

  • grep:
    Searches for patterns within files.

    grep "text" file.txt
    grep -i "text" file.txt   # Case-insensitive search
    
  • find:
    Locates files and directories.

    find /path -name "filename.txt"
    

5. Managing Processes

  • ps:
    Displays running processes.

    ps
    ps aux   # Detailed view of all processes
    
  • kill:
    Terminates a process by its ID (PID).

    kill 12345   # Replace 12345 with the PID
    
  • top or htop:
    Monitors real-time system processes.

    top
    

6. Permissions and Ownership

  • chmod:
    Changes file permissions.

    chmod 644 file.txt    # Sets read/write for owner, read-only for others
    chmod +x script.sh    # Makes a script executable
    
  • chown:
    Changes file ownership.

    chown user:group file.txt
    

7. System Information

  • whoami:
    Displays the current username.

    whoami
    
  • uname:
    Provides system information.

    uname -a   # Detailed system info
    
  • df and du:
    Checks disk usage.

    df -h      # Shows free disk space
    du -sh     # Displays size of a directory or file
    

8. Networking

  • ping:
    Tests network connectivity.

    ping google.com
    
  • curl or wget:
    Fetches data from URLs.

    curl https://example.com
    wget https://example.com/file.txt
    

9. Archiving and Compression

  • tar:
    Archives and extracts files.

    tar -cvf archive.tar folder/     # Create archive
    tar -xvf archive.tar             # Extract archive
    tar -czvf archive.tar.gz folder/ # Compress with gzip
    
  • zip and unzip:
    Compresses or extracts files in ZIP format.

    zip archive.zip file.txt
    unzip archive.zip
    

10. Helpful Commands

  • man (Manual):
    Displays the manual for a command.

    man ls
    
  • history:
    Shows the history of commands entered.

    history
    
  • clear:
    Clears the terminal screen.

    clear
    

These commands provide a solid foundation for working in Bash. As you grow more comfortable, you can explore advanced topics like scripting and automation!