Linux Bash Cheatsheet

Diagnose/statistieken

Processen, geheugengebruik, netwerkactiviteit en schijfactiviteit

# List tasks, htop shows slightly more info
top
htop
# Memory usage, the number below "free" on the "-/+ buffers/cache" line is the actual free space
free -m
# Show network activity, use "n"-key to toggle hostname resolving
iftop
# Disk activity per process
iotop
# Show number of cpu cores
nproc

Beveiliging

Fail2ban

# Validate if regexes work with given logfile
fail2ban-regex /path/to/logfile /etc/fail2ban/filter.d/filter-to-test.conf
# Show jail status
fail2ban-client status jailname

# Remove ip from jail, use latter command for fail2van >v0.8.8
fail2ban-client get jailname actionunban 1.2.3.4
fail2ban-client set jailname unbanip 1.2.3.4

IPTables firewall

# Remove all rules
iptables -F INPUT
iptables -F FORWARD
iptables -F OUTPUT

# Block given ip
iptables -A INPUT -s 1.2.3.4 -j DROP

E-mail

Simpele mail versturen

# Echo a specific body
echo "This is the message body" | mail -s "subject" piet@friet.pan

# Or use a file as body
cat somefile.txt | mail -s "subject" piet@friet.pan

Mail met bijlage

echo "This is the message body" | mutt -a "/path/to/file.to.attach" -s "subject" -- ad@friet.pan

Een .eml bestand doorsturen (bijvoorbeeld mail die in quarantaine staat)

swaks -f from@dom.tld -t to@dom.tld -d message.eml

Spamassassin

# Use -t "testing" the mail, this will output the e-mail adding a report at the bottom.
cat message.eml | spamassassin -t
# Use -D for "debugging" spamassassin, optionally pass a matching test, for example pyzor or bayes
cat message.eml | spamassassin -D pyzor 2>&1 | less
# Manually learn a message as spam or ham
cat "message.eml" | sa-learn --spam|--ham --username=vmail
# Remove address from whitelist manually
spamassassin --remove-addr-from-whitelist foo@bar.baz
# Feed a message to the spamc-process and return the results (--full-spam will only return if message is marked as spam)
cat message.eml | spamc --u rcpt@domain.tld --full|--full-spam

ClamAV

# Either use clamscan (reloads all database files) or use clamdscan (doesn't reload database files, thus faster) to scan a message or a path.
# Add "-i" to only show infected files
clamscan path/or/file
clamdscan path/or/file

Postfix

# Delete all deferred messages from mailq
postsuper -d ALL deferred

# Resend mails on queue (when you for example resolved a temporary error)
# If mails remain on the queue after this than there should be a reason for that!
postqueue -f
sendmail -q
# View contents of a queued message
# Either use mailq or postqueue -p to list message-ids
postcat -q MESSAGEID

Bestanden (bewerken, opzoeken)

Find

# Remove "-ls" to show what exactly matched
find . -type f | xargs grep "String in file" -ls
# When files contain special characters, spaces or quotes, you have to escape them
find . -type f -print0 | xargs -0 grep "String in file" -ls
# Find files and loop execute command on results
find . -name '*.txt' -exec echo {} \;

# If results contain spaces or other special chars, then pipe it trough 'read'
find . -name '*.txt' | while IFS= read -r FILE; do echo $FILE; done;

Grep

# Search in a continuous stream (for example with tail -f)
tail -f file | grep --line-buffered search-string

Comprimeren & extraheren (decomprimeren)

# Compressing files
tar -cvzf target.tar.gz file1 dir1 wildcard/*
zip target.zip file1 dir1 wildcard/*

# Add a progress bar to compressing a tar
tar cf - file-to-compress -P | pv -s $(du -sb file-to-compress | awk '{print $1}') | gzip > target.tar.gz
# Decompressing/extracting archives
# Remove -z to extract a simple tar-archive
tar -xvzf source.tar.gz
unzip source.zip

Afbeeldingen converteren met Imagick

# Merge multiple images into one (creates CSS-sprite)
# -extent is optional and can be used to make every image the same size
# Use +append to create horizontal tiles
convert image1.png image2.png image3.png -gravity NorthWest -extent 33x33 -append sprites.css

PDF

# Merge multiple pdf's into one single file
gs -dBATCH -dNOPAUSE -q -sDEVICE=pdfwrite -sOutputFile=merged.pdf in1.pdf in2.pdf in3.pdf
# Rotate single page or multiple pages
# Add 'north', 'east', 'south' or 'west' after eacht pagenumber you want to rotate.
pdftk A=source.pdf cat A1-3 4east 5south 6-10 output dest.pdf

GIT

Basis handelingen (status, stageing, committing & pushing)

git status
git add .
git commit
git push

Commit bericht aanpassen

git commit --ammend -m "This is the new message"

Standaard Workflow

git fetch -p
git checkout -b branch-name origin/master
git commit -a git push origin branch-name

Tags (maken, pushen en verwijderen)

# Create a new tag
git tag -a "v1.2.3." -m "Release message"

# Force new tags to remote origin
git push --follow-tags

# Delete tag
git push --delete origin "v1.2.3"

SVN

Tags (maken en verwijderen)

# Creating a tag in SVN is nothing more than copying sources to another folder
svn copy svn://path.to.repo/project/stable svn://path.to.repo/project/tags/0.1.2 -m "Release message"

# Deleing a tag is nothing more than deleting the folder
svn delete svn://path.to.repo/project/tags/0.1.2 -m "Remove release"

SVN Patches (maken, patchen en terugdraaien)

# Pipe the diff of two revisions to an output file
svn diff -r [n-1]:[n] > /tmp/patch_[n].diff

# Use that output file to patch svn (keep an eye on conflicts)
svn patch /tmp/patch_[n].diff

# And if patch went wrong, you can undo all changes
svn revert

Properties bewerken (e.g. bestanden "ignoren")

# List all properties, or a specific one
svn proplist
svn propget svn:ignore

# Open an editor to edit the properties (use when for example ignoring multiple files/folders)
svn propedit svn:ignore .

# Or set the property inline
svn propset svn:ignore config.php .

CURL

# Get the headers from a given request, this is very usefull when debugging 301 redirects (since it'll bypass any caches).
curl --verbose --head --location "http://www.yesysite.com/test.html"

Mysql

Mysql databases herstellen na crash

mysqlcheck --defaults-file=/etc/mysql/debian.cnf --check|--repair --all-databases

Mysql gebruikers beheren

# List all users
SELECT * FROM mysql.user;
SELECT host,user,password FROM mysql.user;

# Get current uer permisisons and password, this could be used for copying a user to another host for example
SHOW GRANTS FOR 'user'@'localhost';

# Create a new user with all privileges
GRANT ALL PRIVILEGES ON `database`.* TO 'user'@'localhost' IDENTIFIED BY 'plainpassword'
WITH GRANT OPTION;

# Change user password
SET PASSWORD FOR 'user'@'localhost' = "*WORKINGPASSWORDHASH"|PASSWORD("plainpassword");