Skip to content Skip to content


The Linux 'date' command Linux "diena" komanda

Linux

A command line tool that I find use for very frequently on Linux is the “date” command. Komandrindas rīks, kas man izmantot ļoti bieži uz Linux "date" komandu. It's a pretty simple command that returns you the date and time when you call it without any options. Tas ir diezgan vienkāršs komanda, kas atgriež jūsu datumu un laiku, kad tu to sauc par bez iespējas. But when you begin to use some of the options it provides you with this tool can make your life much easier and fun. Bet, kad jūs sākat izmantot dažas iespējas, ko tas sniedz jums ar šo instrumentu var padarīt Jūsu dzīvi daudz vieglāku un jautri. Let's take a closer look at the command itself and some of its options and the cool tricks that we can make it do. Let's to tuvāk apskatīt komandu pats un daži tās iespējām un vēsu trikus, ka mēs varam to darīt.

The first thing to do is to get the current date and time: Pirmais, kas jādara, ir iegūt pašreizējais datums un laiks:

# date # Datuma
Sun Dec 14 11:33:55 IST 2008 Saule decembris 14 11:33:55 IST 2.008

This is the simplest use of this command. Tas ir vienkāršākais izmantot šo komandu. Now suppose you wanted to just get the date and nothing more: Tagad pieņemsim, ka jūs vēlētos, lai tikai saņemtu datumu un nekas vairāk:

# date +”%d” # Datumu + "% d"
14 14

If you want the date, complete with date, month, and year: Ja jūs vēlaties datumu, komplektā ar datumu, mēnesi un gadu:

# date +”%d%m%y” # Datumu + "% d% m% y"
141208 141208

To get the day of the week along with the rest of the date: Lai saņemtu dienas nedēļā kopā ar pārējo datums:

# date +”%a%d%m%y” # Datumu + "% a% d% m% y"
Sun141208 Sun141208

These are a few of the many possibilities that the “date” command offers you. Šie ir daži no daudz iespēju, ka "date" komanda piedāvā Jums. Check out “date –help for options”. Check out "date-help opcijas". Some interesting ones are: Daži interesanti tiem ir:

%D   date (mm/dd/yy) % D datums (mm / dd / gg)
%d   day of month (01..31) % d dienu (01 .. 31 mēneša laikā)
%m   month (01..12) % m mēnesī (01 .. 12)
%y   last two digits of year (00..99) % y pēdējie divi cipari (00 .. 99 gadu)
%a   locale's abbreviated weekday name (Sun..Sat) % locale ir saīsinātā darba diena vārds (Sun.. Sat)
%A   locale's full weekday name, variable length (Sunday..Saturday) % Locale pilnu darba dienu vārds, mainīga garuma (Sunday.. Sestdiena)
%b   locale's abbreviated month name (Jan..Dec) % b locale ir saīsināts mēneša nosaukums (janvāris dec)
%B   locale's full month name, variable length (January..December) % B locale pilns mēneša nosaukums, mainīga garuma (January.. Decembrim)
%H   hour (00..23) % H stunda (00 .. 23)
%I   hour (01..12) % I stundā (01 .. 12)
%Y   year (1970…) % Y gads (1970 ...)

You can also do some fancy formatting. Jūs varat arī veikt dažas fancy formatējumu. If you want to add a hyphen or a back-slash in between the different parts of the date: Ja jūs vēlaties pievienot defisi vai back-slīpsvītra starp dažādām datums:

# date +”%d-%m-%Y” # Datumu + "% d-% m-% Y"
14-12-2008 14-12-2008

# date +”%d/%m/%Y” # Datumu + "% d /% m /% Y"
14/12/2008 14/12/2008

You can also use spaces and commas. Varat izmantot arī telpas un komatiem. Here's a pretty fancy example: Šeit ir diezgan iedomātā piemērs:

# date +”%A,%B %d %Y” # Datumu + "% A,% B% d% Y"
Sunday,December 14 2008 Svētdiena, decembris 14, 2008

Say you're writing a shell script to back up the logs form you server. Say jūs rakstiski shell skriptu, lai dubl apaļkoku veidlapu Jūs serveri. You want the backup script to get the logs for the day before and back them up. Jūs vēlaties rezerves skriptu, lai saņemtu par dienu logs pirms un atbalstīt tos. Here's how you can get the previous day's date: Lūk, kā jūs varat iegūt iepriekšējās dienas datums:

# date –date=”yesterday” # Datums-date = "vakar"
Sat Dec 13 12:04:03 IST 2008 Sestdiena 13 decembris 12:04:03 IST 2.008

Similarly, you can also get tomorrow's date: Līdzīgi varat iegūt arī rītdienas datums:

# date –date=”tomorrow” # Datums-date = "rīt"
Mon Dec 15 12:04:39 IST 2008 Pirm 15 decembris 12:04:39 IST 2.008

You can get last or next month's date: Jūs varat saņemt pēdējā vai nākamā mēneša datums:

# date –date=”last month” # Datums-date = "pēdējo mēnešu"
Fri Nov 14 12:06:23 IST 2008 Piekt 14 novembris 12:06:23 IST 2.008

# date –date=”next month” # Datums-date = "Nākamais mēnesis"
Wed Jan 14 12:06:25 IST 2009 Treš 14 janvāris 12:06:25 IST 2.009

Pretty cool stuff, eh! Pretty cool stuff, eh! You're just getting started. Tu esi tikai iesākumam. You can customize the date you want more precisely than by day and week, and get the date five days ago or seven days from now: Jūs varat pielāgot datumu Jūs vēlaties precīzāk nekā dienā un nedēļā un saņemt datumam piecas dienas pirms vai septiņas dienas no now:

# date –date=”5 days ago” # Datums-date = "5 dienas"
Tue Dec  9 12:08:26 IST 2008 Otr 9 decembris 12:08:26 IST 2.008

# date –date=”7 days” # Datums-date = "7 dienas"
Fri Dec 21 12:09:23 IST 2008 Fri 21 decembris 12:09:23 IST 2.008

You can even ask it more difficult questions such as: Jūs pat varat uzdot grūtāk tādus jautājumus kā:

# date –date='next Saturday' # Datums-date = 'nākamais Sestdiena'
Sat Dec 20 00:00:00 IST 2008 Sestdiena 20 decembris 00:00:00 IST 2.008

Or something totally in the future: Vai kaut ko pilnīgi kas nākotnē:

# date –date='2 years 3 months 4 day' # Datums-date = '2 gadi 3 mēneši 4 dienas "
Fri Mar 18 12:12:16 IST 2011 Fri 18 marts 12:12:16 IST 2.011

It's almost as if “date” speaks English. Tas ir gandrīz kā "date", runā angļu valodā. I keep discovering new tricks. Es turpinu atklāt jaunus trikus. So play around with it and let me know if you find some new cool possibilities that I might not have discovered yet. Tik spēlē ap to un dariet man zināmu, ja jums atrast kādu jaunu atdzist iespējas, ka es varētu nebūt atklāta yet.

Posted in Iesūtīts Guest Blogger Viesu Blogger , , Linux Linux . .

Related Posts: Related Posts:

How to set the date on your Linux machine Kā noteikt datumu jūsu Linux mašīna
How to save the output of a Linux command to a file Kā lai saglabātu jaudu Linux komandu failu
How to post to Twitter from the Linux command line Kā izlikt čivināt no Linux komandrindas
How to see a detailed history of the commands run in Linux Kā lai redzētu detalizētu vēsturi komandas palaist Linux
How to download files from the Linux command line Kā lejupielādēt failus no Linux komandrindas

One Response One Response

Stay in touch with the conversation, subscribe to the Uzturēt kontaktus ar sarunu, abonēt RSS feed for comments on this post RSS barību komentārus par šo ziņu . .

  1. Vivek says Vivek saka

    Good explanation. Labs izskaidrojums. The -date option is actually with double-hyphen Jaunāko iespēja ir ar divkāršu domuzīmi
    # date –date='next Saturday' # Datums-date = 'nākamais Sestdiena'
    should be Būtu
    # date –date='next Saturday' # Datums-date = 'nākamais Sestdiena'



Some HTML is OK Daži HTML ir OK

or, reply to this post via vai, atbildot uz šo ziņu izmantojot trackback Trackback . .