How find Linux version from command line?

Hi,

I am looking to extract linux version from /etc/*-release file.

I am specifically tring to avoid use of awk command here. would be great if can do done via sed or grep command.

Red Hat Enterprise Linux Server release 6.5 (Tikanga)

output must be 6

regards,
Litu

Hello Litu,

Kindly use the code tags for commands and codes which you are using in the posts, also you can review your post before click on Submit Reply by clicking on Preview Post. Following may be helpful for you.

awk '{print $(NF-1)}' /etc/redhat-release

This should show the version of Linux Server release.

Thanks,
R. Singh

thanks for the reply. But we are trying to avoid use of awk command. can we do the same using grep, sed or cut command.

Hello Litu,

Following sed solution may help you.

sed 's/[[:alpha:]]//g;s/[[:space:]]//g' filename

EDIT: Adding more solutions for same.

cut -f7 -d " " /etc/redhat-release | cut -c1
 
cut -f7 -d " " /etc/redhat-release | cut -f1 -d"."

Thanks,
R. Singh

1 Like

Both the codes give you 6 as the output

rev /etc/redhat-release | cut -f2 -d ' ' | rev | cut -f1 -d '.'

of

cut -f1 -d '.' /etc/redhat-release | rev | cut -f1 -d ' ' | rev

---------- Post updated at 07:54 AM ---------- Previous update was at 07:49 AM ----------

sed solution

sed 's/[^[:digit:]]*\([[:digit:]]*\)\..*/\1/' /etc/redhat-release

Posted by SriniShoo:

Hello SriniShoo,

I am not sure why you are using rev here, I guess we can esaily do it with cut and sed .

EDIT: Also you have missed sed while giving sed solutions.

sed 's/[^[:digit:]]*\([[:digit:]]*\)\..*/\1/' /etc/redhat-release

Thanks,
R. Singh


  1. :digit: ↩ī¸Ž

thanks a lot R Singh...

I would point out that you that Linux version refers to the Linux kernel only. You are looking to find the major version number of a specific Linux distribution.