I saw this command on a forum as a question for registering:

"date -u +%V$(uname)|sha256sum|sed 's/\W//g'"

I think it depends on the time of my PC which changes every second, but the output is still the same every time I try it. So, can someone please explain how this command works?

  • Note that while this uses the date command the output does not in fact change every second but every week specifically at 00:00 UTC on the Monday of each week. This is because of the specified format string in which the only date component actually being output is the ISO week number (%V). This is why trying the command repeatedly is giving the same output you would have to wait until the next week to see it change.
    – MttJocy
    Oct 31 '16 at 21:14

Let's break this into its constituent parts:

The date part

  • date -u : as explained in man date, the -u flag makes it print the Coordinated Universal Time

  • +%V : the data command takes a format argument which is given after a +. For example:

    $ date +%m/%d/%y

    See man date for a list of all the formats it can produce. The %V means:

    %V ISO week number, with Monday as first day of week (01..53)

    So, this week it will return:

    $ date +%V

    And next week it will return 19.

  • $(uname) : this will simply print the result of the uname command which should be Linux on a Linux system.

So, putting all that together, the date command will print WeekNumberLinux:

$ date -u +%V$(uname)

The sha256sum part

sha256sum simply takes an input string and applies a hashing function to it. This produces a long string that is unique to the input given:

$ date -u +%V$(uname) | sha256sum 
9a93b71d5b2841e649195bc81f8e38600860a040bc368cdbd0cde346b73248a1  -

The sed part

The sed commmand uses the susbtitution operator (s/pattern/replacment/) which will substitute pattern with replacement. In this case, the pattern is \W which means "All non-word characters", meaning anything that's not [A-Za-z0-9_], not a letter, not a number and not an underscore. This is used to remove the trailing space and - from the output of sha256sum.

So, the command is a simple way of making sure you are not a robot. Since the date is printing the current week, your output will always be the same until next week when it will change.

date -u +%V$(uname)|sha256sum|sed 's/\W//g'

Removes all the non word characters from the sha256sum of date -u +%V$(uname) command's output.

avinash@avinash-Lenovo-IdeaPad-Z500:~$ date -u +%V$(uname)
avinash@avinash-Lenovo-IdeaPad-Z500:~$ date -u +%V$(uname)|sha256sum
9a93b71d5b2841e649195bc81f8e38600860a040bc368cdbd0cde346b73248a1  -
avinash@avinash-Lenovo-IdeaPad-Z500:~$ date -u +%V$(uname)|sha256sum|sed 's/\W//g'

From man date

%V -  ISO week number, with Monday as first day of week (01..53)
-u, --utc, --universal    print or set Coordinated Universal Time
  • i know that but why the output is the same
    – Fat Mind
    May 4 '14 at 12:41
  • what is the job of +%V
    – Fat Mind
    May 4 '14 at 12:41
  • ok many thanks , but what are the meaning of that part after sed
    – Fat Mind
    May 4 '14 at 12:54
  • you mean i have to explain the sed part. May 4 '14 at 12:55
  • 1
    s/\W//g- searches for the non word characters, if it found any then it replaces the non word characters with null(empty but not a space) and this will apply to all the lines because the global flag g is set. May 4 '14 at 12:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.