如何创建临时目录?

时间:2022-04-09 04:06:15

I use to create a tempfile, delete it and recreate it as a directory:

我用来创建一个tempfile,删除它,重新创建为一个目录:

tmpnam=`tempfile`
rm -f $tmpnam
mkdir "$tmpnam"

The problem is, another process may get a same name X, if it accidently executes tempfile after one process rm -f X and just before mkdir X.

问题是,另一个进程可能会得到一个相同的名称X,如果它在一个进程rm -f X和mkdir X之前意外地执行tempfile。

3 个解决方案

#1


271  

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.

使用mktemp - d。它创建一个具有随机名称的临时目录,并确保该文件不存在。不过,您需要记住在使用该目录之后要删除它。

#2


49  

My favorite one-liner for this is

我最喜欢的一句话是

cd $(mktemp -d)

#3


46  

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

为了获得更健壮的解决方案,我使用以下方法。这样,在脚本退出之后,temp dir总是会被删除。

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 "${BASH_SOURCE[0]}" )" && 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.

来自这里的bash脚本目录。

Bash traps.

Bash陷阱。

#1


271  

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.

使用mktemp - d。它创建一个具有随机名称的临时目录,并确保该文件不存在。不过,您需要记住在使用该目录之后要删除它。

#2


49  

My favorite one-liner for this is

我最喜欢的一句话是

cd $(mktemp -d)

#3


46  

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

为了获得更健壮的解决方案,我使用以下方法。这样,在脚本退出之后,temp dir总是会被删除。

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 "${BASH_SOURCE[0]}" )" && 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.

来自这里的bash脚本目录。

Bash traps.

Bash陷阱。