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 does Shell get the current date and time and disk status?

2025-04-05 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article mainly introduces Shell how to obtain the current date and time and disk to make the relevant knowledge, the content is detailed and easy to understand, the operation is simple and fast, has a certain reference value, I believe that after reading this Shell how to get the current date and time and disk so that the article will have a harvest, let's take a look at it.

Analysis.

There are two core knowledge points:

1. How to automatically represent the date of the day 2. Disk usage

The command to print the date is date. The example command is as follows:

# date Wednesday, December 20, 2017, 16:26:55 CST

The format required in the title should be: the 2017-12-20 drawing date command has this function. The example command is as follows:

# date +% Y-%m-%d 2017-12-20 or # date +% F 2017-12-20

Disk usage is implemented with the command df-h. The example command is as follows:

# df-h File system capacity available available mount point / dev/vda1 99G 1.8G 92G 2% / devtmpfs 911M 0911M 0% / devtmpfs 920M 0920m 0% / dev/shm tmpfs 920M 336K 920m 1% / run tmpfs 920m 0920m 0% / sys/fs/cgroup tmpfs 184M 0184m 0% / run/user/0 exercise answer

With the above analysis, we finally get the answer to this question:

Answer analysis

Assign the date of the day to the variable d to define the daily log file name, and finally enter the results of disk usage directly into the log. The > here is special. It can write the result on the left side of the symbol to the file on the right side of the symbol.

Expand knowledge points

\ 1. Backquotes in shell can indicate the result of a command and usually assign a value to a variable. The example command is as follows:

# n = `wc-l / etc/passwd | awk'{print $1}'`# echo $n 23

\ 2. There are many other uses of the date command. Examples are as follows:

# date +% H # hr 16 # date +% M # min 38 # date +% S # s 55 # date +% T # time 16:39:31 # date +% w # Wednesday 3 # date-d "- 1 day" +% F # one day ago 2017-12-19

\ 3. > in order to redirect correctly, when we run a command, there are both correct and wrong output information. > the correct output information is written to the specified file, which is accompanied by an error redirection symbol 2 >. As the name implies, it writes the error information to the specified file. Examples are as follows:

# ls / etc/passwd / etc/nofile # # where / etc/nofile does not exist So ls: unable to access / etc/nofile: no file or directory / etc/passwd # ls / etc/passwd / etc/nofile > / tmp/log 2 > / tmp/error # cat / tmp/log / etc/passwd # cat / tmp/error ls: unable to access / etc/nofile: without that file or directory, this is the end of the article on "how Shell gets the current date and time and disk usage" Thank you for reading! I believe you all have a certain understanding of "how to get the current date and time and disk on Shell". If you want to learn more, you are welcome to follow the industry information channel.

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

Development

Wechat

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

12
Report