如何创建一个临时目录? [英] How to create a temporary directory?

查看:285
本文介绍了如何创建一个临时目录?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用来创建tempfile,将其删除并重新创建为目录:

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

temp=`tempfile`
rm -f $temp
  # <breakpoint>
mkdir $temp

问题是,当它运行到< breakpoint> 时,碰巧有另一个程序想要做同样的事情,其中​​ mkdir -是一个临时文件.具有相同名称的dir,将导致该程序失败.

The problem is, when it runs to the <breakpoint>, 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.

推荐答案

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

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.

这篇关于如何创建一个临时目录?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆