logo
eng-flag

Bash Cheatsheet

Table of Contents

  1. Basic Commands
  2. File Operations
  3. Directory Operations
  4. File Permissions
  5. System Information
  6. Process Management
  7. Network Commands
  8. Text Processing
  9. Bash Scripting
  10. Variables
  11. Conditionals
  12. Loops
  13. Functions
  14. Input/Output
  15. Job Control

Basic Commands

echo "Hello World"  # Print to screen
date               # Display current date and time
cal                # Display calendar
clear              # Clear terminal screen
exit               # Exit terminal
man <command>      # Display manual for command

File Operations

ls                 # List files and directories
ls -l              # List with detailed information
ls -a              # List including hidden files
cp file1 file2     # Copy file1 to file2
mv file1 file2     # Move or rename file1 to file2
rm file            # Remove file
rm -r dir          # Remove directory and contents
touch file         # Create empty file or update timestamp
cat file           # Display file contents
less file          # View file contents page by page
head file          # Display first 10 lines of file
tail file          # Display last 10 lines of file
grep pattern file  # Search for pattern in file

Directory Operations

pwd                # Print working directory
cd dir             # Change directory to dir
cd ..              # Move up one directory
cd ~               # Change to home directory
mkdir dir          # Create directory
rmdir dir          # Remove empty directory

File Permissions

chmod 755 file     # Change file permissions
chown user file    # Change file owner
chgrp group file   # Change file group

System Information

uname -a           # Display system information
df -h              # Show disk usage
free -h            # Show memory usage
top                # Display running processes
ps aux             # List all running processes

Process Management

ps                 # Display your currently active processes
kill pid           # Kill process with id pid
killall proc       # Kill all processes named proc
bg                 # Lists stopped or background jobs
fg                 # Brings the most recent job to foreground

Network Commands

ping host          # Ping host
wget url           # Download file from url
curl url           # Get contents of url
ssh user@host      # Connect to host as user
scp file user@host:path # Copy file to remote host

Text Processing

cut -d: -f1 /etc/passwd  # Cut out fields from file
sort file          # Sort lines in file
uniq file          # Remove duplicate lines from file
sed 's/foo/bar/g' file  # Replace foo with bar in file
awk '{print $1}' file  # Print first field of each line

Bash Scripting

#!/bin/bash        # Shebang line
# This is a comment
echo "Hello, World!"

Variables

NAME="John"        # Declare variable
echo $NAME         # Access variable
readonly NAME      # Make variable read-only
unset NAME         # Remove variable

Conditionals

if [ "$a" -eq "$b" ]; then
    echo "a is equal to b"
elif [ "$a" -gt "$b" ]; then
    echo "a is greater than b"
else
    echo "a is less than b"
fi

case "$variable" in
    "$condition1" ) command1;;
    "$condition2" ) command2;;
    * ) default command;;
esac

Loops

for i in 1 2 3 4 5
do
   echo "Welcome $i times"
done

while [ $counter -le 10 ]
do
    echo $counter
    ((counter++))
done

until [ $counter -gt 10 ]
do
    echo $counter
    ((counter++))
done

Functions

function_name() {
    echo "This is a function"
}

function_name    # Call the function

function func_with_params() {
    echo "First parameter: $1"
    echo "Second parameter: $2"
}

func_with_params param1 param2

Input/Output

echo "Enter your name:"
read name
echo "Hello, $name"

# Redirect stdout to file
echo "Hello" > output.txt

# Append stdout to file
echo "World" >> output.txt

# Redirect stderr to file
ls /non_existent_directory 2> error.log

# Redirect both stdout and stderr to file
ls /non_existent_directory &> output_and_error.log

Job Control

command &          # Run command in background
jobs               # List background jobs
fg %1              # Bring job 1 to foreground
bg %1              # Send job 1 to background
ctrl-z             # Suspend current job
ctrl-c             # Terminate current job

2024 © All rights reserved - buraxta.com