Hot answers tagged

193

tmpfile=$(mktemp /tmp/abc-script.XXXXXX) : ... rm "$tmpfile" You can make sure that a file is deleted when the scripts exits (including kills and crashes) by opening a file descriptor to the file and deleting it. The file keeps available (for the script; not really for other processes but /proc/$PID/fd/$FD is a work-around) as long as the file descriptor is ...


115

The normal settings for /tmp are 1777, which ls shows as drwxrwxrwt. That is: wide open, except that only the owner of a file can remove it (that's what this extra t bit means for a directory). The problem with a /tmp with mode 777 is that another user could remove a file that you've created and substitute the content of their choice. If your /tmp is a ...


114

What happens to the actual content of /tmp when my hard drive is mounted? Pretty much nothing. They're just hidden from view, not reachable via normal filesystem traversal. Is it possible to perform r/w operations on the actual content of /tmp while the hard drive is mounted? Yes. Processes that had open file handles inside your "original" /tmp will ...


113

/tmp is meant as fast (possibly small) storage with a short lifetime. Many systems clean /tmp very fast - on some systems it is even mounted as RAM-disk. /var/tmp is normally located on a physical disk, is larger and can hold temporary files for a longer time. Some systems also clean /var/tmp, but less often. Also note that /var/tmp might not be available ...


69

A slightly more portable way to handle temporary files is to use mktemp. It'll create temporary files and return their paths for you. For instance: $ mktemp /tmp/tmp.zVNygt4o7P $ ls /tmp/tmp.zVNygt4o7P /tmp/tmp.zVNygt4o7P You could use it in a script quite easily: tmpfile=$(mktemp) echo "Some temp. data..." > $tmpfile rm $tmpfile Reading the man page, ...


63

Use mktemp to create a temporary file or directory: temp_file=$(mktemp) Or for a direcotry: temp_dir=$(mktemp -d) At the end of the script you have to delete the temporary file/dir: rm ${temp_file} rm -R ${temp_dir} mktemp creates file in the /tmp directory or in the drectory given with the --tmpdir argument.


60

The FHS mandates that /tmp exist, as does POSIX so you can rely on its being there (at least on compliant systems; but really it’s pretty much guaranteed to be present on Unix-like systems). But you shouldn’t: the system administrator or the user may prefer other locations for temporary files. See Finding the correct tmp dir on multiple platforms for more ...


44

The correct syntax in bash is the following: rm /tmp/!(lost+found) As @goldilocks wrote in the comments, the original command makes an expansion on the query (it deletes all the files in the /tmp folder, then goes on, and deletes all the files in the current working folder, in your case the home folder). You can try to check if you can recover some of ...


44

In practice, /tmp is pretty much guaranteed to exist. However, even if it exists, that doesn't mean you should put temporary files there. The standard convention is to use the TMPDIR environment variable. If it exists, it points to a directory for temporary files. If it doesn't exist, put temporary files in /tmp. In a shell script, you can use "${TMPDIR:-/...


26

/tmp may be, and sometimes is, cleaned on reboot. /var/tmp is preserved between reboots. See the Wikipedia article on the FHS.


26

You can use dpkg's hook system to remount it -- put this in /etc/apt/apt.conf.d/00exectmp: DPkg::Pre-Invoke {"mount -o remount,exec /tmp";}; DPkg::Post-Invoke {"mount -o remount /tmp";};


25

The !(lost+found) in your rm command was probably the fatal mistake: 1978 rm -rf /tmp/* !(lost+found) 1979 sudo rm -rf /tmp/* !(lost+found) I don't know exactly what bash is doing with that, but this command below prints everything in my /tmp/ and also everything my current directory (which is currently ~): echo /tmp/* !(lost+found)


22

You can mount a tmpfs partititon and write the file there: mount -t tmpfs -o size=500m tmpfs /mountpoint This partition now is limited to 500 MB. If your temporary file grows larger than 500 MB an error will occur: no space left on device. But, it doesn't matter when you specify a larger amount of space than your systems RAM has. tmpfs uses swap space too, ...


21

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.


15

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 ...


15

They have the same purpose and functionality. Every version of UNIX/Linux will handle these directories differently. Historically, before the advent of RAM/swap based filesystems, you had disk-less systems where the / and /usr filesystems would be read-only and /var (variable) would be read-write. The /tmp name would be a symbolic link to /var/tmp. Later,...


15

If you're on system which has mktemp, you should use it as other answers. With POSIX toolchest: umask 0177 tmpfile=/tmp/"$0"."$$"."$(awk 'BEGIN {srand();printf "%d\n", rand() * 10^10}')" trap 'rm -f -- "$tmpfile"' INT TERM HUP EXIT : > "$tmpfile"


14

When the system boots and the harddrive is full, nothing can write to /tmp. So during init a tmpfs is created and mounted. This way your system can safely boot, because it can write to /tmp. Free up disc space, and reboot your machine. (Or simply unmount /tmp, if you are sure nothing uses it). Setting this value to 0 disables the setup., which I would ...


14

You can resolve which filesystem a directory or file is on with the command df, and if you include the -T option, the output will include the filesystem type. $ df -T /tmp Filesystem Type 1K-blocks Used Available Use% Mounted on /dev/sda3 ext4 38715020 5073600 31674780 14% / In the above example, the /tmp directory is on an ext4 filesystem, ...


14

Some shells have the feature built-in. zsh zsh's =(...) form of process substitution uses a temporary file. For instance =(echo test) expands to the path of a temporary file that contains test\n. $ {cat $file; ls -l /dev/fd/3; echo test2 >&3; cat $file} 3<> ${file::==(echo test)} test lrwx------ 1 stephane stephane 64 Jan 30 11:19 /dev/fd/3 -&...


14

Your root file system is full and hence your temp dir (/tmp, and /var/tmp for that matter) are also full. A lot of scripts and programs require some space for working files, even lock files. When /tmp is unwriteable bad things happen. You need to work out how you've filled the filesystem up. Typically places this will happen is in /var/log (check that you'...


13

Use the mktemp utility to create a temporary file with an unpredictable name. It isn't standardized by POSIX, but it's available on *BSD as well as Linux. > /tmp/predictable.$RANDOM is not a good choice because it's mostly predictable¹, which opens your script to an attack where the attacker can trick your script into overwriting a file you have write ...


13

You may also have lost write access to the /tmp/ directory. It should look like that: ls -l / |grep tmp drwxrwxrwt 7 root root 4096 Nov 7 17:17 tmp You can fix the permissions like that: chmod a+rwxt /tmp


13

I would say it is not safe in general. On many systems, /tmp is cleaned on reboot by default. See /etc/default/rcS (TMPTIME defaults to 0), # delete files in /tmp during boot older than x days. # '0' means always, -1 or 'infinite' disables the feature #TMPTIME=0


13

I use pam-tmpdir for this: it creates a user-private temporary directory at login. To set it up, add session optional pam_tmpdir.so to the appropriate PAM services; on a Debian-based system, installing the libpam-tmpdir package will offer to do this for you, or you can add the line to /etc/pam.d/common-session. The next time you log in, you’ll find a ...


12

While it is true that all flash based storage devices have a limited number of writes before the transistor insulation breaks down, it's not as bad as it was when SSDs were first introduced years ago. Basically due to the fact that most modern SSD's employ wear leveling and are based on NandFlash, burning through a drive is not a problem like it used to be. ...


12

I was facing the same issue on one of my Ubuntu server, after searching on net I got the solution. As a protection against low disc space, some daemons automatically "shadows" the current /tmp/ dir with a ram disc if the the root partition runs out of disc space. Sadly there's no automatic reversion of that process once enough disc space is free again. To ...


12

You can ask systemd what a unit’s triggers are: systemctl show -p TriggeredBy systemd-tmpfiles-clean This will show that the systemd-tmpfiles-clean service is triggered by the systemd-tmpfiles-clean.timer timer. That is defined as # SPDX-License-Identifier: LGPL-2.1+ # # This file is part of systemd. # # systemd is free software; you can redistribute ...


11

POSIX Base Specifications, Issue 7 on /tmp: The following directory shall exist on conforming systems and shall be used as described: /tmp A directory made available for applications that need a place to create temporary files. Applications shall be allowed to create files in this directory, but shall not assume that such files are preserved ...


Only top voted, non community-wiki answers of a minimum length are eligible