قالب وردپرس درنا توس
Home / Tips and Tricks / Displaying the Date and Time in the Linux Terminal (and Using in Bash Scripts)

Displaying the Date and Time in the Linux Terminal (and Using in Bash Scripts)



  Bash Shell on Unity Desktop Concept
Fatmawati Achmad Zaenuri / Shutterstock.com

The command date resides in the Bash shell, which is common in most Linux distributions and even MacOS is pre-set. This tutorial will show you how to use date on the command line and how to use it in shell scripts to print more than just the time.

Run the command date see this information. It outputs the current date and time for your time zone:

  date 

  date command output

The default formatting looks a bit silly. Why is not the year printed after the month and day, but displayed behind the time zone at the end? Do not be afraid: If you control the format of the desired output, date returns it in spades. There are more than 40 options that you can pass to date to get the instruction to format the output exactly as you like.

To use one of the options of type date a space, a plus sign + and the option including the leading percent sign. The option % c (date and time in the locale format) causes the date and time to be printed in the normalized format associated with the locale. Your locale will be determined by the geographic and cultural information you provided when you installed your operating system. The locale governs things like the currency symbol, paper sizes, time zones, and other cultural norms.

  Date +% c 

  Output date command with option c

The year now appears in a more natural position in the output.

You can pass multiple options to date at the same time. A sequence of options is called a format string. Use this option to display the name of the day (% A ), the day of the month (% d ), and the month name (% B ) command :

  Date +% A% d% B 

  Date command output with A d B options

That worked, but it's ugly. No problem, we can include spaces as long as we enclose the entire format string in quotation marks. Note that + is outside the quotation marks.

  Date + "% A% d% B" 

  Date command output with option A d B with spaces

You can add text to the format string, for example:

  Date + "Today is :% A% d% B "

  Data command output with user-added text [19659004] Scrolling up and down through the man page date in search of the desired option will soon be tedious. We've grouped the options together to help you find them.

Date and time options

  • % c : Prints the date and time in the format of your locale. including the time zone.

  Date command output

Date display options

  • % D : Prints the date in mm / dd / yy format.
  • % F : Prints date in the format YYYY-MM-DD.
  • % x : Prints the date in the format of your locale.

  Date command output with DF x options

Options for displaying the day [19659023]% a : Prints the name of the day, abbreviated to Mo, Di, Mi, and so on.
  • % A : Prints the full name of the day, Monday, Tuesday, Wednesday, etc. [19659027]% u : Returns the number of the day of the week, where Monday = 1, Tuesday = 2, Wednesday = 3, and so on.
  • % w : Returns the number of the day of the week, where sunday = 0, monday = 1, tuesday = 2, and so on.
  • % d : Prints the day of the month with a leading zero ( 01, 02 … 09), if necessary.
  • % e : Prints the day of the month with a leading space (& # 39; 1 & # 39; & # 39; 2 & # 39; & # 39; 9 & # 39;) if required , Note that the apostrophes are not printed.
  • % j : Prints the day of the year with up to two leading zeros, if necessary.
  •   Output of the date command with the options of A uwdej

    Options Display of the week

    • % U : Prints the week number of the year and considers Sunday as the first day of the week. For example, the third week of the year, the twentieth week of the year, and so on.
    • % V : Returns the ISO week number of the year, with Monday being the first day of the week.
    • % W : Week number of the year, with Monday being the first day of the week.

      Output of Date Command with UVW Options

    Options for Displaying the Month

    • % b or % h : Prints the name of the month with the abbreviation Jan, Feb, Mar, etc
    • % B : Prints the full name of the month, January, February, March, etc. [19659027]% m : Prints the month's number with a leading zero, if necessary, 01, 02, 03 … 12.

      Output of date command with options for Bh B m

    Display year options

    • % C : Prints the century without year. In 2019 it would print 20.
    • % y : Prints the year in double digits. 1919 will be printed in 2019.
    • % Y : Returns the year in four digits.

      Date command output with C y Y options

    Time display options

    • % T : Prints the time as HH: MM: SS.
    • % R : Returns the time Hours and minutes as HH: MM without seconds in 24-hour format off.
    • % r : Prints the time according to the locale based on the 12-hour clock and an AM or PM indicator.
    • % X : Prints the time in 24-hour format according to your locale. Allegedly Note that during the test, this option behaved exactly like % r as shown below. As expected, on a Linux computer configured for the UK locale and set to GMT, he printed the time in 24-hour format without AM or PM.

    <img class = "alignnone size-full wp-image-410505" data-pagespeed-lazy-src = "https://www.howtogeek.com/wp-content/uploads/2019/04/xHTG_011.png .pagespeed.gp + jp + jw + pj + ws + js + rj + rp + rw + ri + cp + md.ic.d0ZLZM1WIO.png "alt =" Output the date command with TR r X options [19659060] Options for Hour display

    • % H : Prints the hours 00, 01, 02 … 23.
    • % I : Prints the hours with the 12-hour clock 00, 01, 02 … 12, leading zero, if any.

     Date command output with HI options

    Minutes display options

    • % M : Prints the minutes, 01, 02, 03 … 59, with if necessary, a leading zero.

     Output of the date command with M options

    Options for displaying seconds

    • % s : Prints the number of seconds since 1970-01-01 00:00 : 00. the beginning of the Unix epoch.
    • % S : Prints the seconds 01, 02, 03 … 59 with a leading zero, fall s required.
    • % N : Prints the nanoseconds. [19659071] Output of the date command with s SN options [19659030] Options for displaying time zone information
      • % z : Prints the time difference between your time zone and your UTC time.
      • %: z : Prints the time difference between your time zone and your UTC time: between hours and minutes. Note the : between % and z .
      • % :: z : Prints the time difference between your time zone and UTC with a: between the hours, minutes, and seconds. Note the :: between % and z .
      • % Z : Outputs the alphabetical time zone name.

       Date range command with time zone options

      Formatting options

      • % p : Prints the AM or PM indicator in uppercase.
      • % P : The AM or PM indicator is printed in lowercase. Note the special features of these two options. A lowercase p uppercase letters, uppercase letters P are lowercase letters.
      • % t : Prints a tab.
      • % n : Prints a new line.

       Date command with AM PM indicator and formatting options

      Options for modifying other options

      These modifiers can be pasted between % and the option letter of other options to display them to change. For example, % - S would remove the leading zero for single-digit seconds.

      • : A single hyphen prevents zeros from single-digit values.
      • _ : A single underscore adds leading digits for single-digit values.
      • 0 : Represents leading zeros for single-digit values.
      • ^ : Use capital letters if possible (not all options take this modifier into consideration).
      • # : Try to use the opposite of the option's default setting (not all options apply to this modifier.)

       Date command output with formatting options

      Two more tricks [19659085] To get the last modification time of a file, use the option -r (reference). Note that here a - (hyphen) character is used instead of a % character and no + symbol is required. Try the following command in your home folder:

        date -r .bashrc 

        Date command output with file modification time option

      The TZ setting allows you to set your time zone for the duration of a file Single change command.

        TZ = GMT date +% c 

        Date command output for another time zone

      Using the date in scripts

      Enabling a bash shell script to print time and date is trivial. Create a text file with the following content and save it as gd.sh .

        #! / Bin / bash
      
      TODAY = $ (date + "Today is% A,% d of% B")
      TIMENOW = $ (date + "local time is% r")
      TIME_UK = $ (TZ = BST date + "UK time is% r")
      
      Echo $ TODAY
      echo $ TIMENOW
      echo $ TIME_UK 

      Enter the following command to set the execute permissions and make the script executable:

        chmod + x gd.sh 

      Run the script with this command:

        ./ gd.sh [19659102]   gd.sh script output  

      We can use the Date command to provide a timestamp. The displayed script creates a directory with the timestamp as name. All text files from the current folder will be copied to the folder. If you run this script regularly, we can take a snapshot of our text files. Over time, we build a bunch of folders with different versions of our text files.

      Note that this is not a robust backup system, just illustrative.

      Create a text file with the following content and save it as snapshot.sh

        #! / bin / bash
      
      # Date and time received
      date_stamp = $ (date + "% F-% H-% M-% S")
      
      # Create a directory with this name
      mkdir "$ date_stamp"
      
      # Copy the files from the current folder
      cp * .txt "$ date_stamp"
      
      # everything ready, report and finish
      echo "Text files copied to the directory:" $ date_stamp 

      Enter the following command to set the execute permissions and make the script executable.

        chmod + x snapshot.sh 

      Run the script with this command: [19659005] ./ snapshot.sh

       Impact of running the script snapshot.sh

      them see that a directory has been created. Its name is the date and time the script was run. This directory contains copies of the text files.

      With a little thought and creativity, even the modest date can be used productively.




    Source link