Skip to content

Cheat sheets for various commands and scripts

Notifications You must be signed in to change notification settings

tatsuyafujisaki/script-cheat-sheet

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

How to denote required and optional arguments on the command line

  • <required_argument>
  • [optional_argument]

https://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap12.html

Bash/Zsh

How to find the absolute script path in Bash or Zsh

echo $(realpath $0)

How to find the absolute script directory in Bash or Zsh

echo $(cd ${0%/*} && pwd -P)

How to find the script's filename extension in Bash or Zsh

echo ${0##*.}

Sample function that requires two arguments

my_function() {
  if [ $# -lt 2 ]
  then
    echo "Usage: $funcstack[1] <first-argument> <second-argument>"
    return
  fi

  echo "First argument: $1"
  echo "Second argument: $2"
}

Usage

$ my_function
Usage: my_function <first-argument> <second-argument>

$ my_function foo
Usage: my_function <first-argument> <second-argument>

$ my_function foo bar
First argument: foo
Second argument: bar

How to restart the shell

exec -l $SHELL

How to show the definition of an alias

alias <alias-name>

How to print date and time in yyyy-mm-dd_hh-mm-ss

date +%Y-%m-%d_%H-%M-%S

How to remove the first or the last character from a variable

s=abc
echo ${s#?} # bc
echo ${s%?} # ab

How to create a directory if it does not exist

directory=~/foo
[ -e ${directory} ] || mkdir -p ${directory}

How to redirect stdout and/or stderr

echo hello > /dev/null # redirects stdout only.
echo hello 2> /dev/null # redirects stderr only.
echo hello &> /dev/null # redirects both stdout and stderr.

How to replace or delete substring(s)

S=old_and_old
echo ${S/old} # deletes the first "old" and shows "_and_old".
echo ${S//old} # deletes all the "old" and shows "_and_".
echo ${S/old/new} # replace the first "old" and shows "new_and_old".
echo ${S//old/new} # replace all the "old" and shows "new_and_new".

How to create a file with content

cat << EOF > sample.txt
aa
bb
cc
EOF

How to resolve a relative path to the absolute path

realpath <file-or-folder>

How to detect the encoding of a file on both Mac/Linux

file -b sample.txt

Zsh

How to get a basename and an extension

s=sample.txt

echo Basename: $s:r # sample
echo Extension: $s:e # txt

How to keep a background job running even after Zsh is closed

<command> &|

https://zsh.sourceforge.io/Doc/Release/Shell-Builtin-Commands.html

How to copy a folder to another folder as a subfolder

rsync -a src dst # Note that it's not "/src" but "src"

How to copy only the content of a folder to another folder

# Note "src/", not "src"
rsync -a src dst

How to convert multiple lines to a single line

$ cat input.txt
aa
bb
cc

$ paste -s -d, input.txt
aa,bb,cc

How to merge files horizontally without using a matching column

$ cat left.txt
aa
bb

$ cat right.txt
xx
yy

$ paste -d, left.txt right.txt
aa,xx
bb,yy

$ paste -d'\0' left.txt right.txt
aaxx
bbyy

How to concatenate files horizontally excluding unmatched rows without using a matching column

  • -t is a separator
  • -1 is the one-based index of a matching column from a first input file.
  • -2 is the one-based index of a matching column from a second input file.
$ cat foo.txt
1,aa
2,bb
4,cc

$ cat bar.txt
1,xx
2,yy
5,zz

$ join -t, -1 1 -2 2 foo.txt bar.txt
1,aa,xx
2,bb,yy

Misc cheet sheet

#kill found processes
ps aux | grep "my_process_name" | awk '{ print $2 }' | xargs kill

#check if command exists
command -v foo > /dev/null 2>&1 || echo 'command not found.'

#one liner
true && { echo 'true1'; echo 'true2'; } || { echo 'false1'; echo 'false2'; }
false && { echo 'true1'; echo 'true2'; } || { echo 'false1'; echo 'false2'; }

# tee both stdout and stderr
./foo.sh 2>&1 | tee -a foo.log

# set JAVA_HOME if not set
[ -z ${JAVA_HOME} ] && export JAVA_HOME=/path/to/javahome

#use DEFAULT_JAVA_HOME if JAVA_HOME not set
DEFAULT_JAVA_HOME=/path/to/javahome
${JAVA_HOME=${DEFAULT_JAVA_HOME}} yourclassfile

#get timstamp in yyyyymmdd
stat -c %y ${FILE1} | awk '{print $1}' | tr -d '-'

#Extract file from targz
tar fvxz foo.tar.gz -C /path/to/destination file_in_targz > /dev/null

Array

array.md

Directory

directory.md

Regex

regex.md