Logo  

CS469 - Linux and Unix Administration and Networking

Common scripting utilities and programs:

> true

  • Always succeeds (true)

> false

  • Always fails (false)

> sort [-k n [ ,m ]] [ -n ] [ -r ] [ -u ] [ file ]

  • Sort the lines in a file.
    -n - Numeric sort
    -r - Reverse order
    -u - Output only the unique entries
    -k n[,m] - Select field or fields to sort on.

> uniq [ -c ]

  • Filters out adjacent matching lines.
    -c - Print a count

    Example:

    users | tr ' ' '\n' | uniq -c

    • outputs # number of unique users on a system.

> basename name [ suffix ]

  • Strip the directory name [and suffix] from a filename.

> dirname name

  • Strip last component from filename.

basename / dirname examples:

file="/some/path/to/file.txt";
dir=$(dirname "$file");                     # dir <- "/some/path/to"
filename=$(basename "$file");               # filename <- "file.txt"
basefilename=$(basename "$file" ".txt");    # basefilename <- "file"

> tr [ -d ] set1 [ set2 ]

  • Translate or delete characters
    -d - Delete any characters in set1 from the input
  • Otherwise translate set1 onto set2

> tac

  • Cat files in reverse

> colrm [ first [ last ]]

  • Remove (character) columns from a file.
    ex:
    ps aux | grep -v root | colrm 16 60

> cut [ -f field-number(s) ] [ -d deliminator-character ]

  • Remove sections from each line of a file.
    ex:
    cut -f 1 -d : /etc/passwd

> tee

  • Read from stdin and write to stdout and files at the same time.
    ex:
    ps aux | tee ps.out
    • Displays the output of ps aux while saving it to ps.out at the same time.

> env

  • Run a program in a modified environment.
    ex:
    env -i PATH=/bin HOME=/tmp printenv

> date [-d when] [+format ]

  • Print or set system date and time.
    Examples:

time=$(date +%s);

  • Get the time in seconds since the epoch

date -d yesterday

  • Yesterdays date

date -d '2021-03-01 yesterday'

  • Last day of February 2021