Compare, Sort and List Linux Kernel Versions with linux-version
You will use linux-version to answer three practical questions: which of two kernel release strings is newer, how to order a list of releases, and which kernel versions are installed in the customary location. The commands are read-only. Allow about ten minutes, including a quick check of the installed package version.
The route
Jump straight to the step you need, or tick off Done means at the end.
1. Check the installed command
This guide describes the command installed here from linux-base version 4.5ubuntu9+24.04.2. The program is designed for version strings reported by uname -r and used in kernel file names. These are not Debian package versions, so do not substitute dpkg --compare-versions and expect identical ordering.
$ command -v linux-version
/usr/bin/linux-version
$ dpkg-query -W -f='${Package} ${Version}\n' linux-base
linux-base 4.5ubuntu9+24.04.2
$ linux-version --help
Usage: /usr/bin/linux-version compare VERSION1 OP VERSION2
/usr/bin/linux-version sort [--reverse] [VERSION1 VERSION2 ...]
/usr/bin/linux-version list [--paths]
Checkpoint: if command -v finds nothing, install or enable the package through your normal distribution process before continuing. The examples below do not need sudo.
2. Compare two kernel versions in a script
Use the compare subcommand followed by two version strings and an operator. The installed command accepts lt, le, eq, ge and gt. A satisfied comparison exits with status zero; an unsatisfied comparison exits non-zero. That makes the command useful in shell conditionals without parsing text output.
$ linux-version compare 6.8.0-31-generic lt 6.8.0-40-generic
$ printf 'comparison status: %s\n' "$?"
comparison status: 0
$ linux-version compare 6.8.0-40-generic gt 6.8.0-31-generic
$ printf 'comparison status: %s\n' "$?"
comparison status: 0
For a real decision, put the command directly in an if statement so the status is not accidentally replaced by another command:
$ running=$(uname -r)
$ candidate='6.8.0-40-generic'
$ if linux-version compare "$running" ge "$candidate"; then
> printf '%s is at least %s\n' "$running" "$candidate"
> else
> printf '%s is older than %s\n' "$running" "$candidate"
> fi
Keep each version as one quoted shell argument. A kernel string such as 6.8.0-40-generic contains no spaces, but quoting makes the boundary clear and keeps the example safe when values come from variables.
3. Handle the installed operator mismatch
The local linux-version(1) page lists ne for "not equal". The installed 4.5ubuntu9+24.04.2 command does not accept it: its help omits ne and the command exits with status 2. Treat the executable and its local help as the authority for scripts running on this host.
$ linux-version compare 6.8.0-40-generic ne 6.8.0-41-generic
Usage: /usr/bin/linux-version compare VERSION1 OP VERSION2
/usr/bin/linux-version sort [--reverse] [VERSION1 VERSION2 ...]
/usr/bin/linux-version list [--paths]
$ printf 'comparison status: %s\n' "$?"
comparison status: 2
To test inequality with this installed version, use a supported equality check and invert its status:
$ if ! linux-version compare "$running" eq "$candidate"; then
> printf 'versions differ\n'
> fi
versions differ
Do not treat status 2 as "not equal". It means the command line was invalid, which is a different failure from a valid comparison returning false.
4. Sort versions supplied as arguments or standard input
With versions on the command line, sort prints them from lowest to highest. Add --reverse for highest first. This is useful for a small list you have already collected:
$ linux-version sort 6.8.0-40-generic 6.8.0-31-generic 6.8.0-39-generic
6.8.0-31-generic
6.8.0-39-generic
6.8.0-40-generic
$ linux-version sort --reverse 6.8.0-40-generic 6.8.0-31-generic 6.8.0-39-generic
6.8.0-40-generic
6.8.0-39-generic
6.8.0-31-generic
With no version arguments, the command reads one line at a time from standard input. Text after the first space is retained, so you can sort a version together with a label or path:
$ printf '%s\n' '6.8.0-40-generic current' '6.8.0-31-generic old' | linux-version sort --reverse
6.8.0-40-generic current
6.8.0-31-generic old
The version must come first on each input line. Do not feed arbitrary log lines and assume the command will discover a version embedded in the middle.
5. List installed kernel versions and paths
The list subcommand checks the customary kernel location and prints the versions it finds. It does not install, remove or select a kernel:
$ linux-version list
6.8.0-139-generic
6.8.0-142-generic
$ linux-version list --paths
6.8.0-139-generic /boot/vmlinuz-6.8.0-139-generic
6.8.0-142-generic /boot/vmlinuz-6.8.0-142-generic
Your output will differ. The path form is convenient when you need to inspect the actual kernel images. To show the newest installed entry first, preserve each version and path by piping the complete lines into sort:
$ linux-version list --paths | linux-version sort --reverse
6.8.0-142-generic /boot/vmlinuz-6.8.0-142-generic
6.8.0-139-generic /boot/vmlinuz-6.8.0-139-generic
Listing is read-only and normally unprivileged. A missing or unusual /boot layout can produce an empty or unexpected list; verify the directory and installed files separately before drawing a conclusion about the running kernel.
6. Avoid the common traps
linux-version compares kernel-style strings, not arbitrary labels. Use uname -r when you need the running kernel's exact release string, and use linux-version list when you need the package-managed kernel images found in the customary location. These answers can legitimately differ after an upgrade or before a reboot.
Do not use a failed comparison as permission to remove a kernel. This command never changes files, boot-loader settings or the running system, and it has no undo operation because it performs no state change. If a later maintenance task uses the selected version to remove packages or alter boot configuration, stop and review that separate command before granting it elevated privileges.
Done means
linux-version --helpmatches the command installed on the host.- Supported comparisons return zero for true and a non-zero status for false or invalid input.
- Scripts use
! ... eq ...for inequality on this installed version rather than the undocumented-by-helpne. sort --reverseproduces newest-first output, including text retained after each version.list --pathsshows the installed kernel version and its corresponding image path without changing the machine.