Best way to find os name and version in Unix/Linux platform
This work fine for all Linux environment.
#!/bin/shcat /etc/*-release
In Ubuntu:
$ cat /etc/*-releaseDISTRIB_ID=UbuntuDISTRIB_RELEASE=10.04DISTRIB_CODENAME=lucidDISTRIB_DESCRIPTION="Ubuntu 10.04.4 LTS"
or 12.04:
$ cat /etc/*-releaseDISTRIB_ID=UbuntuDISTRIB_RELEASE=12.04DISTRIB_CODENAME=preciseDISTRIB_DESCRIPTION="Ubuntu 12.04.4 LTS"NAME="Ubuntu"VERSION="12.04.4 LTS, Precise Pangolin"ID=ubuntuID_LIKE=debianPRETTY_NAME="Ubuntu precise (12.04.4 LTS)"VERSION_ID="12.04"
In RHEL:
$ cat /etc/*-releaseRed Hat Enterprise Linux Server release 6.5 (Santiago)Red Hat Enterprise Linux Server release 6.5 (Santiago)
Or Use this Script:
#!/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 [ "${OS}" = "SunOS" ] ; then OS=Solaris ARCH=`uname -p` OSSTR="${OS} ${REV}(${ARCH} `uname -v`)"elif [ "${OS}" = "AIX" ] ; then OSSTR="${OS} `oslevel` (`oslevel -r`)"elif [ "${OS}" = "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="${DIST}[`cat /etc/UnitedLinux-release | tr "\n" ' ' | sed s/VERSION.*//`]" fi OSSTR="${OS} ${DIST} ${REV}(${PSUEDONAME} ${KERNEL} ${MACH})"fiecho ${OSSTR}
Following command worked out for me nicely. It gives you the OS name and version.
lsb_release -a
The "lsb_release" command provides certain Linux Standard Base and distribution-specific information.So using the below command we can get Operating system name and operating system version.
"lsb_release -a"