bash 如何在文件名中创建带有今天日期的文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/48270960/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to create a file with todays date in the filename
提问by HattrickNZ
Command 1:
命令 1:
$ touch test"date"
Command 2:
命令 2:
$ date +"%F"
2018-01-16
I want to be able to run the command so the file test_2018-01-16
is created. How do or can I combine the 2 commands above to do this?
我希望能够运行命令以便test_2018-01-16
创建文件。如何或我可以结合上面的 2 个命令来做到这一点?
$ touch test_"date"
EDIT1 - Answer
EDIT1 - 答案
tks
tks
these commands
这些命令
touch fred-`date +%F`
touch "test-$(date +%F)"
touch "test2_$(date +"%F %T")"
prduce the following files respectively
分别生成以下文件
fred-2018-01-16
test-2018-01-16
test2_2018-01-16 11:51:53
回答by iamauser
You should use double quotes and need to evaluate date +"%F"
using command substitution.
您应该使用双引号并需要date +"%F"
使用命令替换进行评估。
$ touch "test_$(date +%F)"
This will create an empty file test_2018-01-15
这将创建一个空文件 test_2018-01-15
Double quote helps you create a single file where some options of date
command would include a space.
双引号可帮助您创建单个文件,其中某些date
命令选项将包含空格。
For example, touch test_$(date)
will create multiple files, where as touch "test_$(date)"
won't.
例如,touch test_$(date)
将创建多个文件,而touch "test_$(date)"
不会。
As pointed out by OP, one would need additional quotes "
around the format options, when multiple of them are used:
正如 OP 所指出的"
,当使用多个格式选项时,需要在格式选项周围加上额外的引号:
touch "test_$(date +"%F %T")"
回答by Mike Tubby
In my world (with Bash) its:
在我的世界中(使用 Bash):
touch fred-`date +%F`
where 'fred-' is the prefix and teh date command provides the suffix
其中“fred-”是前缀,日期命令提供后缀