Paulund

Bash Cheatsheet

Copy Files Over SSH

# Copy from local to remote
scp -r /path/to/local/dir user@host:/path/to/remote/dir

# Copy from remote to local
scp -r user@host:/path/to/remote/dir /path/to/local/dir

Copy Files Over SFTP

sftp user@host
put /path/to/local/dir /path/to/remote/dir

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

Get IP Address

curl ifconfig.me

List Contents Of Directory

ls -l

Display The Amount Of Disk Space Used By A Directory

du -sh /path/to/dir

Diff Two Files

diff file1 file2

Create A New File

touch file.txt

Tar

tar -czvf archive.tar.gz /path/to/dir # Archive A Directory
tar -czvf archive.tar.gz /path/to/dir # GZIP A Directory

Search in a file

grep "literal_string" filename
grep "literal_string" filepattern

DNS Lookup

dig example.com

Download

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

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)"