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 define and iterate over an array in Bash or Zsh

for x in 🍎 🍏 🍊
do
  echo ${x}
done
xs=(🍎 🍏 🍊)

for x in "${xs[@]}"
do
  echo ${x}
done
xs=(
🍎
🍏
🍊
)

for x in "${xs[@]}"
do
  echo ${x}
done

How to traverse all the direct subdirectories in Bash or Zsh

for dir in */
do
  echo $dir
done

How to traverse all the direct and indirect subdirectories in Bash or Zsh

for dir in **/
do
  echo $dir
done

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##*.}

How to restart the shell in Bash or Zsh

exec -l $SHELL

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

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

How to remove the first or the last character from a variable in Bash or Zsh

s=🍎🍏🍊
echo ${s#?} # 🍏🍊
echo ${s%?} # 🍎🍏

How to create a directory if it does not exist in Bash or Zsh

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

How to redirect stdout and/or stderr in Bash or Zsh

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

How to replace or delete substring(s) in Bash or Zsh

S=🍎_🍎
echo ${S/🍎} # _🍎
echo ${S//🍎} # _
echo ${S/🍎/🍊} # 🍊_🍎
echo ${S//🍎/🍊} # 🍊_🍊

How to create a file with content in Bash or Zsh

cat << EOF > sample.txt
🍎
🍏
🍊
EOF

How to detect the encoding of a file in Bash or Zsh

file --brief input.txt

How to copy a folder into another folder

rsync --archive src dst

How to convert multiple lines to a single line in Bash or Zsh

$ cat input.txt
🍎
🍏
🍊

$ paste -s -d, input.txt
🍎,🍏,🍊

How to merge files horizontally without using a matching column in Bash or Zsh

$ cat left.txt
🍎
🍏

$ cat right.txt
🍊
πŸ‹

$ paste -d, left.txt right.txt
🍎,🍊
🍏,πŸ‹

$ paste -d'\0' left.txt right.txt
🍎🍊
πŸπŸ‹

Zsh only (not work in Bash)

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"
}
$ 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 get the basename and the extension of a file

s=sample.txt
echo $s:r # sample
echo $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 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