Linux all version name

How to get Ubuntu distribution’s full code name?

Output’s of the above commands shows only the partial code name (ie, trusty ). How do I get the full codename (trusty tahr) of my installed Ubuntu system?

It seems that there’s a convergent process towards the sourcing of the file /etc/os-release . Maybe you should specify what you mean by How do I get the full codename(trusty tahr) of my installed Ubuntu system?. Do you only want to echo it on the terminal, or do you need it assigned to a variable? Is this going to be used on some non- systems?

9 Answers 9

You can just source (the source command is a dot . ) the /etc/os-release and you’ll have access to all the variables defined there:

$ . /etc/os-release $ echo "$VERSION" 14.04, Trusty Tahr 

Edit. If you want to remove the 14.04, part (as asked by terdon), you could:

$ . /etc/os-release $ read _ UBUNTU_VERSION_NAME  

Note that this is a bit clunky, since on other distributions, the VERSION field can have different format. E.g., on my debian,

$ . /etc/os-release $ read _ UBUNTU_VERSION_NAME  

Then, you could imagine something like this (in a script):

#!/bin/bash if [[ -r /etc/os-release ]]; then . /etc/os-release if [[ $ID = ubuntu ]]; then read _ UBUNTU_VERSION_NAME  

There are many and yours is a particularly good one! Comments that are irrelevant to the actual answer can be deleted without warning.

the embedded variable and value in os-release currently is defined to something like this: VERSION="18.04.2 LTS (Bionic Beaver)"

My variant on what's already offered:

The shortest, Bashiest answer to date.

If you don't care to load /etc/os-release 's contents into your current environment, you can fake bash into thinking it's loading a script fairly easily:

Why the downvote? it's a good answer (though with nothing conceptually different from others given before it).

Grep:

$ grep $(lsb_release -rs) /usr/share/python-apt/templates/Ubuntu.info | grep -m 1 "Description: Ubuntu " | cut -d "'" -f2 Trusty Tahr 

Explanation:

  • lsb_release -rs -> Prints your installed Ubuntu version.
  • grep $(lsb_release -rs) /usr/share/python-apt/templates/Ubuntu.info -> Grab all the lines which contains your release version, in my case it's 14.04.
  • grep -m 1 "Description: Ubuntu " -> Again grabs only the matched first line(because of -m flag) which contains the string Description: Ubuntu .
  • cut -d "'" -f2 -> prints the field number 2 according to the delimiter single quote '

Awk:

$ awk -v var=$(lsb_release -rs) '$3~var ' /usr/share/python-apt/templates/Ubuntu.info | cut -d"'" -f2 Trusty Tahr 

Explanation:

Declaring and assigning Variable in awk is done through -v parameter.So the value of lsb_release -rs command is assigned to the variable var which inturn helps to print field 4 ,field 5 from the lines contain the string 14.04 and exists if its found an one.Finally the cut command helps to remove the single quotes.

it seems your solutions are based upon some python module for apt. that would mean a dependency on the file location and also on its formatting. also it only works if lsb_release with those options does provide a string that will match for the initial search operation. - nevertheless pointing out to those second source(!) to let others know about can in some special case be worth a million.

the grep variant might be able to be coded even shorter (with maybe a slight deviation on the exact search term - might not have practical relevance) - try this one: grep -m 1 "Description: Ubuntu lsb_release -rs " /usr/share/python-apt/templates/Ubuntu.info | cut -d \' -f 2

oops, left-ticked quotes vanished as mini-Markdown syntax understood these as code highlighting request. you will have to undo this for the proposal to work.

Answer relevant for at least ubuntu 16.04 and above:

If for some reason lsb_release is not available

cat /etc/os-release | grep UBUNTU_CODENAME | cut -d = -f 2 

even if i see some sympathy to your second way of parsing (instead of sourcing) the file in question - neither of the two options returns the full code name - only the first half is retrieved. xenial instead of xenial xerus, bionic instead of bionic beaver. sorry - that was not the question here.

The command you are looking for is:

grep -oP '(?  
)" data-controller="se-share-sheet" data-se-share-sheet-title="Share a link to this answer" data-se-share-sheet-subtitle="" data-se-share-sheet-post-type="answer" data-se-share-sheet-social="facebook twitter " data-se-share-sheet-location="2" data-se-share-sheet-license-url="https%3a%2f%2fcreativecommons.org%2flicenses%2fby-sa%2f3.0%2f" data-se-share-sheet-license-name="CC BY-SA 3.0" data-s-popover-placement="bottom-start">Share
)" title="">Improve this answer
)">edited Apr 19, 2014 at 6:23
answered Apr 19, 2014 at 4:38
0
Add a comment |
2

Here are some more choices. They all parse the /etc/os-release file which, on my 13.10, looks like this:

NAME="Ubuntu" VERSION="13.10, Saucy Salamander" ID=ubuntu ID_LIKE=debian PRETTY_NAME="Ubuntu 13.10" VERSION_ID="13.10" HOME_URL="http://www.ubuntu.com/" SUPPORT_URL="http://help.ubuntu.com/" BUG_REPORT_URL="http://bugs.launchpad.net/ubuntu/" 

All of the solutions below will parse the second line to produce Saucy Salamander .

grep -oP 'VERSION=.* \K\w* \w*' /etc/os-release 
awk -F'[" ]' '/VERSION=/' /etc/os-release 
sed -nr '/VERSION=/' /etc/os-release 
perl -lne '/VERSION=.*\b(\w+ \w+)/ && print $1' /etc/os-release 
grep VERSION= /etc/os-release | cut -d ' ' -f 2- | tr -d '"' 

Источник

Best way to find the OS name and version on a Unix/Linux platform

But it does not seem to be the best solution, as LSB_RELEASE support is no longer for RHEL 7.

Is there a way that will work on any Unix or Linux platform?

uname is in most unix environments and guaranteed to be on every LSB compliant linux distro: refspecs.linuxfoundation.org/LSB_2.0.1/LSB-Core/LSB-Core/…

@Niraj - By reading the manpage linux.die.net/man/1/uname and grokking its output (assuming it is supported in RH6.5) . either way there is no (single) portable way to get this because it is mostly irrelevant info. Portable programs should probe for required features, not use some whitelist of prechecked distros.

10 Answers 10

This work fine for all Linux environments.

$ cat /etc/*-release DISTRIB_ID=Ubuntu DISTRIB_RELEASE=10.04 DISTRIB_CODENAME=lucid DISTRIB_DESCRIPTION="Ubuntu 10.04.4 LTS" 
$ cat /etc/*-release DISTRIB_ID=Ubuntu DISTRIB_RELEASE=12.04 DISTRIB_CODENAME=precise DISTRIB_DESCRIPTION="Ubuntu 12.04.4 LTS" NAME="Ubuntu" VERSION="12.04.4 LTS, Precise Pangolin" ID=ubuntu ID_LIKE=debian PRETTY_NAME="Ubuntu precise (12.04.4 LTS)" VERSION_ID="12.04" 
$ cat /etc/*-release Red Hat Enterprise Linux Server release 6.5 (Santiago) Red Hat Enterprise Linux Server release 6.5 (Santiago) 
#!/bin/sh # Detects which OS and if it is Linux then it will detect which Linux # Distribution. OS=`uname -s` REV=`uname -r` MACH=`uname -m` GetVersionFromFile() < VERSION=`cat $1 | tr "\n" ' ' | sed s/.*VERSION.*=\ // ` >if [ "$" = "SunOS" ] ; then OS=Solaris ARCH=`uname -p` OSSTR="$ $($ `uname -v`)" elif [ "$" = "AIX" ] ; then OSSTR="$ `oslevel` (`oslevel -r`)" elif [ "$" = "Linux" ] ; then KERNEL=`uname -r` if [ -f /etc/redhat-release ] ; then DIST='RedHat' PSUEDONAME=`cat /etc/redhat-release | sed s/.*\(// | sed s/\)//` REV=`cat /etc/redhat-release | sed s/.*release\ // | sed s/\ .*//` elif [ -f /etc/SuSE-release ] ; then DIST=`cat /etc/SuSE-release | tr "\n" ' '| sed s/VERSION.*//` REV=`cat /etc/SuSE-release | tr "\n" ' ' | sed s/.*=\ //` elif [ -f /etc/mandrake-release ] ; then DIST='Mandrake' PSUEDONAME=`cat /etc/mandrake-release | sed s/.*\(// | sed s/\)//` REV=`cat /etc/mandrake-release | sed s/.*release\ // | sed s/\ .*//` elif [ -f /etc/debian_version ] ; then DIST="Debian `cat /etc/debian_version`" REV="" fi if [ -f /etc/UnitedLinux-release ] ; then DIST="$[`cat /etc/UnitedLinux-release | tr "\n" ' ' | sed s/VERSION.*//`]" fi OSSTR="$ $ $($ $ $)" fi echo $

The script is useful but for linux it is showing ==Linux RedHat version(Final 2.6.32-431.el6.x86_64 x86_64) .my redhat version is 6.5 but it is not showing in output ?

I tested on RHEL6.3 It is showing output as Linux RedHat 6.3(Santiago 2.6.32-279.22.1.el6.x86_64 x86_64)

Following command worked out for me nicely. It gives you the OS name and version.

The "lsb_release" command provides a certain Linux Standard Base (LSB) and distribution-specific information.

So using the below command we can get the Operating system name and operating system version.

"lsb_release -a"

This command gives you a description of your operating system:

In every distribution, it has different files, so I list the most common ones:

---- CentOS Linux distribution `cat /proc/version` ---- Debian Linux distribution `cat /etc/debian_version` ---- Redhat Linux distribution `cat /etc/redhat-release` ---- Ubuntu Linux distribution `cat /etc/issue` or `cat /etc/lsb-release` 

In the last one, /etc/issue didn't exist, so I tried the second one and it returned the right answer.

With quotes:

cat /etc/*-release | grep "PRETTY_NAME" | sed 's/PRETTY_NAME=//g' 

Without quotes:

cat /etc/*-release | grep "PRETTY_NAME" | sed 's/PRETTY_NAME=//g' | sed 's/"//g' 

@PeterMortensen in /etc/*-release file original value is stored with double quotes. if you want to save it in database most probably you would want to remove those double quotes as well. 🙂

My own take at @kvivek's script, with more easily machine parsable output:

#!/bin/sh # Outputs OS Name, Version & misc. info in a machine-readable way. # See also NeoFetch for a more professional and elaborate bash script: # https://github.com/dylanaraps/neofetch SEP="," PRINT_HEADER=false print_help() < echo "`basename $0` - Outputs OS Name, Version & misc. info" echo "in a machine-readable way." echo echo "Usage:" echo " `basename $0` [OPTIONS]" echo "Options:" echo " -h, --help print this help message" echo " -n, --names print a header line, naming the fields" echo " -s, --separator SEP overrides the default field-separator ('$SEP') with the supplied one" ># parse command-line args while [ $# -gt 0 ] do arg="$1" shift # past switch case "$" in -h|--help) print_help exit 0 ;; -n|--names) PRINT_HEADER=true ;; -s|--separator) SEP="$1" shift # past value ;; *) # non-/unknown option echo "Unknown switch '$arg'" >&2 print_help ;; esac done OS=`uname -s` DIST="N/A" REV=`uname -r` MACH=`uname -m` PSUEDONAME="N/A" GetVersionFromFile() < VERSION=`cat $1 | tr "\n" ' ' | sed s/.*VERSION.*=\ // ` >if [ "$" = "SunOS" ] ; then DIST=Solaris DIST_VER=`uname -v` # also: cat /etc/release elif [ "$" = "AIX" ] ; then DIST="$" DIST_VER=`oslevel -r` elif [ "$" = "Linux" ] ; then if [ -f /etc/redhat-release ] ; then DIST='RedHat' PSUEDONAME=`sed -e 's/.*\(//' -e 's/\)//' /etc/redhat-release ` DIST_VER=`sed -e 's/.*release\ //' -e 's/\ .*//' /etc/redhat-release ` elif [ -f /etc/SuSE-release ] ; then DIST=`cat /etc/SuSE-release | tr "\n" ' '| sed s/VERSION.*//` DIST_VER=`cat /etc/SuSE-release | tr "\n" ' ' | sed s/.*=\ //` elif [ -f /etc/mandrake-release ] ; then DIST='Mandrake' PSUEDONAME=`sed -e 's/.*\(//' -e 's/\)//' /etc/mandrake-release` DIST_VER=`sed -e 's/.*release\ //' -e 's/\ .*//' /etc/mandrake-release` elif [ -f /etc/debian_version ] ; then DIST="Debian" DIST_VER=`cat /etc/debian_version` PSUEDONAME=`lsb_release -a 2> /dev/null | grep '^Codename:' | sed -e 's/.*[[:space:]]//'` #elif [ -f /etc/gentoo-release ] ; then #TODO #elif [ -f /etc/slackware-version ] ; then #TODO elif [ -f /etc/issue ] ; then # We use this indirection because /etc/issue may look like # "Debian GNU/Linux 10 \n \l" ISSUE=`cat /etc/issue` ISSUE=`echo -e "$" | head -n 1 | sed -e 's/[[:space:]]\+$//'` DIST=`echo -e "$" | sed -e 's/[[:space:]].*//'` DIST_VER=`echo -e "$" | sed -e 's/.*[[:space:]]//'` fi if [ -f /etc/UnitedLinux-release ] ; then DIST="$[`cat /etc/UnitedLinux-release | tr "\n" ' ' | sed s/VERSION.*//`]" fi # NOTE `sed -e 's/.*(//' -e 's/).*//' /proc/version` # is an option that worked ~ 2010 and earlier fi if $PRINT_HEADER then echo "OS$Distribution$Distribution-Version$Pseudo-Name$Kernel-Revision$Machine-Architecture" fi echo "$$$$$$$$$$$" 

NOTE: Only tested on Debian 11

Example Runs

No args

Linux,Debian,10.0,buster,4.19.0-5-amd64,x86_64 

Header with names and custom separator

OS | Distribution | Distribution-Version | Pseudo-Name | Kernel-Revision | Machine-Architecture Linux | Debian | 10.0 | buster | 4.19.0-5-amd64 | x86_64 

Filtered output

Источник

Читайте также:  Быстро создать файл linux
Оцените статью
Adblock
detector