4

I'm reading this blog at the moment: https://www.baeldung.com/linux/dir-find-largest-file-recursively

And I am asking about this command:

find /usr/lib -type f -printf "%s\t%p\n" | sort -n | tail -1
92107656        /usr/lib/x86_64-linux-gnu/libwireshark.so.13.0.3

The %swill print the size and %p will print the path. But how will sort -n know it needs to sort by size and not path?

I know this might be a stupid question, but anyway I asked it.

As per the docs, https://man7.org/linux/man-pages/man1/sort.1.html

-n, --numeric-sort
compare according to string numerical value

This sorts using string numerical value. How does it know that it needs to look at the first column specifically?

2
  • With sort, there is no concept of columns. A string is taken as input. A whole string (including what could appear to your eyes as column separators) It just happens in your specific case that the first part of the string (which appears as a separated column to you) is enough to establish the sort order of the input lines.
    – MC68020
    Dec 31, 2023 at 11:04
  • 5
    @MC68020 strictly speaking, sort des have a concept of fields, that's what the -k and -t options are for. Its just that in the absence of -k, it will indeed sort on the entire line.
    – terdon
    Dec 31, 2023 at 21:33

3 Answers 3

11

Many options affect how sort compares lines

That's from info coreutils sort invocation which explains how sort works:

A pair of lines is compared as follows: sort compares each pair of fields (see --key), in the order specified on the command line, according to the associated ordering options, until a difference is found or no fields are left. If no key fields are specified, sort uses a default key of the entire line.

So sort -n will sort the input numerically (as explained further down), comparing entire lines since no key field is specified:

-n
--numeric-sort
--sort=numeric

Sort numerically. The number begins each line and consists of optional blanks, an optional - sign, and zero or more digits possibly separated by thousands separators, optionally followed by a decimal-point character and zero or more digits. An empty number is treated as 0. The LC_NUMERIC locale specifies the decimal-point character and thousands separator. By default a blank is a space or a tab, but the LC_CTYPE locale can change this.

Comparison is exact; there is no rounding error.

4

Because that's literally what sort does, anyway. It sorts lines. Sorting always happens by comparing the beginning of two strings (in every case, not just the sort command; check with you sorting words on paper), and only if the beginnings are equal, moving forward one element and comparing that, and so on.

In -n mode, sort simply reads the characters at the beginning of each line for as long as they form a number, and then interprets that number as the first element. The second element then becomes the first character after what can be interpreted as number, the third Element is the second character after what can be interpreted as number, and so on).

In no mode ever does sort do anything but sorting things, and thus, it always compares the first elements first, and the question "why does it not sort by some part of lines that comes later on" never arises.

There is hence nothing in sort (without using its -k flag) that thinks of your data as being in columns. It sorts strings of characters, with the special exception that -n makes it try to interpret all characters at the beginning of these strings that can be interpreted as one number as a single "supercharacter".

0
2

There is no concept of columns, but there is a concept of fields, with a --field-separator option.

Then, each field can have an individual --key=KEYDEF. The lines can be sorted by multiple key fields (which can occur in any order on the line), and any key definition can independently specify ignore blanks, numeric key, and ascending or descending order. There are eleven such sub-options available for any key specification.

The man sort page is utterly inadequate. At least refer to info sort.

You can also read https://www.gnu.org/software/coreutils/manual/coreutils.pdf and scroll down to Section 7.1 sort: Sort text files which gives ten pages of detail, including something like:

sort -t : -b -k 5,5 -k 3,3n /etc/passwd

This section also refers to the DSU idiom (Decorate, Sort, Undecorate), which is totally flexible.

1
  • "The man sort page is utterly inadequate.". ++ (as are many others /-;! )
    – shellter
    Jan 1 at 16:40

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .