★ cdtmp: change to a temporary directory
• 136 words • 1 min • updated
⚠️ This post is over one year old. It may no longer be up to date or relevant. Opinions may have changed.
Sometimes I need to do random experimentation in a throwaway directory.
For that, I have the following cdtmp function in my shell:
shell
cdtmp () {
builtin cd "$(mktemp -d "/tmp/$USER-${1:+$1-}$(date +%Y-%m-%d)-XXXXXX")" || return
builtin pwd
}Here are two example usages:
shell
% cdtmp
/tmp/thiago-2024-10-07-V3c3Na
thiago@thiagoperrotta-MacBook-Pro /tmp/thiago-2024-10-07-V3c3Na
% cdtmp devops
/tmp/thiago-devops-2024-10-07-P4W1fhThis idea was built upon Alex Kotliarskyi’s, which he describes as:
It’s a super simple alias that creates a temporary directory and then jumps into it. Here are a few examples of what I use it for:
— Clone a random interesting git repo to experiment with
I also use it for testing one-off bash, C++, golang or python scripts, for example:
shell
% cdtmp
/tmp/thiago-2024-10-07-F5aFrJ
thiago@thiagoperrotta-MacBook-Pro /tmp/thiago-2024-10-07-F5aFrJ
% vim main.sh
thiago@thiagoperrotta-MacBook-Pro /tmp/thiago-2024-10-07-F5aFrJ
% bash main.sh
hello worldBacklinks
- Try (Dec 02, 2025)
- Online diff tool (Dec 22, 2024)