I'm starting to write a few bash scripts for my project and they need some intermediate files or variables to be written.

I want to know which folders can I be sure of having access to when someone runs my script? Is it a common practice to use /tmp/ ? Or should I used something else?


I find the Filesystem Hierarchy Standard document invaluable when looking for this stuff.

There are a few options,

  • /tmp - 'non-permanent' temporary files
  • /var/tmp - 'permanent' temporary files
  • /var/cache - 'application' transient data files

It really depends on the kind of data you're storing.


If you need to write some temporary files that only last as long as your script or application is running, use the directory indicated by the TMPDIR environment variable, or if that variable is not defined, /tmp.

/tmp is cleared at boot time on some systems (sometimes it's even in RAM, e.g. by default on Solaris, and on some Linux installations), so it cannot be used for files that must survive a power failure or a reboot. /var/tmp can be used for files that must survive a reboot, but that may be cleaned by the system administrator from time to time. If your application needs to save files on a permanent basis, write them somewhere in the user's home directory (in ~/.programmingnoobsapp or ~/.cache/programmingnoobsapp) or under /var/lib/programmingnoobsapp or /var/cache/programmingnoobsapp.

Note that /tmp is shared between all users, so you need to take precautions when creating a file there. You need to pick a file name that doesn't exist yet, and you need to be careful not to allow a race condition where another process creates the file ahead of you with different permissions, which could be a security hole (if the other process is running as a different user, it could then access and modify your process's data). Use the mktemp command to create a file in /tmp or /var/tmp. By default, mktemp creates a file in $TMPDIR, or /tmp if TMPDIR is unset, which is usually the right place. If you need to use multiple temporary files, or even if you need a single one, I recommend creating a directory for all your temporary files with mktemp -d and removing it at the end of your script.

trap 'rm -rf "$tmp_root"' EXIT INT TERM HUP
tmp_root=$(mktemp -d)
  • wrt to mktemp, I do not remove temporary files at the end of my script. I have a tmpwatch cron job which automatically removes old files. In the vast majority of cases, I do not read the intermediate file and the system will delete the files by itself. However, if something goes wrong with the script, I have all my intermediate files for easy debugging. – emory Jun 18 '13 at 4:20
  • 1
    +1 for mktemp. For me this is the correct answer: delegate the task of safely creating a temporary file to mktemp (see this article ). Although the one answer quoting the FHS has great value too. – Carlos Campderrós Jun 18 '13 at 7:51
  • Why do you initialize tmp_root before assigning it? Wouldn't it be more natural to unset it? – l0b0 Jun 18 '13 at 10:53
  • @l0b0 Either will work (except under set -u, but that's uncommon). It needs to be (un)set to a safe value before setting the trap, in case the script is killed just after setting the trap. – Gilles 'SO- stop being evil' Jun 18 '13 at 10:58
  • I'd add set -o errexit so that if tmp_root is already set read-only in the environment it won't destroy the wrong directory... – l0b0 Jun 18 '13 at 12:08

Just to complement the answers that have already been posted thus far.

There is also /dev/shm on some Linux distributions which can be use for temporary storage. This storage should only be used when performance of the file I/O is a critical factor given /dev/shm makes use of the tmpfs filesystem. Also it should be used for reasonably sized files & data. The tmpfs filesystem makes use of a system RAM as storage so it isn't persistent from boot to boot.

There's a good overview of all the choices mentioned here on StackOverflow as well in this Q&A titled: /tmp vs. /dev/shm for temp file storage on Linux?. It's covered well in this SuperUser Q&A titled: When should I use /dev/shm/ and when should I use /tmp?.



Yes, /tmp is mostly for files that are required temporarily. The /tmp has sticky bit on which mean only the item's owner, the directory's owner, or the superuser can rename or delete files. many programs use this to create lock files and for temporary storage of data. On some distribution this directory is cleared out at the bootup or shutdown.

  • 1
    Not all /tmp directories are cleared at boot. For example, default AIX installations don't (and this is UNIX & Linux, not just Linux). – EightBitTony Jun 17 '13 at 22:55
  • However, if it is "memory" resident, which seems to be default in a lot of cases, it vanishes when the system stops for any reason. – mdpc Jun 18 '13 at 4:13

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.