------------------------------------------------------------------------------- Sorting by fields This is not easy to understand from the manpages But to sort by 3'rd then 2rd, and then 1st field... sort -t: -k3,3 -k2,2 -k1,1 /etc/passwd If second field are numbers... sort -t: -k3,3 -k2,2n -k1,1 /etc/passwd If the field number is NOT repeated, it goes from that field to EOL ------------------------------------------------------------------------------- Sorting - Human readable units sort using numbers with letter suffix (eg 265Kbytes, without space!) sort -h EG: du -sh * | sort -rh FYI: convert numbers to si (base 1000) or iec (base 1024) units numfmt --to=si 4096 4.1K numfmt --to=iec 4096 4.0K numfmt --to=iec-i 4096 4.0Ki ------------------------------------------------------------------------------- Sort Internet Addresses (IPs) This makes things tricky, as numerical sorting treats '.' as a decimal and not as a field separator. Old method before "Semantic Versioning" sort -n -t . -k 1,1 -k 2,2 -k 3,3 -k 4,4 Semantic Versioning can sort IP's sort -V ------------------------------------------------------------------------------- Sorting Version Numbers See "Semantic Versioning" https://semver.org/ Sorting Version numbers with '.' separations are tricky... sort kernel-1.13.4 kernel-1.3.17 kernel-1.3.7 kernel-1.3.8 kernela-2.0.0 kernelb-0.0.1 sort -V kernela-2.0.0 kernelb-0.0.1 kernel-1.3.7 kernel-1.3.8 kernel-1.3.17 kernel-1.13.4 It worked but... Note how the name aspects are not sorted in the same order as before. There is not even a gurantee that 'a' and 'b' are in that order! The fix is to seperate the name and version fields... EG sort by name first, then version number sort -t- -k1,1d -k2V kernel-1.3.7 kernel-1.3.8 kernel-1.3.17 kernel-1.13.4 kernela-2.0.0 kernelb-0.0.1 ---- Version sorting is also available using "ls -v" ls -1v /usr/src/kernels/ 4.16.8-300.fc28.x86_64/ 4.16.12-300.fc28.x86_64/ 4.17.7-200.fc28.x86_64/ But with names present it produces the same 'name' ordering problem, but unlike "sort", "ls" can not specify multiple sort fields. ls -1v version_test_dir/ kernela-2.0.0 kernelb-0.0.1 kernel-1.3.7 kernel-1.3.8 kernel-1.3.17 kernel-1.13.4 Solution for filenames... ls -1 version_test_dir/ | sort -t- -k1,1d -k2V | column ------------------------------------------------------------------------------- Version number testing https://stackoverflow.com/questions/4023830/ This uses -C to quietly check for sorted order.RNING True if second line is 'same or newer' than first version_test() { printf '%s\n' "$@" | sort -C -V; } # version_test {minimim_desired_version} {installed_version} # FALSE if an update is needed # Testing Version Numbers # order below is sorted order from "sort -V" echo ' # Older Version | 0 | 0.1.1.2 | 0.9.8 | 1.0.0d | 1.0.1 | 1.0.1a | 1.0.1k | 1.0.2g | 1.0.2k-fips | 1.1.1 | 0001.0001.0001a | 1.1.1a | 1.1.1a.2 # Same or Newer | 1.1.1b | 0001.0001.0001k | 1.1.1k | 1.1.1k-fips | 1.1.1.1 | 1.1.1.2 | 1.2 | 2 ' | sed -n '/^ *|/!d; s///; s/^ //; p' | while read -r v; do if version_test 1.1.1b "$v" then printf "%-20s%s\n" "$v" "NEWER (or at least the same)" else printf "%-20s%s\n" "$v" "OLDER" fi done Test can also be used for 'between version' testing version_test {minimum_version} {current_version} {maximum_version} WARNING: version_test 01.01.01 1.1.1 && echo true || echo false # > true version_test 1.1.1 01.01.01 && echo true || echo false # > false Numerically they are the same, but not to version as they sort differently -------------------------------------------------------------------------------