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
How can I get the current date and time in the terminal and set a custom command in the terminal for it?
提问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 --help
for a list.
要自定义输出,有许多可用选项,请参阅date --help
参考资料。
For example, date '+%A %W %Y %X'
gives Tuesday 34 2013 08:04:22
which 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 date
to get time and date of a day:
您可以使用date
获取一天的时间和日期:
[pengyu@GLaDOS ~]$date
Tue Aug 27 15:01:27 CST 2013
Also hwclock
would 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 date
to 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)中,以使其可在任何地方调用。