mktemp for directories

{ "env": "Ubuntu 22.04", "last_modified": "30/Oct/2022" }

mktemp command in linux creates a temporary file in /tmp. This is useful when I want to output huge logs into a file that needs to be deleted later. Sometimes I want to create a directory inside /tmp to miss around but creating one the regular way:
mkdir /tmp/"random letters I need to come up with"; cd /tmp/"what was the name again?"
...
this is too boring and very vanilla, same for looking online for a solution implemented, hence I will script it to be a handy one command. I will add the following command to ~/.bashrc so it becomes persistent.

The command

_ mktempd () { mkdir "/tmp/tmpd.$(cat /dev/urandom | tr -dc '[:alpha:]'| fold -w ${1:-10} | head -n1)" && echo $_ }

how it works:

mkdir /tmp/tmpd.XXXXXXXXXX

This will create a directory named "tmpd.XXXXXXXXXX" in /tmp. Note: XXXXXXXXXX will be repalced in the next command.

$(cat /dev/urandom | tr -dc '[:aplpha:]' | fold -w {1:-10} | head -n1

/dev/urandom will spill its content into stdout, then pipe that output to tr where it will delete all non alphabet characters, pipe the output of tr to fold to limit the output to only 10 charcaters, then head -n1 will give me the first line. this line will replace XXXXXXXXXX from the previous command

&& echo $_

If the last command was successful, print the paramater of the last command, this will print the path to the newly created temporary directory.

Usage:

1. cd $(mktempd)
2. sudo cp file{1..1000} $(mktempd) && cd $_ 3. sudo mount /dev/sdc1 $(mktempd) && cd $_

Please reach out via GitHub