-
Notifications
You must be signed in to change notification settings - Fork 9
/
cd-missing-mkdir
45 lines (39 loc) · 1.15 KB
/
cd-missing-mkdir
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#!/bin/bash
# mkdir wrapper | Vivien Didelot and Spencer Tipping
# Licensed under the terms of the MIT source code license
# Keep a special file key in each directory we should nuke.
cd_autorm=$(basename "$(mktemp -u "$HOME/.cd-autorm-XXXXXXXXXXXXXXXX")")
cd_on '^!$' cd_make_permanent
function cd_make_permanent {
if [[ -e "$cd_autorm" ]]; then
echo "cd: $PWD is now permanent"
rm -f "$cd_autorm"
else
echo "cd: $PWD is already permanent"
fi
}
function cd_missing_fn {
local target=$1
if ! mkdir -p -- "$target"; then
echo "cd: cannot create ephemeral directory $target"
return 1
fi
touch -- "$target/$cd_autorm"
echo "cd: created ephemeral directory $target; cd ! to make it permanent"
cd_goto "$target"
}
function cd_missing_unfn {
# Look for the autorm key. This tells us that we created the directory, so we
# have the ability to nuke it.
local mode=$1
local from=$2
local to=$3
if [[ -e "$from/$cd_autorm" ]]; then
rm "$from/$cd_autorm"
if rmdir "$from" >& /dev/null; then
echo "cd: removed empty ephemeral directory $from"
else
echo "cd: $from is nonempty; converted to permanent"
fi
fi
}