Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to implement date and time operation in Linux

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/01 Report--

This article introduces you how to achieve date and time operation in Linux, the content is very detailed, interested friends can refer to, hope to be helpful to you.

How do I view the date and time of the system?

$date

Tue Oct 16 11:18:32 CST 2012

Date supports formatted output, such as

$date + "% r% n% a% b% d,% Y"

11:27:14 AM

Tue Oct 16, 2012

Displays the specified date and time

$date-d "+ 1 month"

Fri Nov 16 11:31:10 CST 2012

Show Calendar

$cal

October 2012

Su Mo Tu We Th Fr Sa

1 2 3 4 5 6

7 8 9 10 11 12 13

14 15 16 17 18 19 20

21 22 23 24 25 26 27

28 29 30 31

How do I modify the system date and time?

Use the-s argument of the date command.

# date-s 12 date-s 14:15:00

Or

Date-s' 2011-12-4 1414 151500'

How do I view the hardware clock?

# hwclock-show

Tue 16 Oct 2012 09:19:57 PM CST-0.844514 seconds

How do I set the hardware clock?

# hwclock-set-date= "09 swap 17 Universe 13:26:00"

# hwclock-hctosys / / synchronize the hardware clock with the system clock

# hwclock-systohc / / synchronize the system clock with the hardware clock

The hardware clock can also be viewed and set through the clock command.

Time zone

UTC Universal Time Coordinated, World Standard time

GMT Greenwich Mean Time, GMT

CST China standard Time, China Standard time

For example, set the time zone to CST

Cp / usr/share/zoneinfo/Asia/Shanghai / etc/localtime

NTP

Network Time Protocol (NTP) is a protocol used to synchronize computer time. It enables computers to synchronize their servers or clock sources (such as quartz clock, GPS, etc.). It can provide high-precision time correction and prevent malicious protocol attacks by means of encryption confirmation.

Official website: http://www.pool.ntp.org/en/

Ntpdate synchronization can be used and crontab can be added to the server

# ntpdate cn.pool.ntp.org

Use the time command to get the command execution time

$time ls-Rl dir/*

[...]

Real 0m22.156s

User 0m1.652s

The concept of time in sys 0m4.772sLinux files

(1) modification time (mtime, modification time) is the time when the content of the file is modified. This is the time displayed by default with the command ls-l.

(2) status time (ctime, status time): when the state of a file changes, the time will change, such as changing the permissions and properties of the file, it will change.

(3) access time (atime, access time): this time will be changed when the contents of the file are read, for example, using cat to read / etc/man.config, then the atime of the file will change.

You can view it using ls-l-time = atime-full-time and ls-l-time = ctime-full-time. You can also use the stat command to view it.

$stat README.md

File: `README.md'

Size: 25 Blocks: 8 IO Block: 4096 regular file

Device: 809h/2057d Inode: 2755432 Links: 1

Access: (0664) Uid: (1000 / diglike) Gid: (1000 / diglike)

Access: 2012-09-21 1514 3614 42.238294913 + 0800

Modify: 2012-09-21 15 purl 35 purl 31.626295155 + 0800

Change: 2012-09-21 15 purl 35 purl 31.750295154 + 0800

Birth:-

$ls-full-time README.md

-rw-rw-r- 1 diglike diglike 25 2012-09-21 15 purl 35 purl 31.626295155 + 0800 README.md

$ls-time=atime-full-time README.md

-rw-rw-r- 1 diglike diglike 25 2012-09-21 15 purl 36 README.md 42.238294913 + 0800

$ls-time=ctime-full-time README.md

-rw-rw-r- 1 diglike diglike 25 2012-09-21 15 README.md 35 purl 31.750295154 + 0800

How to modify the access time and modification time of Linux files?

You can use the touch command to modify it.

The-a parameter only modifies atime

The-m parameter only modifies mtime

The-c parameter does not create a file

-t [[CC] YY] MMDDhhmm [.ss] modified to specified time

Such as:

Touch-t 1210011224.30 abc

Touch-d "5 days ago" abc

Customize the date and time format of ls output

Each Linux system may be different and is controlled by the TIME_STYLE environment variable. The following is the default output format for Ubuntu 12.04:

$ll README.md

-rw-rw-r- 1 diglike diglike 25 Sep 21 15:35 README.md

I modified it and displayed it as

$ll README.md

-rw-rw-r- 1 diglike diglike 25 2012-09-21 15:35 README.md

Because I added export TIME_STYLE=long-iso to .bashrc

Can also be customized, such as

$export TIME_STYLE= "+ Y-%m-%d H:%M:%S z"

On how to achieve date and time operations in Linux to share here, I hope that the above content can be of some help to you, can learn more knowledge. If you think the article is good, you can share it for more people to see.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report