DEV Community

Badal Raj
Badal Raj

Posted on

The Ultimate Guide to Linux and Shell Scripting: BEGINNER FRIENDLY

Table of Contents

  1. Introduction to Linux
  2. Linux File System Hierarchy
  3. Basic Linux Commands
  4. File Permissions and Ownership
  5. Text Processing Commands
  6. Process Management
  7. Introduction to Shell Scripting
  8. Variables and Input
  9. Conditional Statements
  10. Loops
  11. Functions
  12. Conclusion

1. Introduction to Linux

What is Linux?

Linux is an open-source, Unix-like operating system kernel first released by Linus Torvalds in 1991. Unlike Windows or macOS, Linux is highly customizable and powers:

  • Servers (90% of the internet runs on Linux)
  • Cloud computing (AWS, Google Cloud, Azure)
  • Embedded systems (Routers, Smart TVs, Android)
  • Supercomputers (All top 500 supercomputers run Linux)

Why Use Linux?

✔ Free and Open-Source (No licensing fees)

✔ Secure (Less prone to malware)

✔ Lightweight (Runs on old hardware)

✔ Highly Customizable (Choose your own desktop environment)

Popular Linux Distributions

Distro Best For
Ubuntu Beginners
Debian Stability
CentOS Servers
Arch Linux Advanced Users
Kali Linux Cybersecurity

2. Linux File System Hierarchy

Linux follows a standard directory structure:

Directory Purpose
/ Root directory
/bin Essential binaries (ls, cp)
/etc Configuration files
/home User directories
/var Variable data (logs)
/tmp Temporary files
/usr User programs

Example:

ls /  # List root directory
Enter fullscreen mode Exit fullscreen mode

3. Basic Linux Commands

Navigation

Command Description Example
pwd Print current directory pwd
cd Change directory cd /home/user
ls List files ls -l

File Operations

Command Description Example
touch Create file touch file.txt
cp Copy file cp file.txt backup/
mv Move/rename mv file.txt newname.txt
rm Delete file rm file.txt

Viewing Files

cat file.txt      # Display entire file
less file.txt    # Scroll through file
head -n 5 file.txt # Show first 5 lines
tail -f log.txt  # Follow log in real-time
Enter fullscreen mode Exit fullscreen mode

4. File Permissions and Ownership

Linux uses permissions to control file access:

-rw-r--r-- 1 user group 1024 Jan 1 10:00 file.txt
Enter fullscreen mode Exit fullscreen mode
  • Permissionsrw- (user), r-- (group), r-- (others)
  • Change permissions:

    chmod 755 script.sh  # rwxr-xr-x
    
  • Change ownership:

    chown user:group file.txt
    

5. Text Processing Commands

Command Description Example
grep Search text grep "error" log.txt
sed Find & replace sed 's/old/new/g' file.txt
awk Text processing awk '{print $1}' file.txt
sort Sort lines sort file.txt
uniq Remove duplicates uniq file.txt

6. Process Management

Command Description Example
ps List processes ps aux
top Live process monitor top
kill Terminate process kill -9 PID
bg / fg Background/Foreground jobs bg %1

7. Introduction to Shell Scripting

shell script is a program that automates tasks using shell commands.

Create Your First Script

#!/bin/bashecho "Hello, World!"
Enter fullscreen mode Exit fullscreen mode
  • Save as hello.sh
  • Make executable:CopyDownload

    bash

    chmod +x hello.sh
    
  • Run:CopyDownload

    bash

    ./hello.sh
    

8. Variables and Input

#!/bin/bashname="Linux"
echo "Welcome to $name!"

# User input
read -p "Enter your name: " username
echo "Hello, $username!"
Enter fullscreen mode Exit fullscreen mode

9. Conditional Statements

#!/bin/bashif [ $1 -gt 10 ]; then
  echo "Greater than 10"
else
  echo "10 or less"
fi
Enter fullscreen mode Exit fullscreen mode

10. Loops

For Loop

for i in {1..5}; do
  echo "Number: $i"
done
Enter fullscreen mode Exit fullscreen mode

While Loop

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

11. Functions

#!/bin/bashgreet() {
  echo "Hello, $1!"
}
greet "Alice"
Enter fullscreen mode Exit fullscreen mode

Conclusion
In conclusion, Linux and shell scripting offer powerful tools for both beginners and advanced users to efficiently manage and automate tasks. Linux's open-source nature, security, and flexibility make it a preferred choice for a wide range of applications, from personal computing to enterprise-level servers and supercomputers. Understanding the Linux file system hierarchy, mastering basic commands, and learning to manipulate file permissions are foundational skills that enhance productivity. Shell scripting further extends these capabilities by allowing users to automate repetitive tasks, manage processes, and handle complex operations with ease. By exploring variables, conditional statements, loops, and functions, users can create robust scripts that streamline workflows and improve system management. Embracing Linux and shell scripting not only empowers users to customize their computing environment but also opens up a world of possibilities in the realm of technology and innovation.

Top comments (0)