- Everything Essential About the tmp Directory in Linux
- What is the /tmp directory in Linux?
- Are /tmp and /var/tmp the same? No!
- Automate tmp directory cleaning
- Did you know about the black hole of Linux filesystem?
- Creating a Temporary File in Linux
- Creating a Temporary File in Linux
- Syntax
- Create a Random Temporary File in tmp Directory
- Specifying a Directory and Permissions
- Specify Template of temporary file
- Create multiple Temporary files
- Checking the Status of the Temporary File
- Removing the Temporary File
- mktemp output and file status
- Example
- Use mktemp in Bash Script
- Example
- Output
- Conclusion
- How to create temporary files on linux that will automatically clean up after themselves no matter what?
- 6 Answers 6
Everything Essential About the tmp Directory in Linux
Learn some crucial things about the /tmp directory. You’ll also learn how it is different from the /var/tmp directory.
If you have been using Linux for a while, you must have come across the /tmp directory.
You may have some idea about it but you probably didn’t pay enough attention to it.
Then there is also a /var/tmp directory that sounds similar.
So in this article, I will walk you through some crucial things that you need to know about the /tmp directory. I’ll also discuss how it is different from the /var/tmp directory.
What is the /tmp directory in Linux?
As the name suggests, the tmp (temporary) directory under root is used to store the data used by the system and user applications to store the data that are needed for a short period of time. Most Linux distributions are preconfigured to empty the tmp directory after each reboot.
Sounds complex? Let me give you an example.
So let’s suppose you are installing software in your system so the installer may store some files that are needed during the installation.
Similarly, while working on a project, your system may store the files in the tmp directory when making changes or they can also be the auto-saved versions of that file.
In simple words, the tmp directory is nothing but a directory used to store the files that are needed temporarily and can be removed once they are no longer needed.
Are /tmp and /var/tmp the same? No!
Yes, there is a significant difference between the /tmp and the /var/tmp directory.
The short answer is how they both deal with the temporary files.
The /tmp directory is used to store the short-lived temporary files whereas the /var/tmp directory is used to store long-lived temporary files.
Want more details? Here you have it!
- Endurance: Generally, the files stored in the /tmp directory are removed at the boot time whereas the files inside /var/tmp are kept even after reboot.
- For user VS Systemwide: Typically, the files inside the /tmp directory can be accessed by every user whereas the files of /var/tmp are mostly user-specific.
- Usage (the most crucial difference): The /tmp directory is used to store the files that are needed for a short time like for the installation of a package. Whereas the /var/tmp directory is used for files that are needed for a longer period of time like system backup or log files.
Automate tmp directory cleaning
As I said earlier, most, if not all, distributions clean the /tmp directory when you reboot your Linux system.
If that’s the case, then why do you need to explicitly clean the /tmp directory? Because you don’t reboot your server everyday like a desktop computer. I mean check the uptime of your server; it might be running for weeks, if not for months and years.
This is not needed for everyone. Only if your server is running low on the disk space, go for the extra effort of automatic tmp directory cleaning.
To automate the cleaning of the tmp directory, the most critical thing is to identify what to remove in the first place.
So the sweet spot is to remove the files that are not used for the last three days and are not owned by the root.
And for that purpose, you can use the find command in the following manner:
sudo find /tmp -type f \( ! -user root \) -atime +3 -delete
But this won’t automate the process.
First, open the root crontab using the following:
If you are using the corn table for the first time, it will ask you to choose your preferred text editor. I will recommend using the nano:
Once done, go to the end of the file in nano using Alt + / and paste the following line into the file:
0 0 * * * sudo find /tmp -type f ! -user root -atime +3 -delete
Save changes and that’s it!
Did you know about the black hole of Linux filesystem?
I’m talking about the /dev/null directory here as whatever is sent there, can not be traced back! Want to know how it can be used? Here you have a detailed guide:
I hope you will find this guide helpful. And if you have any questions or suggestions, leave a comment.
Creating a Temporary File in Linux
In Linux, it is often necessary to create temporary files for various purposes, such as storing intermediate data during processing or storing configuration information for a script. Temporary files are usually created in the /tmp directory, which is a standard location for storing temporary files on most Linux systems.
Creating a Temporary File in Linux
There are several ways to create a temporary file in Linux. One of the most common methods is to use the mktemp command, which creates a unique temporary file and prints the file name to the console.
Syntax
To create a temporary file using mktemp, use the following syntax.
The template argument is a string that specifies the name and location of the temporary file, and can include XXXXXX as a placeholder for a unique suffix that mktemp will generate. The options argument is optional and can be used to specify various options, such as the directory in which to create the file or the permissions to set on the file.
Create a Random Temporary File in tmp Directory
The simplest way to create a temporary file can be done by running mktemp command without any arguments. If you run the mktemp command without any arguments, it will create a unique temporary file in the default temporary file directory (usually /tmp) with a default template of tmp.XXXXXX.
This will create a file with a name like /tmp/tmp.qhgG9f.
You can also use the -q option to suppress the output of the mktemp command and store the file name in a variable instead.
$ temp_file=$(mktemp -q) $ echo "Temporary file: $temp_file"
This will create a temporary file with a name like /tmp/tmp.qhgG9f and store the file name in the temp_file variable. The echo command will then print the file name to the console.
Specifying a Directory and Permissions
To specify a different directory in which to create the temporary file, use the -d option.
$ mktemp -d /my/custom/dir/temp.XXXXXX
This will create a temporary file in the /my/custom/dir directory with a name like temp.qhgG9f.
$ mktemp -d /my/custom/dir/temp.XXXXXX
You can also use the -p option to specify a prefix for the file name, like this −
$ mktemp -p /my/custom/dir mytemp.XXXXXX
This will create a file with a name like /my/custom/dir/mytemp.qhgG9f.
Specify Template of temporary file
To create a temporary file in the /tmp directory with a unique suffix and default permissions, you can use the following command −
This will create a file with a name like /tmp/temp.qYhg9f, where qYhg9f is the unique suffix generated by mktemp.
You can also use the -t option to specify a template that includes the XXXXXX placeholder, like this −
This will create a file with a name like temp.qYhg9f in the default temporary file directory (usually /tmp).
Create multiple Temporary files
To create multiple temporary files at once, use the -u option followed by a template with multiple instances of the XXXXXX placeholder.
$ mktemp -u /tmp/temp1.XXXXXX /tmp/temp2.XXXXXX
This will create two temporary files with names like /tmp/temp1.qhgG9f and /tmp/temp2.qhgG9f.
Checking the Status of the Temporary File
After creating a temporary file, you may want to check its status to ensure that it was created successfully. You can use the stat command to display information about a file, including its size, permissions, and creation date.
To use stat, specify the name of the file as an argument, like this −
This will display information about the file, including its size, permissions, and creation date.
Removing the Temporary File
When you are finished with a temporary file, it is a good idea to remove it to free up disk space and prevent clutter. You can use the rm command to remove a file, like this:
This will delete the file permanently, so be sure that you no longer need the file before removing it.
mktemp output and file status
Here is an example of the mktemp command in action −
Example
$ mktemp /tmp/temp.XXXXXX /tmp/temp.qhgG9f $ stat /tmp/temp.qhgG9f File: /tmp/temp.qhgG9f Size: 0 Blocks: 0 IO Block: 4096 regular empty file Device: 802h/2050d Inode: 1234 Links: 1 Access: (0664/-rw-rw-r--) Uid: ( 1000/ user) Gid: ( 1000/ user) Access: 2022-12-16 11:07:35.000000000 -0500 Modify: 2022-12-16 11:07:35.000000000 -0500 Change: 2022-12-16 11:07:35.000000000 -0500 Birth: - $ rm /tmp/temp.qhgG9f
In this example, the mktemp command creates a temporary file in the /tmp directory with a unique suffix
Use mktemp in Bash Script
Once the temporary file has been created, you can use it like any other file on the system. When you are finished with the file, you can remove it using the rm command.
Example
Here is an example of creating a temporary file and then displaying its contents −
# Create the temporary file and store its path in a variable temp_file=$(mktemp) # Write some content to the file echo "This is a temporary file" > $temp_file # Display the contents of the file cat $temp_file # Remove the temporary file rm $temp_file
This code will create a temporary file, write the string «This is a temporary file» to it, display the contents of the file, and then delete the file. The output of this code will be −
Output
Conclusion
In Linux, temporary files can be created easily using the mktemp command. By specifying a template and optional options, you can create a unique temporary file in the desired location and with the desired permissions. You can then use the stat command to check the status of the file, and the rm command to remove it when it is no longer needed.
How to create temporary files on linux that will automatically clean up after themselves no matter what?
I want to create a temporary file on linux while making sure that the file will disappear after my program has terminated, even if it got killed or someone performs a hard reboot in the wrong moment. Does tmpfile() handle all this for me?
@JarrodRoberson I’ll re-add the [race-condition] tag — this is all about the time window between open() and unlink() .
@JarrodRoberson: Uh, aren’t you talking about a deadlock? My definition of «race condition» is something like «there are some parallel processes and depending on how fast they are in their race, different things happen». Let’s look what the english wikipedia says: «A race condition or race hazard is a flaw in an electronic system or process whereby the output or result of the process is unexpectedly and critically dependent on the sequence or timing of other events.»
«A race condition occurs when a program doesn’t work as it’s supposed to because of an unexpected ordering of events that produces contention over the same resource.» I was assuming that from your comments your concern was specifically on a dead-lock which is a result of trying to remediate a race-condition ( contention of the shared resource ). It is still not clear what your concern is, calling tmpfile() and having the program exit abnormally before calling unlink() is the least of your worries if your application is really that fragile.
6 Answers 6
You seem pre-occupied with the idea that files might get left behind some how because of some race condition, I don’t see an explanation of why this is a concern.
«A race condition occurs when a program doesn’t work as it’s supposed to because of an unexpected ordering of events that produces contention over the same resource.»
I was assuming that from your comments on other answers your concern was specifically on a dead-lock which is a result of trying to remediate a race-condition ( contention of the shared resource ). It is still not clear what your concern is, calling tmpfile() and having the program exit abnormally before that function gets to call unlink() is the least of your worries if your application is really that fragile.
Given that there isn’t any mention of concurrency, threading or other processes sharing this file descriptor to this temp file, I still don’t see the possibility for a race condition, maybe the concept of an incomplete logical transaction, but that can be detected and cleaned up.
The correct way to make absolutely sure that any allocated file system resources are cleaned up is not solely on exit of an application but also also on start-up. All my server code, makes sure that everything is cleaned up from a previous run before it starts and makes itself available.
Put your temp files in a sub-dir in /tmp make sure your application cleans this sub-dir on startup and normal shutdown. You can wrap your app start up with a shell script that detects abnormal ( kill -9 ) shutdown based on PID existence and also does clean up activities.