Hacker News new | ask | show | jobs
by munchor 3284 days ago
Sharing is caring? :)
1 comments

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.