Linux 将目录的标题设为 bash 中的日期?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7128392/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-05 05:47:26  来源:igfitidea点击:

Making the title of a directory the date in bash?

linuxbashshellubuntu-11.04

提问by Christian

I want to make the current date into the title of a directory in /home/chris/Downloadsby using mkdirand date -I

我想/home/chris/Downloads通过使用mkdir和使当前日期成为目录的标题date -I

I tried mkdir "date -I" that gets me a folder named"date -I" Without the quotes it gives the error

我试过 mkdir "date -I" 这让我得到一个名为"date -I"的文件夹没有引号它会给出错误

mkdir: invalid option -- 'I'

Trying to make it a variable next

试图让它成为下一个变量

date= date -I
mkdir -p $date

with the -poption, it looked good, but upon inspection, the folder wasn't created. removing -pgets me the error

使用该-p选项,它看起来不错,但经过​​检查,未创建文件夹。删除-p让我得到错误

mkdir: cannot create directory `/home/chris/Downloads/': File exists

and even pointing it to the entire path

甚至将它指向整个路径

date= date -I
mkdir "/home/chris/Downloads/$date"

gets me the same error as before

给我和以前一样的错误

It's not that the variable is empty, I echo'd it and the value is what I should expect, it seems to be that the value isn't substituted before the directory is created. What would be the way to get around this problem? I'm running Ubuntu 11.04 (Natty Narwhal) if that gives you any more info.

不是变量为空,我回显了它并且值是我应该期望的,似乎是在创建目录之前没有替换该值。解决这个问题的方法是什么?如果可以提供更多信息,我正在运行 Ubuntu 11.04 (Natty Narwhal)。

采纳答案by Thomas Berger

Your syntax is wrong:

你的语法错误:

mkdir -p /home/chris/downloads/$(date -I)

or

或者

mkdir -p /home/chris/downloads/`date -I`

will work

将工作

回答by Femi

Use this: backticks run the command instead of printing it out.

使用这个:反引号运行命令而不是打印出来。

mkdir `date -I`

回答by Nick

Can also try xargs (however, not sure if it's a good practice)

也可以尝试 xargs (但是,不确定这是否是一个好习惯)

date -I | xargs mkdir