LinuxCommandLibrary

tsort

TLDR

Sort dependencies

$ tsort [dependencies.txt]
copy
From stdin
$ echo -e "a b\nb c" | tsort
copy
Sort makefile dependencies
$ tsort [makefile_deps]
copy

SYNOPSIS

tsort [file]

DESCRIPTION

tsort performs topological sorting. It orders items respecting dependencies.
Input is pairs of items. First depends on second.
Output lists items in order. Dependencies come before dependents.
Cycles are reported as errors. Circular dependencies can't be sorted.
Common for build systems. Determine compilation order.

INPUT FORMAT

$ a b    # a depends on b
b c    # b depends on c
c d    # c depends on d
copy

OUTPUT

Items in dependency order: d, c, b, a

EXAMPLE

$ $ echo -e "main util\nutil lib\nlib" | tsort
lib
util
main
copy

CAVEATS

Detects cycles but can't resolve. Single dependency per line pair. Whitespace separated.

HISTORY

tsort is part of POSIX and has been in Unix since early versions. It was originally used for ordering object files in linker commands.

SEE ALSO

sort(1), make(1)

Copied to clipboard