Shell scripts and tools.

man pages

  1   User Commands
  2   System Calls
  3   C Library Functions
  4   Devices and Special Files
  5   File Formats and Conventions
  6   Games et. Al.
  7   Miscellany
  8   System Administration tools and Daemons

File testing in sh

-b filename     block special file
-c filename     special character file
-d dirname      check for directory existence
-e filename     check for file existence
-f filename     check for regular file existence not a directory
-G filename     check if file exists and is owned by effective group ID.
-g filename     true if file exists and is set-group-id.
-k filename     sticky bit
-L filename     symbolic link
-O filename     true if file exists and is owned by the effective user id.
-r filename     check if file is a readable
-S filename     check if file is socket
-s filename     check if file is nonzero size
-u filename     check if file set-user-id bit is set
-w filename     check if file is writable
-x filename     check if file is executable
-z <string> ... true if the length of the string is non-zero


if [ ! -e "$file" ]; then
  echo "File does not exist"
  echo "File exists"

Check if variable exists

the -z flag tests if the value of a string is zero, so in order to test if a value exists it must be inverted

$ test -z <varname>

Pipe stdout to multiple commands

$ cat file.txt | tee >(pbcopy) >(do_stuff) >(do_more_stuff) | grep errors

Find and replace in multiple files

$ ag -l <pattern> | xargs sed -i '' -E 's/<old>/<new>/g'

Delete a range of lines

$ cat file.txt | sed -e '1,2d'

Manipulate columns with awk

$ cat file.txt | awk '{$3=$1; gsub(/0[12345]_/, "", $3); $2="|"}{print}'

Check for value, fill in if it doesn't exist

$ screen_width=${COLUMNS:-$(tput cols)}

Connect to ssh server

ssh -i <path/to/file> <name>@<ip>

or with a ~/.ssh/configfile

ssh <Host>

list all open files for user

lsof -u <ownername>

follow logs as they grow

$ tail -r <file>

execute a command in npm module dir

npm ex <module name> <command> ... execute a command in the module dir


DNS lookup utility

# example
$ dig @ -p 5000 +short


Vim like file manager. Useful to do bulk directory operations. Offers different views on files.


System / gfx configuration tool.


boot manager


list all pci devices


List all available devices. Useful to determine how to partition.


Can be used to repair machines that have lost root access / are unbootable for other reasons. Live CDs ftw! Also used to reset the pid of a tree of processes, a commonly known technique used with docker.

manage audio players

$ playerctl

pipe stderr to stdout

# bash
$ <command> 2>&1 /dev/null

# POSIX sh
$ <command> >/dev/null 2>&1
cat << EOF
  oh my, such nice text

detect if script is sourced

if [ "$_" = "$0" ]
  then echo 'yup, script is directly called'
  else echo 'nope, script is not directly called'

Switch statement

case $1 in
  "")         usage; exit 1 ;;
  -h|--help)  usage; exit ;;
  -l|--link)  link "$@" ;;
  *)          readonly name=$1 ;;

Format text to be <80 chars

$ fmt -80

Create random file name

$ echo $RANDOM

Math in shell

In shell it's not recommended to use expr or bc, instead use:

res=$((foo - 2 / 3))

Prepend zeroes / add leading zeroes

printf "%02d" 4
# => 04

Shell traps

Cleaning up after yourself is essential in programming. The shell trap command listens to a POSIX signal, and then runs a command. It's event-driven programming in Shell!

warning: trap cannot process SIGKILL, as is the nature of the signal. Use kill -9 as a last resort.

Here's an example of trap in action:

readonly TMP_FILE='/tmp/bar.file'

# delete the file if the program is prematurely ended
pr $1 > "$TMP_FILE"


Renaming files

Renaming is made easy using the rename(1) command. Just plop in a regex and the renaming is done for you:

# Change foo to bar in matching filenames
$ rename 's/foo/bar/' *.txt

# Convert to lower case
$ rename -c *.txt

# Replace whitespace with underscores
$ rename 's/\s+/_/g' *.txt

# No action, just show what renames would occur
$ rename -n 's/foo/bar/' *.txt


The init process is the heart of the OS. It schedules everything that happens on top of the kernel.

$ du -s <directory>    # print size
$ du -sH <directory>   # print size and follow symlinks

Find distro version

$ uname -a

See Also

results matching ""

    No results matching ""