File me Issues or star this repo.
See more CheatSheets from Denny: #denny-cheatsheets
Name | Comment |
Redirect stdout/stderr | ls /tmp >/dev/null 2>&1 |
Deal with filename | basename $f , dirname $f |
Use timeout: avoid command hang | timeout 10 sh -c ‘ls -lt’ |
Restart shell without killing terminal | exec -l $SHELL |
Run sub-shell | echo $BASH_SUBSHELL; ( echo "Running in subshell: $BASH_SUBSHELL" ) |
Run static code check | link: shellcheck |
Name | Comment |
Show file content | cat /etc/hosts |
Show file content with line numbers | cat -n /etc/hosts |
Show with line numbers, while excluding blank lines | cat -b /etc/hosts |
Show the first 3 lines | head -n3 /etc/hosts |
Show the last 3 lines | tail -n3 /etc/hosts |
Keep tailing log files | tail -f /var/log/system.log , taif /var/log/system.log |
Show the 4th line | sed -n '4p' /etc/hosts |
Show 4th, 5th, 7th and 8th lines | sed -n '4,5p;7,8p' /etc/hosts |
Show matched string with 3 lines before and after | grep -C 3 "127.0.0.1" /etc/hosts |
For table-like files, show 2nd column | awk -F'\t' '{print $2}' /etc/hosts |
For table-like files, swap 1st and 2nd columns | awk -F'\t' '{print $2,$1}' /etc/hosts |
Name | Comment |
Echo red text | echo -e “hello,\e[0;31m there \e[0;31m” |
Echo multiple lines | echo -e “hello,\ndenny” |
Echo bold text | echo -e hello, “\033[1mThis is bold text.\033[0m” |
Echo underlined text | echo -e hello, “\033[4mThis is underlined text.\033[0m” |
1.2.3 Check process via /proc
Name | Comment |
Check process start command | cat /proc/$pid/cmdline |
Check process environment variables | cat /proc/$pid/environ |
Check process ulimits setting | cat /proc/$pid/limits |
Name | Comment |
Copy one file | cp /etc/hosts /tmp/hosts |
Copy one folder | cp -r /usr/local/bin/ /tmp/bin/ |
Copy for backup | cp /tmp/hosts{,.bak} , ls -lth /tmp/hosts* |
Name | Comment |
Go to given folder | cd /var/log/ |
Go to home | cd |
Go to parent folder | cd .. |
Go to previous folder | cd - |
Name | Comment |
* | expr 5 \* 4 |
+ | let z=x+y, z=$x+$y |
== | int1 -eq int2 , =[ $? -eq 0 ] && echo “good”= |
>= | int1 -ge =int2 |
> | int1 -gt =int2 |
<= | int1 -le =int2 |
< | int1 -lt =int2 |
!= | int1 -ne =int2 |
# Run grep for files filtered by find
find /var/log -name "*.log" | xargs grep -i error
# Loop with pipes
cat /etc/passwd | awk -F':' '{print $1}' | xargs -I{} sudo -l -U {} | grep -v "not allowed to"
[ 0 -eq $(find ./data -name "*.txt" -type f -print | wc -l) ]
/sbin/ifconfig eth0 | grep 'inet addr:' | cut -d: -f2 | awk '{ print $1}'
- Check if a string contains a substring
#!/bin/bash
S="Pineapple"
if [[ "${S}" == *apple* ]]; then
echo "Yes"
else
echo "No"
fi
- Check if a string in a list
stackoverflow link
#!/usr/bin/env bash
IAAS_TYPE="gcp|aws"
TYPE="gcp"
if [[ "gcp|aws" == *${TYPE}* ]]; then
echo "yes"
else
echo "no"
fi
License: Code is licensed under MIT License.