1

As many of you know uname -r returns the kernel release while uname -v returns the version (build flags, date).

I'm trying to figure out a way to get an output of ONLY the release and the build date like

4.10.0-37-generic Fri Oct 6 22:42:22 UTC 2017

If I use uname -rv I get

4.10.0-37-generic #41~16.04.1-Ubuntu SMP Fri Oct 6 22:42:22 UTC 2017

So what I need here is to find a pattern to remove the #41~16.04.1-Ubuntu SMP. The only restrictions for a solution are:

  • it has to be executable in a single line
  • the locale of the system is US (or other English locale where the days of the week and month are printed in English)
  • it has to be Linux distro- and distro-family-independent solution. That is why I went for uname since as far as I know this is a most basic tool that is present on all Linux distros - Debian, Red Hat, Slackware, Arch etc. Other tools can be used if they are present on "all" distros out there
  • emphasis is on Linux, which means that BSD solutions (including MacOS) are not relevant.

The problem is that I'm not familiar with the command and especially if the way the date is printed is uniform that is I have considered simply removing the string after the first space (from left to right) until the sixth space (included, from right to left) but this will work ONLY if the date consists of 5 spaces:

<day of the week> <month> <day of the month> <daytime (HH:MM:SS)> <time standard/time zone> <Year>

Another thing I have considered is using mutual exclusion between the output from uname -rv and cat /proc/version.

Linux version 4.10.0-37-generic (buildd@lcy01-30) (gcc version 5.4.0 20160609 (Ubuntu 5.4.0-6ubuntu1~16.04.4) ) #41~16.04.1-Ubuntu SMP Fri Oct 6 22:42:22 UTC 2017
              |_______________|                                                                                                        |_________________________|


4.10.0-37-generic #41~16.04.1-Ubuntu SMP Fri Oct 6 22:42:22 UTC 2017
|_______________|                       |_________________________|

Regular expression should be an option here too however as I've mentioned above I don't know if the format of the date is uniform hence making of regular expression is quite difficult (imho). To this we can also add the varying nature of uname -v where the substring between the # and the date can be very different from one system to another.

  • Did you try sed 's/#\S\+\( SMP\)\? //' ? – Valentin Bajrami Nov 15 '17 at 9:49
  • Check @JaroslavKucera 's answer. I'm not sure if something else than SMP is possible. Also I have tried the output from uname -rv on two systems and one didn't print #41~16.04.1-Ubuntu ... but instead #4 .... I know what SMP stands for but not sure if it's 1)always at the location a specific locatio and 2)always present. – rbaleksandar Nov 15 '17 at 10:00
  • Did you try it? This doesn't require SMP to be there. SMP is optional. – Valentin Bajrami Nov 15 '17 at 10:02
  • I've tried it and it doesn't give any output whatsoever. I had to use Ctrl-C to kill it. It might be because my current system is a Ubuntu running in VirtualBox. However as I've mentioned testing a solution on just one-two distributions is not enough. The problem is that right now one of the solutions below work. But there is no guarantee that the currently used Linux distro won't be replaced on the target system in the near future. That is why I'm looking for a uniform solution (if such is available). – rbaleksandar Nov 15 '17 at 10:06
3

There is the sed way:

uname -rv | sed -e 's/\s#.*SMP//'

I've tried it on the Fedora/RHEL and it seems to work correctly too.

  • 1
    This will only work when SMP occurs in the string. – Kusalananda Nov 15 '17 at 9:24
  • 1
    Could you please post debian uname string? – Jaroslav Kucera Nov 15 '17 at 9:26
  • On Ubuntu it does...And Ubuntu is from the same Debian family... LOL Talking about diversity in the Linux world. -_- Also @Kusalananda is right - I cannot guarantee that SMP will be present in the output. – rbaleksandar Nov 15 '17 at 9:26
  • Well, then it looks like you'll have to take care of distribution/SMP/nonSMP script way :-( – Jaroslav Kucera Nov 15 '17 at 9:29
  • Is there still some nonSMP kernel version used in recent distros? – Jaroslav Kucera Nov 15 '17 at 9:31
0

It seems to me that you'd simply want to remove the second and third space-separated column:

$ uname -rv | cut -d ' ' -f 1,4-
4.4.0-87-generic Tue Jul 18 12:55:35 UTC 2017

cut here would interpret its input as space-separated fields, and would pick out the first such field along with any field from the fourth onwards.

  • 1
    This fails on Debian: uname -rv outputs 4.9.0-3-amd64 SMP Debian 4.9.30-2+deb9u3 (2017-08-06)... I’ve got another system which outputs 3.10.105 #1 SMP PREEMPT Sun Aug 20 17:02:37 CEST 2017, which isn’t handled either. – Stephen Kitt Nov 15 '17 at 9:22
  • Yeap, as I've mentioned a uniform distro-independent solution is needed here. – rbaleksandar Nov 15 '17 at 9:23
  • Also fails on Arch, I'm afraid. – terdon Nov 15 '17 at 9:29
  • @rbaleksandar Maybe a specific parsing of the uname -rv output for each distribution is needed? A case statement with one entry for each distribution. – Kusalananda Nov 15 '17 at 9:35
  • A one-line solution is needed here plus a distro-specific handling would mean that you have to consider a huge diversity of possible systems... If someone can guarantee that the date output is uniform. I shall edit the post to make a restriction on the language of the OS, since Tue is Tuesday, which is for English locale. – rbaleksandar Nov 15 '17 at 9:37
0

How about:

echo $(uname -r)" "$(uname -v | sed -En 's/(^.*)((Mon)|(Tue)|(Wed)|(Thu)|(Fri)|(Sat)|(Sun).*$)/\2/p')
  • Still won't match on the Debian. The format is different, see comments under other answers. – Jaroslav Kucera Nov 15 '17 at 9:46
0

What about

echo "$(uname -r)$(uname -v | sed -nE 's/.*(( \S+){6})$/\1/p')"
  • This produces “5.0.7-200.fc29.x86_64 Mon Apr 8 15:40:59 UTC 2019” on Fedora. – Stephen Kitt May 6 at 13:23
  • Exactly as original poster requested -- one word version, and date from week day to year. @Stephen-Kitt do you expect any other result? – Prince Mandor May 14 at 10:20
  • Did I say I expected any other result? However on Debian it doesn’t work. – Stephen Kitt May 14 at 11:29

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.