Skip to content

Commit

Permalink
🐚 mktmpdir
Browse files Browse the repository at this point in the history
  • Loading branch information
juftin committed Sep 10, 2024
1 parent 4c34588 commit 4af6bf6
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 3 deletions.
1 change: 1 addition & 0 deletions docs/dotfiles/features.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ make | sed 's/\x1b\[[0-9;]*m//g'

- `mkcd`: Create a directory and change to it.
- `mktmpdir`: Create a temporary directory.
- `mkcdtmp`: Create a temporary directory and change to it.
- `ideacd`: Open a directory in the IDE and switch to it.
- `runin`: Run command in another directory and return.

Expand Down
25 changes: 22 additions & 3 deletions shell/.shell_functions
Original file line number Diff line number Diff line change
Expand Up @@ -77,9 +77,28 @@ mkcd() {
# Create a temporary directory
#############################################
mktmpdir() {
test -z "$TMPDIR" && TMPDIR="$(mktemp -d)"
mkdir -p "${TMPDIR}"
echo "${TMPDIR}"
if [[ -f /usr/share/dict/words ]]; then
RANDOM_WORD_1=$(shuf -n 1 /usr/share/dict/words | tr '[:upper:]' '[:lower:]')
RANDOM_WORD_2=$(shuf -n 1 /usr/share/dict/words | tr '[:upper:]' '[:lower:]')
RANDOM_CHARS="${RANDOM_WORD_1}-${RANDOM_WORD_2}"
else
RANDOM_CHARS=$(openssl rand -base64 30 | tr -dc 'a-z' | head -c 20)
fi
DATE_STRING=$(date +%Y%m%d)
NEW_TEMP_DIR="/tmp/${DATE_STRING}/${RANDOM_CHARS}"
mkdir -p "${NEW_TEMP_DIR}"
echo "${NEW_TEMP_DIR}"
}

#############################################
# mkcdtmp
# -------------------------------------------
# Create a temporary directory and cd into it
#############################################
mkcdtmp() {
NEW_TEMP_DIR="$(mktmpdir)"
echo "${NEW_TEMP_DIR}"
cd "${NEW_TEMP_DIR}"
}

#############################################
Expand Down

0 comments on commit 4af6bf6

Please sign in to comment.