Linux:使用mktemp创建临时文件/目录

35 篇文章 0 订阅

mktemp的作用是创建临时文件/目录,并返回文件名,简单的语法如下:

NAME
       mktemp - create a temporary file or directory

SYNOPSIS
       mktemp [OPTION]... [TEMPLATE]

DESCRIPTION
       Create  a  temporary  file or directory, safely, and print its name.  TEMPLATE must contain at least 3 consecutive
       'X's in last component.  If TEMPLATE is not specified, use tmp.XXXXXXXXXX, and --tmpdir  is  implied.   Files  are
       created u+rw, and directories u+rwx, minus umask restrictions.

       -d, --directory create a directory, not a file
       -u, --dry-run do not create anything; merely print a name (unsafe)
       --suffix=SUFF append  SUFF to TEMPLATE.  SUFF must not contain slash.  This option is implied if TEMPLATE does not end in X.


示例1:创建临时文件   

qingsong@db2a:/tmp$ echo `mktemp`
/tmp/tmp.9Hs6fScHlN
qingsong@db2a:/tmp$ ls -l /tmp/tmp.9Hs6fScHlN 
-rw------- 1 qingsong qingsong 0 Aug 25 15:22 /tmp/tmp.9Hs6fScHlN

示例2:创建临时目录(-d)

qingsong@db2a:/tmp$ echo `mktemp -d`
/tmp/tmp.LY3ZvKPd6e
qingsong@db2a:/tmp$ ls -ld /tmp/tmp.LY3ZvKPd6e
drwx------ 2 qingsong qingsong 4096 Aug 25 15:23 /tmp/tmp.LY3ZvKPd6e

示例3:仅返回一个临时名子(-u),并不真正创建文件:

qingsong@db2a:/tmp$ echo `mktemp -u`
/tmp/tmp.30bTZuaVDw
qingsong@db2a:/tmp$ ls -l /tmp/tmp.30bTZuaVDw
ls: cannot access /tmp/tmp.30bTZuaVDw: No such file or directory

示例4:指定创建临时文件的模板:

qingsong@db2a:/tmp$ echo `mktemp db2icrt.XXXXXXX`
db2icrt.sB1GXz7
后面的X要求至少有3位

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值