I am sorting a large number of files one after another. Using sort -o file file. The for loop halted midway and before restarting I want to use sort's -c option to speedup sorting. But it says that -co options are not compatible. Why is it so?

sort --version
sort (GNU coreutils) 8.13

OS: Ubuntu 12.10
  • Please include your operating system and sort version. There are various implementations of sort with different options. – terdon Oct 6 '13 at 3:45

Because you are telling it that you want to only --check which does not generate output and you also specify that you want the sorted --output to go to a certain file. These are mutually exclusive concepts, and they are enforced in sort.c (gnu coreutils 8.20) with

  if (checkonly)

      if (outfile)
          static char opts[] = {0, 'o', 0};
          opts[0] = checkonly;
          incompatible_options (opts);

Assuming you are using GNU sort, the options you mention do:

   -c, --check, --check=diagnose-first
          check for sorted input; do not sort
   -o, --output=FILE
          write result to FILE instead of standard output

The -c flag will just inform you whether or not the input is already sorted:

$ echo -e "A\nB\nC" | sort -c
$ echo -e "A\nC\nB" | sort -c
sort: -:3: disorder: B

So, if the input is sorted, nothing is printed and if the inpnut is not sorted, an error message is printed to standard error. In other words, sort -c never prints to the standard output. Since it never prints, it does not make much sense to redirect its output.


sort -c never produces output. It only checks the file. With no output, there is no reason to write it to a file. So, incompatible with -o.

You could use -c as some sort of a check on a file before sorting it, but I'm not sure that would actually speed things up like you want.


There is a big difference in checking if a file is sorted and sort it.

When checking, in the best case two entries are enough (pretty fast), at the first entry that isn't sorted the command can exit and the check is done, but at this point the output would be incomplete, the worst case is when the file is sorted (only in this case the output would have make sense).

If you have many files to sort and happen often to interrupt, probably you should keep track in some way of the done files before sorting again.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.