Skip to content

The Linux command line and Bash scripting are incredibly versatile tools that can help you automate tasks, manage systems, and become more efficient in your daily workflows. Hereโ€™s a comprehensive guide packed with commands and examples to supercharge your Linux and Bash skills! ๐Ÿš€

Notifications You must be signed in to change notification settings

shihabshahrier/Mastering-Linux-Command-Line-Bash-Scripting

Folders and files

NameName
Last commit message
Last commit date

Latest commit

ย 

History

2 Commits
ย 
ย 

Repository files navigation

๐Ÿง Mastering Linux Command Line & Bash Scripting

The Linux command line and Bash scripting are incredibly versatile tools that can help you automate tasks, manage systems, and become more efficient in your daily workflows. Hereโ€™s a comprehensive guide packed with commands and examples to supercharge your Linux and Bash skills! ๐Ÿš€


1. Essential Linux Commands ๐Ÿ› ๏ธ

Navigation

  • Change Directory:

    cd /path/to/directory

    Move to a specific directory.

  • List Files and Directories:

    ls -la

    List all files and directories, including hidden ones, with detailed information.

  • Show Current Directory:

    pwd

    Prints the current working directory.

  • Find Files:

    find /path -name "filename"

    Search for files or directories by name.


File Management

  • Create a File:

    touch newfile.txt

    Creates an empty file.

  • Create a Directory:

    mkdir new_directory

    Creates a new directory.

  • Copy Files and Directories:

    cp file.txt /destination/
    cp -r folder /destination/

    Copies files or directories.

  • Move or Rename Files:

    mv oldname.txt newname.txt
    mv file.txt /destination/

    Moves or renames files.

  • Delete Files and Directories:

    rm file.txt
    rm -r directory/

    Deletes files or directories.


Viewing Files

  • Display File Content:

    cat file.txt

    Prints the contents of a file.

  • Paginate Through Content:

    less file.txt

    View content one page at a time.

  • Show the First/Last Lines:

    head file.txt  # First 10 lines
    tail file.txt  # Last 10 lines

    Displays the first or last lines of a file.

  • View Line Numbers in a File:

    nl file.txt

    Shows line numbers with the content.


2. Bash Scripting Basics ๐Ÿ’ป

Writing Your First Script

  1. Create a new script file:

    nano script.sh
  2. Add the script content:

    #!/bin/bash
    echo "Hello, World!"
  3. Save and make it executable:

    chmod +x script.sh
  4. Run the script:

    ./script.sh

Variables, Conditions, and Loops

  • Using Variables:

    name="Linux"
    echo "Welcome to $name!"
  • Conditional Statements:

    if [ -f file.txt ]; then
        echo "File exists."
    else
        echo "File does not exist."
    fi
  • For Loop:

    for i in {1..5}; do
        echo "Iteration $i"
    done
  • While Loop:

    count=1
    while [ $count -le 5 ]; do
        echo "Count: $count"
        ((count++))
    done

3. Advanced Bash Techniques ๐Ÿš€

String Manipulation

  • Substring Extraction:

    text="Hello, Linux!"
    echo ${text:7:5}  # Output: Linux
  • Replace Text:

    text="I love cats"
    echo ${text/cats/dogs}  # Output: I love dogs

Automating Tasks

  • Backup a Directory:

    #!/bin/bash
    tar -czf backup-$(date +%Y-%m-%d).tar.gz /path/to/directory
    echo "Backup created!"
  • Find and Delete Old Files:

    find /path/to/files -type f -mtime +30 -exec rm {} \;

    Deletes files older than 30 days.


Debugging Scripts

  • Run with Debugging:
    bash -x script.sh
    Shows each command before it executes.

4. Process and System Management ๐Ÿง 

Process Management

  • View Running Processes:

    ps aux
  • Kill a Process:

    kill -9 <PID>
  • Monitor System Usage:

    top
    htop  # Enhanced interactive view

Disk and Memory Management

  • Check Disk Space:

    df -h
  • Check Free Memory:

    free -h

5. Networking Commands ๐ŸŒ

Basic Networking

  • Test Connectivity:

    ping google.com
  • Download a File:

    wget https://example.com/file.zip
  • Check Open Ports:

    netstat -tuln

6. Text Processing Power ๐Ÿ“„

Search with grep

  • Find a Keyword:

    grep "error" logfile.txt
  • Search Recursively:

    grep -r "TODO" .

Edit with sed

  • Replace Text Inline:

    sed 's/old/new/' file.txt
  • Delete Lines Containing a Pattern:

    sed '/pattern/d' file.txt

Extract with awk

  • Print Specific Columns:

    awk '{print $1, $3}' file.txt
  • Filter Rows:

    awk '$3 > 50' data.txt

7. Scheduling and Automation ๐Ÿ•’

Recurring Tasks with cron

  • Edit Cron Jobs:

    crontab -e
  • Run a Script Daily at Midnight:

    0 0 * * * /path/to/script.sh

One-Time Tasks with at

  • Schedule a Task:
    at 4:00 PM

8. Security Best Practices ๐Ÿ”’

  • Encrypt Files:

    gpg -c file.txt
  • Check File Integrity:

    sha256sum file.txt
  • Set File Permissions:

    chmod 600 file.txt

Next Steps โœจ

  1. Dive deeper into tools like awk, sed, and grep.
  2. Automate server setups with Bash and tools like Ansible.
  3. Explore system tuning using sysctl.
  4. Learn how to troubleshoot and debug complex Bash scripts.

๐Ÿง With these skills, youโ€™re ready to conquer Linux environments and automate like a pro! ๐Ÿš€

About

The Linux command line and Bash scripting are incredibly versatile tools that can help you automate tasks, manage systems, and become more efficient in your daily workflows. Hereโ€™s a comprehensive guide packed with commands and examples to supercharge your Linux and Bash skills! ๐Ÿš€

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published