Set tmp dir linux

Create temporary directory on Linux with Bash using mktemp

When writing a test or whenn running a build job it is usually a good practice to use a temporary directory and then clean up after the process is done.

It is also a good practice to make sure the temporary directoy is unique so if two processes run at the same time they won’t interfere.

In Linux one usually has a directory called /tmp to store temporary files. In most of the programming languages there is some tool to create temporary directories. Sometimes these also handle the removal of these directories once they are not needed any more.

In Unix/Linux shell we can use the mktemp commmand to create a temporary directory inside the /tmp directory.

The -d flag instructs the command to create the directory.

The -t flag allows us to provide a template. Each X character will be replaced by a random character. The more such characters the better your chances of having a unique directory. The rest of the template can and should be use to make it clear what is the purpose of the directory. In the above example this directory was created for and by the CI system.

The command will print the directory to the screen, but you can capture it to a variable and you can use it to create your files in this temporary directory.

tmp_dir=$(mktemp -d -t ci-XXXXXXXXXX)

At the end of the process you will probably want to remove the directory.

#!/bin/bash tmp_dir=$(mktemp -d -t ci-XXXXXXXXXX) echo $tmp_dir # . rm -rf $tmp_dir

It will print the name of the directory that will look like this:

Date based directory

It can be also a good idea to include the date in the name of the directory. Especially if we might keep it for later observation. In any case here is an example on how to generate a temporary directory that has a fixed part, a date-based part, and a random part:

#!/bin/bash tmp_dir=$(mktemp -d -t ci-$(date +%Y-%m-%d-%H-%M-%S)-XXXXXXXXXX) echo $tmp_dir rm -rf $tmp_dir

The directory name will look like this:

/tmp/ci-2019-01-11-11-48-34-KdQBLLObFw

Other root directory

By default the temporary directory will be created inside the /tmp directory. Sometimes we might want it to be in some other directory. For example I’ve created a tmp directory in my home directory and wanted to place the temporary directories inside that directory:

#!/bin/bash tmp_dir=$(mktemp -d -t ci-XXXXXXXXXX --tmpdir=/home/gabor/tmp) echo $tmp_dir rm -rf $tmp_dir

The path to the new directory will look like this:

For further options you might want to check out the documentation of mktemp.

Prev Next

Gabor Szabo

If you have any comments or questions, feel free to post them on the source of this page in GitHub. Source on GitHub. Comment on this post

Author: Gabor Szabo

Gábor who writes the articles of the Code Maven site offers courses in in the subjects that are discussed on this web site.

Читайте также:  Linux commands in fortran

Gábor helps companies set up test automation, CI/CD Continuous Integration and Continuous Delivery and other DevOps related systems. Gabor can help your team improve the development speed and reduce the risk of bugs.

He is also the author of a number of eBooks.

Contact Gabor if you’d like to hire his services.

If you would like to support his freely available work, you can do it via Patreon, GitHub, or PayPal.

Источник

Create temporary directory on Linux with Bash using mktemp

When writing a test or whenn running a build job it is usually a good practice to use a temporary directory and then clean up after the process is done.

It is also a good practice to make sure the temporary directoy is unique so if two processes run at the same time they won’t interfere.

In Linux one usually has a directory called /tmp to store temporary files. In most of the programming languages there is some tool to create temporary directories. Sometimes these also handle the removal of these directories once they are not needed any more.

In Unix/Linux shell we can use the mktemp commmand to create a temporary directory inside the /tmp directory.

The -d flag instructs the command to create the directory.

The -t flag allows us to provide a template. Each X character will be replaced by a random character. The more such characters the better your chances of having a unique directory. The rest of the template can and should be use to make it clear what is the purpose of the directory. In the above example this directory was created for and by the CI system.

The command will print the directory to the screen, but you can capture it to a variable and you can use it to create your files in this temporary directory.

tmp_dir=$(mktemp -d -t ci-XXXXXXXXXX)

At the end of the process you will probably want to remove the directory.

#!/bin/bash tmp_dir=$(mktemp -d -t ci-XXXXXXXXXX) echo $tmp_dir # . rm -rf $tmp_dir

It will print the name of the directory that will look like this:

Date based directory

It can be also a good idea to include the date in the name of the directory. Especially if we might keep it for later observation. In any case here is an example on how to generate a temporary directory that has a fixed part, a date-based part, and a random part:

#!/bin/bash tmp_dir=$(mktemp -d -t ci-$(date +%Y-%m-%d-%H-%M-%S)-XXXXXXXXXX) echo $tmp_dir rm -rf $tmp_dir

The directory name will look like this:

/tmp/ci-2019-01-11-11-48-34-KdQBLLObFw

Other root directory

By default the temporary directory will be created inside the /tmp directory. Sometimes we might want it to be in some other directory. For example I’ve created a tmp directory in my home directory and wanted to place the temporary directories inside that directory:

#!/bin/bash tmp_dir=$(mktemp -d -t ci-XXXXXXXXXX --tmpdir=/home/gabor/tmp) echo $tmp_dir rm -rf $tmp_dir

The path to the new directory will look like this:

For further options you might want to check out the documentation of mktemp.

Prev Next

Gabor Szabo

If you have any comments or questions, feel free to post them on the source of this page in GitHub. Source on GitHub. Comment on this post

Читайте также:  Создать загрузочный диск linux mint

Author: Gabor Szabo

Gábor who writes the articles of the Code Maven site offers courses in in the subjects that are discussed on this web site.

Gábor helps companies set up test automation, CI/CD Continuous Integration and Continuous Delivery and other DevOps related systems. Gabor can help your team improve the development speed and reduce the risk of bugs.

He is also the author of a number of eBooks.

Contact Gabor if you’d like to hire his services.

If you would like to support his freely available work, you can do it via Patreon, GitHub, or PayPal.

Источник

How to create a temporary directory?

The problem is, when it runs to the , there happens to be another program wants to do the same thing, which mkdir-ed a temp dir with the same name, will cause the failure of this program.

6 Answers 6

Use mktemp -d . It creates a temporary directory with a random name and makes sure that file doesn’t already exist. You need to remember to delete the directory after using it though.

This is a OS X vs Linux thing. See this question for a version that works on both: unix.stackexchange.com/questions/30091/…

Why do you say «You need to remember to delete the directory after using it though.»? Doesn’t that kinda defeat the purpose of using a temp directory?

@M.K.Safi I think the main purpose is to create an empty directory where you can store temporary files without cluttering up other parts of the filesystem. You get to choose if/when you want to delete it. If it creates it in /tmp, then it should be cleared when the system restarts anyway.

For a more robust solution i use something like the following. That way the temp dir will always be deleted after the script exits.

The cleanup function is executed on the EXIT signal. That guarantees that the cleanup function is always called, even if the script aborts somewhere.

#!/bin/bash # the directory of the script DIR="$( cd "$( dirname "$" )" && pwd )" # the temp directory used, within $DIR # omit the -p parameter to create a temporal directory in the default location WORK_DIR=`mktemp -d -p "$DIR"` # check if tmp dir was created if [[ ! "$WORK_DIR" || ! -d "$WORK_DIR" ]]; then echo "Could not create temp dir" exit 1 fi # deletes the temp directory function cleanup < rm -rf "$WORK_DIR" echo "Deleted temp working directory $WORK_DIR" ># register the cleanup function to be called on the EXIT signal trap cleanup EXIT # implementation of script starts here . 

Directory of bash script from here.

FreeBSD Caution! mktemp on FreeBSD doesn’t have -p option, and cleanup will rm -rf your current directory!

@madfriend really? if mktemp fails, WORK_DIR will be empty, meaning the command would just be rm -rf with no argument. I don’t use FreeBSD but I’d be pretty surprised if rm -rf was equivalent to rm -rf .

@jbg yes, it seems odd to me now too — it shouldn’t be a really big problem. I might have tweaked an old version of this script so that a path to temporary directory was calculated relatively to current directory, resulting in extinction of mankind current directory removal.

To make it better, you can avoid an empty directory or at least contain the problem within a directory using a solution where you do: 1. TMPWORKDIR=$(basename ‘mktemp -d -p /tmp/git/’) and then 2. rmdir /tmp/git/»$» . If the variable is empty now, you will still fall back to /tmp/git/ not to the whole system. Consider something like this in the answer and I’ll gladly agree. 😉

My favorite one-liner for this is

@piggybox Frankly, I’d be very cautious of using rm -r $(pwd) . Consider the possibility that temporary directory creation fails for whatever reason (maybe the /tmp filesystem is full or has been remounted read only due to an error?); then cd $(mktemp -d) will evaluate to cd which changes to the user’s home directory, which would subsequently be deleted.

The following snippet will safely create and then clean up a temporary directory.

The first trap line executes exit 1 command when any of the specified signals is received. The second trap line removes the $TEMPD on program’s exit (both normal and abnormal). We initialize these traps after we check that mkdir -d succeeded to avoid accidentally executing the exit trap with $TEMPD in an unknown state.

#!/bin/bash # set -x # un-comment to see what's going on when you run the script # Create a temporary directory and store its name in a variable. TEMPD=$(mktemp -d) # Exit if the temp directory wasn't created successfully. if [ ! -e "$TEMPD" ]; then >&2 echo "Failed to create temp directory" exit 1 fi # Make sure the temp directory gets removed on script exit. trap "exit 1" HUP INT PIPE QUIT TERM trap 'rm -rf "$TEMPD"' EXIT 

While this is an interesting solution for the error handling, a bit more explanation of the advantages and possible shortcomings would be nice.

Don’t suggest TMPDIR as variable name here, as that is typically the system’s temporary directory. en.wikipedia.org/wiki/TMPDIR

Here is a simple explanation about how to create a temp dir using templates.

  1. Creates a temporary file or directory, safely, and prints its name.
  2. TEMPLATE must contain at least 3 consecutive ‘X’s in last component.
  3. If TEMPLATE is not specified, it will use tmp.XXXXXXXXXX
  4. directories created are u+rwx, minus umask restrictions.
 PARENT_DIR=./temp_dirs # (optional) specify a dir for your tempdirs mkdir $PARENT_DIR TEMPLATE_PREFIX='tmp' # prefix of your new tempdir template TEMPLATE_RANDOM='XXXX' # Increase the Xs for more random characters TEMPLATE=$/$.$ # create the tempdir using your custom $TEMPLATE, which may include # a path such as a parent dir, and assign the new path to a var NEW_TEMP_DIR_PATH=$(mktemp -d $TEMPLATE) echo $NEW_TEMP_DIR_PATH # create the tempdir in parent dir, using default template # 'tmp.XXXXXXXXXX' and assign the new path to a var NEW_TEMP_DIR_PATH=$(mktemp -p $PARENT_DIR) echo $NEW_TEMP_DIR_PATH # create a tempdir in your systems default tmp path e.g. /tmp # using the default template 'tmp.XXXXXXXXXX' and assign path to var NEW_TEMP_DIR_PATH=$(mktemp -d) echo $NEW_TEMP_DIR_PATH # Do whatever you want with your generated temp dir and var holding its path 

Источник

Оцените статью
Adblock
detector