Shell Cheat Sheet

Posted : admin On 1/2/2022
  • SSH command cheat sheet, including 19 popular Secure Shell commands to manage your remote server. Learn how to manage and navigate files in the Linux terminal!
  • Reverse shell Cheat Sheet. GitHub Gist: instantly share code, notes, and snippets.

Explain shell commands



UNIX C Shell Cheat Sheet Setup.cshrc typically includes C shell speciļ¬c commands such as the set noclobber, set history, set savehist, and alias commands.

Redirect std and err output to /dev/null:

Redirect to stderr:

Use tee to redirect to a file and stdout:

stderr + /dev/tty0:

stderr + syslog

Log bash script output

Put this on top of your bash script:

Echo to stderr

Batch Rename Files

-n = dry run

rename -n 's/_bottom/_x/' *.png

rename -n 's/.htm$/.html/' *.htm

Add a prefix

rename -n 's/^/PRE_/' *

Add a suffix

sudo rename -n 's/$/.conf/' /etc/apache2/sites-available/*

Compare files in two directories

Not the content!

diff -qr dir1/ dir2/

Search for a string in files recusevly

grep -r 'word' .

Batch search and replace in files

sed -i 's/my_search/my_replace/g' *.html

Sed replace with regular expression

sed -E 's/ [email protected][a-zA-Z0-9]+$/ [email protected]/' /etc/ssh/

Sed delete line by regular expression

sed -i '/[email protected]$/d' /root/.ssh/authorized_keys


find . -name '*.php' xargs -n 1 sed -i -e 's Theme' . sfConfig::get('app_theme_package', 'NG') . 'Plugin Plugin g'

Find Symlinks

find ./ -type l -exec ls -l {} ;

find . -type l

Find multiple file types

Note the spaces after and before the braces

find . ( -name '*.php' -or -name '*.html' -or -name '*.css' -or -name '*.js' ) -exec ls -la {} ;

Find files older than x days

find /my/dir -type f -mtime +7 xargs ls -l

Extract columns/fields

  • du ./* -shx cut -c 1-3
    • Characters 1-3
  • du ./* -shx awk '{print $2}'
    • Second column separated by whitespace
  • echo 'hey whats up with you' awk '{for (i=3; i<NF; i++) printf $i ' '; print $NF}'
    • Get all remaining columns starting from column 3

Return Code / Exit Status

Shell Cheat Sheet Pdf

Print the return code of the last command. 0=success, >0=failure

echo $?

Test exit status

Run multiple commands even with errors, but 'log' occurence of error:

For longer/more complex shell scripts the following is recommended:

Return Code Condition


Multiline options with comments


  • A backslash ' at the end of the line allows to break a line into multiple lines. Make sure there is no whitspace (spaces,...) after the backslash!
  • Everything inside '`' backticks is executed. We use this trick for comments with '#'

Assign heredoc EOF to Variable

If clauses

If string contains...

Combine clauses

Repeat until it works

until ssh [email protected]; do echo 'Nope'; sleep 2; done

Test multiple script arguments

Bind Shell Cheat Sheet

Test if multiple variables are set


Search and replace in a file

Sed search and replace from stdin

  • echo -n 'foo' sed -e '/.*search/{r /dev/stdin' -e 'd;}' /my/file

or direct replace in file:

  • echo -n 'foo' sed -i -e '/.*search/{r /dev/stdin' -e 'd;}' /my/file

Create user by script

Find and iterate

Iterate lines in a variable

Shell cheat sheet pdf

Iterate Mysql result

Check if a mount point/disk is mounted

Echo tabs

  • echo -e 'Heytwhatstup'

Check if a website is running correctly

Can be used in cron like this, sends an email in case of error if you configure '[email protected]' option in crontab.
You need also SHELL=/bin/bash

Shell Script Cheat Sheet

if [[ `wget -q --no-check-certificate -O - grep 'Ok Text'` ' ]]; then echo ' website is running correcly!'; fi

Check if a remote service is running correctly

Can be used in cron like this, sends an email in case of error if you configure '[email protected]' option in crontab
You need also SHELL=/bin/bash

nc -z 1234; if [[ $? != 0 ]]; then echo 'Service 1234 is not running on!'; fi
Explanation: nc = netcat, '$?' checks the return code of the 'nc' command. If not '0' (not ok) perform notifying.

Logging to stderr and syslog

logger -s 'ERROR: no more haribos!'

Bash Scripting Cheat Sheet Pdf

Gernerate random string

cat /dev/urandom tr -dc 'a-zA-Z0-9' fold -w 6 head -n 1

Shell Cheat Sheet Pdf

Send email to root

Bash Cheat Sheet

echo -e 'huhunhaha' mail -s 'Test subj' root

Fish Shell Cheat Sheet

Get local ip

ip route get head -1 cut -d ' ' -f8

  • Created by Klemens Ullmann-Marx, 07/01/2009 09:58:39
  • Updated by Klemens Ullmann-Marx, 02/16/2021 10:51:09
  • Read access: Everyone, WikiAdmins, Logged in users Write access: WikiAdmins, Logged in users
  • Read counter: 18293 Edit counter: 71
  • Tags: bash