7

I often want a temporary directly where I can unpack some archive (or create a temporary project), see around some files. It is unpredictable in advance for how much time a particular directory may be needed.

Such directories are often clutter home directory, /tmp, project directories. They often have names like some weak passwords, like qqq, 1, test that become undescriptive a month after.

Is there some shell command or external program that can help manage such throw-away directories, so that they get cleaned up automatically when I lose interest in them, where I don't need to invent a name for them, but that can be given a name and made persistent easily?

If there is no such tool, is it a good idea to create one?

2
  • 1
    I just use ~/temp and if it already has stuff in it, delete it. – user253751 Jul 14 at 8:55
  • I make temporary directories constantly and have commands to help me. j creates a directory junk (mkdir -p junk) and cds into it. jjj is equivalent to rm -fr junk and jj is equivalent to rmdir junk (only deletes it if it's empty). Of course if I want to make it permanent (almost never), then mv handles that. If I accidentally leave a junk directory around, my backup ignores junk directories. It's a little more elaborate than that, but not much. – jrw32982 Jul 16 at 4:25
13

It doesn’t quite cover all the features you mention (easily making the temporary directory persistent), but I rather like Kusalananda’s shell for this. It creates a temporary directory, starts a new shell inside it and cleans the temporary directory up when the shell exits.

Before the shell exits, if you decide you want to keep the temporary directory, send a USR1 signal to shell; typically

kill -USR1 $PPID

When you exit, shell will tell you where to find the temporary directory, and you can move it somewhere more persistent.

If there is no such tool, is it a good idea to create one?

This is the best kind of tool to create — you already know it would be useful for you.

5
  • 2
    My script (thanks for the plug!) is more or less a glorified variant of ( d=$(mktemp -d) && cd -- "$d" && "$SHELL" && cd - && rm -rf -- "$d" ). – Kusalananda Jul 14 at 10:18
  • @Kusalananda, pretty much exactly the set of commands I almost wrote as an answer here :P (didn't remember the more developed script, though). For optional persistence, could probably add some functions to e.g. create a marker file in the dir for the script to check before deleting... – ilkkachu Jul 14 at 11:17
  • shell seems to mangle environment variables (including HOME), preventing something like 7z x ~/Download/*.zip; gedit tool.c. – Vi. Jul 14 at 18:59
  • Yes, it sets HOME to the temporary directory, to ensure a clean startup; I suspect that comes from the original idea behind shell, which was a tool to help with testing answers for questions on Unix.SE! However ~user still works, so 7z x ~user/Download/*.zip; gedit tool.c (replacing user as appropriate) would do the trick. – Stephen Kitt Jul 14 at 19:08
  • @Vi. As Stephen said, shell sets HOME so that writing to files under ~/ does not modify files in the user's actual home directory. Use ~user instead, with your username. It also clears most of the environment to avoid having variables affecting the behavior of some tools. – Kusalananda Jul 21 at 9:33
3

I just use cd $(mktemp -d) and then hack away. In general, the stuff exists for as long as I need it, but it goes away without any action on my part.

I think my OS is configured (by default not by action on my part) to delete temporary files on reboot. Other distributions are auto-configured to delete temporary files after a certain period of inactivity.

Either way would work for me (and probably for you).

4
  • 1
    Most of the time I create such dirs on tmpfs, so they also disappear on reboot. But the reboot may only come half a year later. – Vi. Jul 14 at 11:09
  • Another way to handle it would be to do your work inside a docker container. – emory Jul 14 at 22:11
  • Docker environment (unless specially prepared) also lacks easy access to files around, helper scripts and GUI. – Vi. Jul 15 at 22:06
  • @Vi I do not know how the temp files get cleared other than whatever mechanism does it works good enough for me. I guess the first question is: are you dealing with sensitive data (e.g., trade secrets, PHI, etc.). If not then it seems the only real issue is running out of disk space. Why not just delete all your temp files whenever your disk space is tight? – emory Jul 21 at 13:40
0

When you say "get cleaned up automatically when I lose interest in them", it is pretty subjective and can vary from person to person. That said, the word "automatically" immediately brings the cron utility into mind. Here is a simple one-time setup that should address your requirements (or at least part of it). Please note that I am not claiming to cover all possible scenarios and advanced usage, just trying to help the OP.

Overview:

  1. Define an alias to create directory with a random name, but with a pre-assigned suffix.
  2. Use this alias instead of mktemp whenever you want to create a tempdir. It will spit out the name of the tempdir when you run it and it will always create the dir in /tmp.
  3. Run a cron job to find and remove these temp directories, but matched with certain criteria (e.g. older than 30 days).

Detailed steps:

Define the alias called mktmpdir (you can put this in your ~/bash_aliases or ~/.bashrc):

alias mktmpdir='mktemp -d --suffix=.delme'

-d (create a directory, not a file)

--suffix=.delme (always add a suffix called .delme to dirs. Note the period)

Run crontab -e to add the cron job which will clean out directories with modification time older than 30 days (there are many options to the find command, just read the man page):

0 5 * * 0 find /tmp -type d -ctime +30 -name *.delme -user YourUserName -exec rm -rf {} + 

Explanation of options:

  • 0 5 * * 0 (run this job at 5 AM on Sundays)
  • -type d (only directories)
  • -ctime +30 (older than 30 days)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.