如果需要,如何在一行中触摸文件和mkdir [英] How to touch a file and mkdir if needed in one line
本文介绍了如果需要,如何在一行中触摸文件和mkdir的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要触摸一个具有绝对文件名的文件,例如:/opt/test/test.txt,但是我不确定系统上是否存在/opt/test.因此,代码应与此类似:
I need to touch a file with an absolute file name such as: /opt/test/test.txt, but I'm not sure if there is /opt/test existed on the system. So the code should similar with this:
if (-d '/opt/test') {
touch '/opt/test/test.txt';
} else {
mkdir -p '/opt/test';
touch '/opt/test/test.txt'
}
有没有更好的方法来简化代码?我希望有些系统命令只用一行就可以完成相同的工作.
Is there any better way to simplify the code? I hope there is some system commands that can do the same job with only one line.
推荐答案
在perl中,使用我最喜欢的模块之一:路径::小.
In perl, using one of my favorite module: Path::Tiny.
path("/opt/test/test.txt")->touchpath;
来自文档:
组合mkpath和touch.如果不创建父目录 存在,然后再触摸文件.
Combines mkpath and touch. Creates the parent directory if it doesn't exist, before touching the file.
这篇关于如果需要,如何在一行中触摸文件和mkdir的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文