Here's a solution I hammered out:

function gettmp
        set DIRNAME tmp-dir.(uuidgen)
        mkdir $DIRNAME
        cd $DIRNAME
end

set -gx SET_PWD "$PWD"
set -gx OLD_PWD "$PWD"

function react_to_pwd --on-variable PWD
        set -gx OLD_PWD "$SET_PWD"
        set -gx SET_PWD "$PWD"

        switch "$OLD_PWD"
        case "*tmp-dir.*"
                switch "$PWD"
                case "*tmp-dir.*"
                        echo -n ""
                case "*"
                        echo "Removing $OLD_PWD"
                        rm -rf "$OLD_PWD"
                end
        end
end

The react_to_pwd is called any time PWD changes and keeps track of the last PWD that was set. Then, anytime you change out of a directory containing tmp-dir.* into a non-temp directory, the old directory will be removed.

gettmp just creates and cd's into a directory with that naming scheme.

Garrett

On 2/1/22 5:37 AM, Michael Stillwell wrote:
I occasionally need to do various tasks that require the creation of a temporary directory, something like:

$ mkdir tmp
$ cd tmp
$ unzip ../foo.zip
$ # do something to the contents of tmp
$ cd ..
$ rm -rf tmp

What's the idiomatic fish way of creating the directory (with a random, unique name) and ensuring it's deleted? Do I need to dirty my hands with /usr/bin/mktemp??



Michael


_______________________________________________
Fish-users mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/fish-users
--
Garrett Mills
garrettmills.dev <https://garrettmills.dev/>
_______________________________________________
Fish-users mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/fish-users

Reply via email to