How to learn basic Linux shell commands

How to Learn Basic Linux Shell Commands Table of Contents 1. [Introduction](#introduction) 2. [Prerequisites](#prerequisites) 3. [Understanding the Linux Shell](#understanding-the-linux-shell) 4. [Getting Started with Basic Commands](#getting-started-with-basic-commands) 5. [File and Directory Operations](#file-and-directory-operations) 6. [Text Processing and Viewing Commands](#text-processing-and-viewing-commands) 7. [System Information and Process Management](#system-information-and-process-management) 8. [File Permissions and Ownership](#file-permissions-and-ownership) 9. [Advanced Command Features](#advanced-command-features) 10. [Common Issues and Troubleshooting](#common-issues-and-troubleshooting) 11. [Best Practices and Professional Tips](#best-practices-and-professional-tips) 12. [Next Steps and Conclusion](#next-steps-and-conclusion) Introduction The Linux shell is a powerful command-line interface that serves as the gateway to the full potential of Linux and Unix-like operating systems. Whether you're a system administrator, developer, or enthusiastic user, mastering basic shell commands is essential for efficient system navigation, file management, and automation tasks. This comprehensive guide will take you from complete beginner to confident command-line user, covering fundamental concepts, essential commands, practical examples, and professional best practices. By the end of this article, you'll have a solid foundation in Linux shell commands and the confidence to explore more advanced topics. Prerequisites Before diving into Linux shell commands, ensure you have: System Requirements - Access to a Linux system (physical installation, virtual machine, or cloud instance) - Alternatively, Windows Subsystem for Linux (WSL) or macOS terminal - Basic computer literacy and familiarity with file systems Recommended Setup Options 1. Ubuntu Desktop/Server - Beginner-friendly with excellent documentation 2. CentOS/RHEL - Enterprise-focused distribution 3. Virtual Machine - VirtualBox or VMware with Linux distribution 4. Cloud Instance - AWS EC2, Google Cloud, or DigitalOcean droplet 5. Docker Container - Quick setup for practice environment Knowledge Prerequisites - Basic understanding of file systems and directories - Familiarity with text editors (nano, vim, or gedit) - Understanding of user accounts and basic security concepts Understanding the Linux Shell What is a Shell? The shell is a command-line interpreter that processes commands and returns output. It acts as an interface between users and the Linux kernel, translating human-readable commands into system calls. Common Shell Types Bash (Bourne Again Shell) ```bash Check your current shell echo $SHELL Output: /bin/bash ``` Zsh (Z Shell) Popular for its advanced features and customization options. Fish (Friendly Interactive Shell) Known for user-friendly features and syntax highlighting. Shell Prompt Anatomy Understanding the shell prompt structure: ```bash username@hostname:current_directory$ Example: john@ubuntu-server:/home/john$ ``` - `username`: Current logged-in user - `hostname`: Computer/server name - `current_directory`: Present working directory - `$`: Regular user prompt (`#` for root user) Getting Started with Basic Commands Navigation Commands pwd (Print Working Directory) Shows your current location in the file system: ```bash pwd Output: /home/username ``` ls (List Directory Contents) Display files and directories: ```bash Basic listing ls Detailed listing with permissions, size, and dates ls -l Show hidden files (starting with .) ls -a Combine options for detailed view including hidden files ls -la Human-readable file sizes ls -lh Sort by modification time (newest first) ls -lt ``` Example Output: ```bash $ ls -la total 32 drwxr-xr-x 4 john john 4096 Nov 15 10:30 . drwxr-xr-x 3 root root 4096 Nov 10 09:15 .. -rw-r--r-- 1 john john 220 Nov 10 09:15 .bash_logout -rw-r--r-- 1 john john 3526 Nov 10 09:15 .bashrc drwxr-xr-x 2 john john 4096 Nov 15 10:30 Documents -rw-r--r-- 1 john john 1024 Nov 15 10:25 example.txt ``` cd (Change Directory) Navigate between directories: ```bash Go to home directory cd cd ~ Go to specific directory cd /var/log Go up one directory level cd .. Go up two directory levels cd ../.. Go to previous directory cd - Navigate to subdirectory cd Documents/Projects ``` Getting Help man (Manual Pages) Access comprehensive documentation: ```bash View manual for ls command man ls Search for commands related to "copy" man -k copy Show brief description whatis ls ``` help and --help Quick help for built-in commands: ```bash For built-in commands help cd For external commands ls --help grep --help ``` File and Directory Operations Creating Files and Directories touch (Create Empty Files) ```bash Create single file touch newfile.txt Create multiple files touch file1.txt file2.txt file3.txt Create file with specific timestamp touch -t 202311151030 oldfile.txt ``` mkdir (Make Directory) ```bash Create single directory mkdir new_folder Create multiple directories mkdir folder1 folder2 folder3 Create nested directories mkdir -p projects/web/html Create directory with specific permissions mkdir -m 755 public_folder ``` Copying and Moving Files cp (Copy Files and Directories) ```bash Copy file to another location cp source.txt destination.txt Copy file to directory cp document.pdf ~/Documents/ Copy multiple files to directory cp *.txt ~/backup/ Copy directory recursively cp -r source_folder/ destination_folder/ Copy with preserved attributes cp -p original.txt copy_with_attributes.txt Interactive copy (ask before overwrite) cp -i important.txt backup.txt ``` mv (Move/Rename Files and Directories) ```bash Rename file mv oldname.txt newname.txt Move file to directory mv document.pdf ~/Documents/ Move multiple files mv *.log /var/log/archive/ Move directory mv old_folder/ new_location/ Interactive move mv -i source.txt destination.txt ``` Removing Files and Directories rm (Remove Files and Directories) ```bash Remove single file rm unwanted.txt Remove multiple files rm file1.txt file2.txt Remove files matching pattern rm *.tmp Remove directory and contents recursively rm -r old_folder/ Force removal without prompts rm -f stubborn_file.txt Interactive removal rm -i important.txt Remove empty directory rmdir empty_folder/ ``` Warning: The `rm` command permanently deletes files. There's no built-in "recycle bin" in Linux command line. Finding Files and Directories find (Search for Files and Directories) ```bash Find files by name find /home -name "*.txt" Find directories by name find /var -type d -name "log*" Find files by size find . -size +100M Find files modified in last 7 days find /home -mtime -7 Find and execute command on results find . -name "*.tmp" -exec rm {} \; Find files by permissions find /home -perm 755 ``` locate (Quick File Search) ```bash Update locate database sudo updatedb Find files quickly locate filename.txt Case-insensitive search locate -i FILENAME.TXT ``` Text Processing and Viewing Commands Viewing File Contents cat (Display File Contents) ```bash Display entire file cat filename.txt Display multiple files cat file1.txt file2.txt Number lines cat -n filename.txt Display with line endings visible cat -A filename.txt ``` less and more (Paginated File Viewing) ```bash View large files page by page less large_file.log more large_file.log Navigation in less: Space: Next page b: Previous page /search_term: Search forward ?search_term: Search backward q: Quit ``` head and tail (View File Beginnings and Endings) ```bash Show first 10 lines (default) head filename.txt Show first 20 lines head -n 20 filename.txt Show last 10 lines (default) tail filename.txt Show last 20 lines tail -n 20 filename.txt Follow file changes (useful for logs) tail -f /var/log/syslog ``` Text Processing grep (Search Text Patterns) ```bash Search for pattern in file grep "error" logfile.txt Case-insensitive search grep -i "ERROR" logfile.txt Search recursively in directories grep -r "TODO" /home/user/projects/ Show line numbers grep -n "function" script.py Count matching lines grep -c "warning" logfile.txt Invert match (show non-matching lines) grep -v "debug" logfile.txt Use regular expressions grep "^[0-9]" numbers.txt ``` sort (Sort Text Lines) ```bash Sort lines alphabetically sort names.txt Sort numerically sort -n numbers.txt Reverse sort sort -r names.txt Sort by specific field (space-separated) sort -k 2 data.txt Remove duplicates while sorting sort -u names.txt ``` uniq (Remove Duplicate Lines) ```bash Remove consecutive duplicates uniq data.txt Count occurrences uniq -c data.txt Show only duplicated lines uniq -d data.txt Show only unique lines uniq -u data.txt ``` wc (Word, Line, Character Count) ```bash Count lines, words, and characters wc filename.txt Count only lines wc -l filename.txt Count only words wc -w filename.txt Count only characters wc -c filename.txt ``` System Information and Process Management System Information Commands uname (System Information) ```bash Show all system information uname -a Show kernel name uname -s Show kernel release uname -r Show machine architecture uname -m ``` df (Disk Space Usage) ```bash Show disk usage for all filesystems df Human-readable format df -h Show specific filesystem df -h /home ``` du (Directory Space Usage) ```bash Show disk usage of current directory du Human-readable format du -h Show summary for directory du -sh /var/log Show usage for all subdirectories du -h --max-depth=1 ``` free (Memory Usage) ```bash Show memory usage free Human-readable format free -h Show in megabytes free -m ``` Process Management ps (Process Status) ```bash Show processes for current user ps Show all processes ps aux Show process tree ps -ef --forest Show processes for specific user ps -u username ``` top (Real-time Process Monitor) ```bash Interactive process monitor top Key commands in top: q: Quit k: Kill process r: Renice process 1: Show individual CPU cores ``` htop (Enhanced Process Monitor) ```bash More user-friendly process monitor htop (Install with: sudo apt install htop) ``` jobs, bg, fg (Job Control) ```bash Run command in background command & List active jobs jobs Bring job to foreground fg %1 Send job to background bg %1 Suspend current process Ctrl+Z ``` kill (Terminate Processes) ```bash Kill process by PID kill 1234 Force kill process kill -9 1234 Kill process by name killall firefox Kill all processes by user pkill -u username ``` File Permissions and Ownership Understanding Linux Permissions Linux uses a permission system with three types of access for three categories of users: Permission Types: - `r` (read): View file contents or list directory contents - `w` (write): Modify file contents or create/delete files in directory - `x` (execute): Run file as program or access directory User Categories: - Owner (u): File/directory owner - Group (g): Users in the file's group - Others (o): All other users Viewing Permissions ```bash Detailed listing showing permissions ls -l filename.txt Output: -rw-r--r-- 1 john users 1024 Nov 15 10:30 filename.txt ``` Permission String Breakdown: - First character: File type (`-` for file, `d` for directory, `l` for link) - Next 9 characters: Permissions in groups of 3 (owner, group, others) Changing Permissions chmod (Change Mode) ```bash Using symbolic notation chmod u+x script.sh # Add execute for owner chmod g-w filename.txt # Remove write for group chmod o+r document.pdf # Add read for others chmod a+x program # Add execute for all Using octal notation chmod 755 script.sh # rwxr-xr-x chmod 644 document.txt # rw-r--r-- chmod 600 private.key # rw------- Recursive permission change chmod -R 755 /var/www/html/ ``` Common Permission Combinations: - `755`: Standard for executable files and directories - `644`: Standard for regular files - `600`: Private files (owner only) - `777`: Full permissions for all (use cautiously) Changing Ownership chown (Change Owner) ```bash Change owner sudo chown newowner filename.txt Change owner and group sudo chown newowner:newgroup filename.txt Change ownership recursively sudo chown -R webuser:webgroup /var/www/ Change only group sudo chgrp newgroup filename.txt ``` Advanced Command Features Input/Output Redirection Output Redirection ```bash Redirect output to file (overwrite) ls -l > directory_listing.txt Append output to file echo "New line" >> logfile.txt Redirect error output command 2> error.log Redirect both output and errors command > output.txt 2>&1 Discard output command > /dev/null ``` Input Redirection ```bash Use file as input sort < unsorted.txt Here document cat << EOF This is a multi-line text block EOF ``` Pipes and Command Chaining Pipes (|) ```bash Chain commands together ls -l | grep "txt" ps aux | grep "firefox" cat logfile.txt | grep "error" | wc -l Complex pipeline example cat /var/log/access.log | grep "404" | awk '{print $1}' | sort | uniq -c | sort -nr ``` Command Chaining ```bash Run commands sequentially (regardless of success) command1 ; command2 ; command3 Run next command only if previous succeeds command1 && command2 && command3 Run next command only if previous fails command1 || command2 Combine conditional operators mkdir project && cd project && touch README.md ``` Wildcards and Pattern Matching Common Wildcards ```bash * matches any characters ls *.txt rm temp* ? matches single character ls file?.txt [] matches character range ls file[1-3].txt ls [A-Z]*.doc {} matches specific patterns cp file.{txt,bak} backup/ ``` Command Substitution ```bash Using backticks (older syntax) echo "Today is `date`" Using $() (preferred syntax) echo "Today is $(date)" current_dir=$(pwd) file_count=$(ls | wc -l) Practical example backup_file="backup_$(date +%Y%m%d).tar.gz" tar -czf $backup_file important_files/ ``` Environment Variables ```bash Display environment variables env printenv Display specific variable echo $HOME echo $PATH echo $USER Set temporary variable export MYVAR="Hello World" echo $MYVAR Add to PATH export PATH=$PATH:/usr/local/bin Make permanent by adding to ~/.bashrc echo 'export MYVAR="Hello World"' >> ~/.bashrc ``` Common Issues and Troubleshooting Permission Denied Errors Problem: `Permission denied` when trying to execute or access files. Solutions: ```bash Check file permissions ls -l filename Add execute permissions chmod +x filename Run with sudo for system files sudo command Check if you're in the right directory pwd ``` Command Not Found Problem: `command not found` error messages. Solutions: ```bash Check if command is installed which command_name whereis command_name Check PATH variable echo $PATH Install missing package (Ubuntu/Debian) sudo apt update && sudo apt install package_name Install missing package (CentOS/RHEL) sudo yum install package_name ``` File System Full Problem: `No space left on device` errors. Solutions: ```bash Check disk usage df -h Find large directories du -sh /* | sort -hr Clean temporary files sudo rm -rf /tmp/* sudo apt autoremove # Ubuntu/Debian sudo yum clean all # CentOS/RHEL Find and remove large log files find /var/log -name "*.log" -size +100M ``` Process Management Issues Problem: Unresponsive processes or high CPU usage. Solutions: ```bash Identify problematic processes top htop ps aux --sort=-%cpu Kill specific process kill -15 PID # Graceful termination kill -9 PID # Force kill Kill by process name killall process_name pkill -f "process pattern" ``` Network and Connectivity Problem: Network-related command issues. Solutions: ```bash Check network connectivity ping google.com ping -c 4 8.8.8.8 Check network configuration ip addr show ifconfig Check DNS resolution nslookup google.com dig google.com Check open ports netstat -tulpn ss -tulpn ``` Best Practices and Professional Tips Command Line Efficiency Use Tab Completion - Press `Tab` to auto-complete commands, filenames, and paths - Press `Tab` twice to see all available options Command History ```bash Search command history history | grep "pattern" Execute previous command !! Execute command from history by number !123 Search and execute from history Ctrl+R (then type search term) Clear history history -c ``` Useful Keyboard Shortcuts - `Ctrl+A`: Move cursor to beginning of line - `Ctrl+E`: Move cursor to end of line - `Ctrl+U`: Clear line before cursor - `Ctrl+K`: Clear line after cursor - `Ctrl+L`: Clear screen - `Ctrl+C`: Cancel current command - `Ctrl+Z`: Suspend current process Safety and Security Practices Always Verify Commands ```bash Use -i flag for interactive confirmation rm -i important_file.txt mv -i source.txt destination.txt cp -i original.txt backup.txt Test commands with echo first echo rm *.txt # See what would be deleted Then run: rm *.txt ``` Backup Important Data ```bash Create backups before major operations cp important.conf important.conf.backup tar -czf backup_$(date +%Y%m%d).tar.gz /important/directory/ ``` Use Appropriate Permissions ```bash Don't use 777 permissions unnecessarily chmod 755 script.sh # Good for executables chmod 644 data.txt # Good for data files chmod 600 private.key # Good for sensitive files ``` Script Writing Basics Simple Bash Script Example ```bash #!/bin/bash backup_script.sh Variables BACKUP_DIR="/backup" SOURCE_DIR="/home/user/documents" DATE=$(date +%Y%m%d_%H%M%S) BACKUP_FILE="backup_$DATE.tar.gz" Create backup echo "Creating backup..." tar -czf "$BACKUP_DIR/$BACKUP_FILE" "$SOURCE_DIR" if [ $? -eq 0 ]; then echo "Backup completed successfully: $BACKUP_FILE" else echo "Backup failed!" exit 1 fi ``` Make Script Executable ```bash chmod +x backup_script.sh ./backup_script.sh ``` Monitoring and Logging Monitor System Resources ```bash Continuous monitoring watch -n 2 "df -h" watch -n 1 "free -h" Log command output command > logfile.txt 2>&1 command | tee logfile.txt # Display and log simultaneously ``` Useful Aliases Add to `~/.bashrc` for permanent aliases: ```bash Navigation aliases alias ll='ls -la' alias la='ls -A' alias l='ls -CF' Safety aliases alias rm='rm -i' alias mv='mv -i' alias cp='cp -i' Utility aliases alias grep='grep --color=auto' alias h='history' alias c='clear' System information alias df='df -h' alias du='du -h' alias free='free -h' ``` Documentation and Learning Keep Learning Resources Handy ```bash Quick reference man bash info coreutils Online resources bookmarks - Linux Command Library - ExplainShell.com - TLDR pages: tldr command_name ``` Practice Regularly - Set up a test environment for experimentation - Practice with different file types and sizes - Create automation scripts for routine tasks - Join Linux communities and forums Next Steps and Conclusion Advanced Topics to Explore After mastering these basic commands, consider exploring: 1. Advanced Text Processing - `sed` (stream editor) - `awk` (pattern scanning and processing) - Regular expressions 2. System Administration - Service management (`systemctl`, `service`) - Package management (`apt`, `yum`, `dnf`) - User management (`useradd`, `usermod`, `passwd`) 3. Networking Commands - `ssh`, `scp`, `rsync` - `curl`, `wget` - `iptables`, `netstat` 4. Advanced Scripting - Bash scripting with functions and loops - Error handling and logging - Automation and scheduling (`cron`) 5. Version Control - Git command-line usage - Repository management Building Your Skills Practice Projects 1. File Organization Script: Create a script to organize files by type 2. System Monitor: Build a monitoring script for system resources 3. Backup Automation: Develop automated backup solutions 4. Log Analysis: Create tools to analyze log files 5. Development Environment: Set up development tools via command line Certification Paths - Linux Professional Institute Certification (LPIC) - Red Hat Certified System Administrator (RHCSA) - CompTIA Linux+ Conclusion Learning Linux shell commands is a journey that opens doors to powerful system administration, development, and automation capabilities. The commands covered in this guide provide a solid foundation for working efficiently in Linux environments. Remember these key principles as you continue your Linux journey: 1. Practice Regularly: Consistent hands-on practice is essential for retaining command knowledge 2. Read Documentation: Use `man` pages and help options to understand command capabilities fully 3. Start Simple: Begin with basic operations and gradually tackle more complex tasks 4. Safety First: Always verify commands, especially those that modify or delete data 5. Automate Wisely: Use scripts to automate repetitive tasks, but understand what they do The command line might seem intimidating at first, but with patience and practice, it becomes an incredibly powerful and efficient tool. Each command you master adds to your capability to work with Linux systems effectively, whether for personal projects, professional development, or system administration tasks. Continue exploring, experimenting, and building upon these fundamentals. The Linux command line offers virtually unlimited possibilities for system interaction, automation, and problem-solving. Your investment in learning these skills will pay dividends throughout your technical career.