Linux 如何在终端中获取当前日期和时间并在终端中为其设置自定义命令?

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

How can I get the current date and time in the terminal and set a custom command in the terminal for it?

linuxcommand-lineterminal

提问by harshal

I have to check the time in a Linux terminal.

我必须在 Linux 终端中检查时间。

What is the command for getting date and time in a Linux terminal?

在 Linux 终端中获取日期和时间的命令是什么?

Is there a way in which we can set a custom function?

有没有办法设置自定义函数?

采纳答案by Vorsprung

The command is date

命令是 date

To customise the output there are a myriad of options available, see date --helpfor a list.

要自定义输出,有许多可用选项,请参阅date --help参考资料。

For example, date '+%A %W %Y %X'gives Tuesday 34 2013 08:04:22which is the name of the day of the week, the week number, the year and the time.

例如,date '+%A %W %Y %X'给出Tuesday 34 2013 08:04:22星期几的名称、星期数、年份和时间。

回答by starrify

You can use dateto get time and date of a day:

您可以使用date获取一天的时间和日期:

[pengyu@GLaDOS ~]$date
Tue Aug 27 15:01:27 CST 2013

Also hwclockwould do:

hwclock可以这样做:

[pengyu@GLaDOS ~]$hwclock
Tue 27 Aug 2013 03:01:29 PM CST  -0.516080 seconds

For customized output, you can either redirect the output of dateto something like awk, or write your own program to do that.

对于自定义输出,您可以将 的输出重定向date到类似的内容awk,或者编写自己的程序来执行此操作。

Remember to put your own executable scripts/binary into your PATH(e.g. /usr/bin) to make it invokable anywhere.

请记住将您自己的可执行脚本/二进制文件放入您的PATH(例如 /usr/bin)中,以使其可在任何地方调用。