Book Image

Linux Shell Scripting Essentials

Book Image

Linux Shell Scripting Essentials

Overview of this book

Table of Contents (15 chapters)
Linux Shell Scripting Essentials
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Temporary files


Temporary files are the files that are needed for a short interval of time while an application is running. Such files are being used to keep intermediate results of running a program and they are no longer needed after the program execution is complete. In shell, we can create temporary files using the mktemp command.

Creating a temporary file using mktemp

The mktemp command creates a temporary file and prints its name on stdout. Temporary files are created by default in the /tmp directory.

The syntax of creating a temporary file is as follows:

$ mktmp
/tmp/tmp.xEXXxYeRcF

A file with the name tmp.xEXXxYeRcF gets created into the /tmp directory. We can further read and write into this file in an application for temporary use. Using the mktemp command instead of using a random name for a temporary filename avoids accidental overwrite of an existing temporary file.

To create a temporary directory, we can use the -d option with mktemp:

$ temp_dir=mktemp -d
$ echo $temp_dir
/tmp...