Temporary file naming and random numbers
While writing shell scripts, we often need to store temporary data. The most suitable location to store temporary data is /tmp
(which will be cleaned out by the system on reboot). We can use two methods to generate standard filenames for temporary data.
How to do it...
Perform the following steps to create a temporary file and perform different naming operations on it:
Create a temporary file as follows:
$ filename=`mktemp` $ echo $filename /tmp/tmp.8xvhkjF5fH
This will create a temporary file and print its filename which we store in
$filename
in this example.To create a temporary directory, use the following commands:
$ dirname=`mktemp -d` $ echo $dirname tmp.NI8xzW7VRX
This will create a temporary directory and print its filename which we store in
$dirname
in this example.To just generate a filename without actually creating a file or directory, use this:
$ tmpfile=`mktemp -u` $ echo $tmpfile /tmp/tmp.RsGmilRpcT
Here, the filename will be stored in...