Hacker News new | ask | show | jobs
by koolba 3284 days ago
I have a shell alias that creates a temp directory labeled with the current time stamp and suffixed tag then changes to it. Makes it very quick get a new scratch area for shell fu.

Plus all the scratch dirs are in one place so cleanup is a breeze to free up space.

1 comments

Sharing is caring? :)
Ask and ye shall receive!

    make-scratch-dir () {
      local name="$1"
      local pattern='^[a-z0-9\\-]+$'
      if [[ "$#" != 1 ]]; then
        echo "Usage: make-scratch-dir <name>" 1>&2
        return 1
      elif [[ ! "$name" =~ $pattern ]]; then
        echo "Invalid name: ${name}"
      fi
      local full_path="$HOME/tmp/scratch/$(date +%Y%m%d-%H%M%S)-${name}"
      mkdir -p "${full_path}"
      pushd "${full_path}"
      echo "Now in temp dir: ${full_path}"
    }
The directory change is done via pushd so you can hop back via popd. Also, it restricts the suffixes to ensure the directories are all "simple" names.
I didn't know of pushd, very interesting, may be useful for directory traversal in general.