Linux date 命令的格式技巧
该date
命令很简单。但是,它有几个有用的选项可以增强它的功能。它还能够为您提供有关过去或未来日期的实用信息。本文向您展示了一些用于操作命令date
输出的格式控件。在文章的最后,我提供了一些关于如何将此命令与常见任务结合使用的实用建议。
日期的基本语法
该命令的最基本语法date
是简单地输入命令,不带任何选项和格式控制。以下是该命令及其结果输出的示例:
该命令的一个修改date
是-u
选项。此选项将输出转换为协调世界时 (UTC)。以下是示例:
[ 你可能还喜欢: 在 Linux 中使用 at 执行一次性 cron 任务]
格式控件
当您开始添加格式控件时,命令的真正自定义date
才会发生。控件可以对输出字段进行排序、管理缩写、设置屏幕上或文件中的日期格式等。在文章的最后,我将展示一些如何使用的示例date
,您将看到控制格式是多么有用。
基本格式控制语法
使用一个或多个格式控件来显示信息。以下是一般语法:
# date +%<format-option>
我们来看几个例子。
标注输出字段
如果希望输出带标签,可以使用以下格式:
更改字段的顺序
您可以更改字段的显示顺序。这是最有用的自定义功能之一,date
因为它允许您以最有用或最熟悉的格式显示输出。以下是其中一种方法:
下面是另一个示例:
此示例反转了结果:
在字段之间使用破折号、斜线或空格
也许您需要格式化date
输出以满足特定标准,例如用破折号、斜线或空格字符分隔的日期信息。以下是几个不同的例子:
Display information from outside my current locale or time zone
It's embarrassing to say, but for whatever reason, time zones completely mess with my mind. They always have and they probably always will. That's why I was excited to discover that the date
command can save me from having to visualize the sun's position over a particular geographical location to figure out the appropriate time zone.
First, you must know the name of the time zone you wish to check. You can use the timedatectl list-timezones
command to display this information.
Next, combine the TZ value with the date
command to display the time zone's time and date information. To show the time on the east coast of the US, run this command:
Run the following command to display the time in Tokyo:
Display future time/date
What if you're coordinating a meeting with someone on the east coast of the US, and you want to confirm or display a particular future time? You can use the date
command to display that information.
First, you could display the date and time information for next Friday:
Here is an example that displays the local time for 10 AM next Friday on the east coast of the US:
Display past dates
You can also use date
to display past information.
This example shows time and date information from 15 days ago:
Practical application
It's one thing to know these tricks when using the date
command, but it's another to understand how to apply them. Here are a few straightforward scenarios to get you thinking about your own tasks where date
might be useful.
Redirect
You can redirect the date
command into a text file. You can use any of the format controls above to customize the output. For example, what if you are conducting a very simple server documentation project? You might use the following commands:
Set a date alias
Do you prefer the date and time to be displayed in a specific format different from the default? Set an alias for date
that shows the information the way you like it. You can do this in your ~/.bashrc
file.
Set the format as a variable
You can also set the date
configuration as a variable on your system.
Create a template file, then copy/paste the content into scripts depending on the format desired
Perhaps you create or manage various scripts, and the date
output is needed in several of them. However, the date
format must be different in each script. You could create a master template file that stores the required formats and then copy/paste the appropriate template into your new scripts.
[ A free course for you: Virtualization and Infrastructure Migration Technical Overview. ]
Wrap up
Like many things in Linux, the date
command is simple but also very flexible. You can control the format of the data that returned to you, ensuring you get the information you need as efficiently as possible. How have you used the date
command to make your life easier?