Paulund
2021-09-09

Linux

# SCP
scp -r /path/to/local/dir user@host:/path/to/remote/dir # Copy from local to remote
scp -r user@host:/path/to/remote/dir /path/to/local/dir # Copy from remote to local
cp -r /path/to/local/dir /path/to/local/another_dir # Copy from one folder to another

# Copy Files Over SFTP
sftp user@host && put /path/to/local/dir /path/to/remote/dir

# SSH
ssh user@host # Connect to a remote server
ssh -p 2222 user@host # Connect to a remote server on a different port
ssh-keygen -t rsa # Generate SSH Key
ssh-copy-id user@host # Copy SSH Key To Remote Server

# Find Files
find . -name "file.txt" # Find files by name
find . -type f -exec sed -i 's/old-text/new-text/g' {} + # Find and replace in files

# Networking
ipconfig # Get IP Address
curl ifconfig.me # Get IP Address
dig example.com # DNS Lookup
ping example.com # Ping A Server
netstat -tuln # List Open Ports

# File Commands
ls
ls -l # List Files With Details
ls -a # List All Files Including Hidden Files
ls -lh # List Files With Human Readable File Sizes
pwd # Print Working Directory
mkdir dir_name # Create A New Directory
rmdir dir_name # Remove A Directory
rm file.txt # Remove A File
rm -rf dir_name # Remove A Directory And All Its Contents
touch file.txt # Create A New File
cat file.txt # Display The Contents Of A File
du -sh /path/to/dir # Display The Amount Of Disk Space Used By A Directory
diff file1 file2 # Diff Two Files

# Process Management
ps # List Running Processes
ps aux # List All Running Processes
kill pid # Kill A Process
top # Display All Running Processes
bg # List stopped or background jobs
fg # Brings the most recent job to the foreground

# File Permissions
chmod 755 file.txt # Change File Permissions
chown user:group file.txt # Change File Owner
chown -R user:group /path/to/dir # Change File Owner Recursively

# Tar
tar -czvf archive.tar.gz /path/to/dir # Archive A Directory
tar -czvf archive.tar.gz /path/to/dir # GZIP A Directory
gzip -d archive.tar.gz # Unzip A GZIP File
gunzip archive.tar.gz # Unzip A GZIP File

# Searching
grep "literal_string" filename
grep "literal_string" filepattern
find /dir -name name* # Find files starting with name
locate filename # Find files by name

# Download
wget https://example.com/file.zip # Download Files
wget -c https://example.com/file.zip # Download Files With Progress Bar

# Package Management
sudo apt-get install package_name # Install A Package
sudo apt-get remove package_name # Remove A Package
sudo apt-get update # Update Package List
sudo apt-get upgrade # Upgrade All Packages

# Variables
NAME="John"
echo $NAME
echo "$NAME"
echo "${NAME}!"

# String Quotes
NAME="John"
echo "Hi $NAME"  #=> Hi John
echo 'Hi $NAME'  #=> Hi $NAME

# Functions
get_name() {
  echo "John"
}
echo "You are $(get_name)"