KORN SHELL PROGRAMMING CHEAT SHEET Special Characters Metacharacters have special meaning to the shell unless quoted (by preceding it with a or enclosing it in ` `) Inside double quotes “ “ parameter and command substitution occur and quotes characters `”$. Bash cheat sheet: Top 25 commands and creating custom commands. Which tell the shell how to behave as a user works at the command line or in scripts. Windows PowerShell Commands Cheat Sheet We break down what Windows PowerShell is, and provide you a definitive downloadable PowerShell Commands Cheat Sheet (PDF) as a quick reference to get you started and running your own commands. Tim Keary Network administration expert UPDATED: March 31, 2021. Reads and executes commands from a designated file in the current shell. Suspend: Suspends the execution of the shell until a SIGCONT signal is received. Test: Returns an exit status of 0 or 1 based on the specified condition. Times: Displays the accumulated user and system shell time.
Explain shell commands
Basics
Redirection
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/ root@[a-zA-Z0-9]+$/ root@foo/' /etc/ssh/ssh_host_rsa_key.pub
Sed delete line by regular expression
sed -i '/root@host.example.com$/d' /root/.ssh/authorized_keys
Recursive
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
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
Variables
Multiline options with comments
Explanation:
- 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 user@server.test; do echo 'Nope'; sleep 2; done
Test multiple script arguments
Test if multiple variables are set
Confirmation
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
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 'MAILTO=me@example.com' option in crontab.
You need also SHELL=/bin/bash
if [[ `wget -q --no-check-certificate -O - https://example.com:1234/xyz/ | grep 'Ok Text'` ' ]]; then echo 'example.com 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 'MAILTO=me@example.com' option in crontab
You need also SHELL=/bin/bash
nc -z example.com 1234; if [[ $? != 0 ]]; then echo 'Service 1234 is not running on example.com!'; 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!'
Gernerate random string
cat /dev/urandom | tr -dc 'a-zA-Z0-9' | fold -w 6 | head -n 1
Send email to root
echo -e 'huhunhaha' | mail -s 'Test subj' root
Get local ip
ip route get 1.1.1.1 | 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
Explain shell commands
Basics
Redirection
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/ root@[a-zA-Z0-9]+$/ root@foo/' /etc/ssh/ssh_host_rsa_key.pub
Sed delete line by regular expression
sed -i '/root@host.example.com$/d' /root/.ssh/authorized_keys
Recursive
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
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
Variables
Multiline options with comments
Explanation:
- 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 user@server.test; do echo 'Nope'; sleep 2; done
Test multiple script arguments
Shell Cheat Sheet Pentestmonkey
Test if multiple variables are set
Confirmation
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
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 'MAILTO=me@example.com' option in crontab.
You need also SHELL=/bin/bash
if [[ `wget -q --no-check-certificate -O - https://example.com:1234/xyz/ | grep 'Ok Text'` ' ]]; then echo 'example.com 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 'MAILTO=me@example.com' option in crontab
You need also SHELL=/bin/bash
nc -z example.com 1234; if [[ $? != 0 ]]; then echo 'Service 1234 is not running on example.com!'; 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!'
Gernerate random string
cat /dev/urandom | tr -dc 'a-zA-Z0-9' | fold -w 6 | head -n 1
Send email to root
echo -e 'huhunhaha' | mail -s 'Test subj' root
Linux Shell Cheat Sheet Pdf
Get local ip
ip route get 1.1.1.1 | head -1 | cut -d ' ' -f8
Mongo Shell Cheat Sheet
- 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: 18295 | Edit counter: 71
- Tags: bash