Skip to content

tsort command in Linux

The tsort command in Linux is used to sort text in topological order. It reads pairs of items from standard input and writes to standard output the same items but in such a way that for any pair A B, where A appears before B in the input, A also appears before B in the output. This command is particularly useful for sorting dependencies in build systems or directed graphs. The tsort command can help in organizing tasks or processes that are dependent on each other in a meaningful sequence.

Terminal window
tsort [options] [file]
OptionDescription
-Detect cycles in the input and report
-zUse a 0-terminated list of values in the input file
—helpDisplay help message and exit
—versionOutput version information and exit
ParameterDescription
fileInput file containing a list of items and their dependencies

Sort a Text File Containing Dependency Information

Section titled “Sort a Text File Containing Dependency Information”
Terminal window
tsort dependencies.txt

Sorts a text file containing dependency information.

Terminal window
tsort -q dependencies.txt

Tries to resolve circular dependencies in a text file.

Terminal window
tsort -n input.txt

Displays the sorted list with numbers assigned to each item.

Terminal window
tsort -r input.txt

Sorts a file in reverse order.

Terminal window
echo "B A C" | tsort

Reads input from standard input and sorts it.

Terminal window
tsort -i input.txt

Ignores lines in the input file starting with #.

Terminal window
tsort -f input.txt

Sorts ignoring case sensitivity.

Terminal window
tsort input.txt > output.txt

Writes the sorted result to an output file.

To use the tsort command in Linux, execute the following command:

Terminal window
tsort file.txt

The tsort command in Linux is used to perform topological sorting on directed graphs. It reads a list of pairs of strings from a file representing directed edges and writes to standard output the strings in a valid order for which all the input edges are from the first string of the pair to the second string.

You can provide input to tsort by creating a text file with pairs of strings representing directed edges. Each pair should be on a new line in the file.

Terminal window
echo -e "b a\nc b\na c" > input.txt
tsort input.txt

No, tsort is not able to handle cyclic graphs. If the input contains a cycle, tsort will report an error mentioning the specific cycle that caused the issue.

If there are overlapping edges in the input file provided to tsort, the command will consider the later edge as an additional constraint and adjust the sorting order accordingly.

How can I display the numeric IDs of the sorted strings instead of their names?

Section titled “How can I display the numeric IDs of the sorted strings instead of their names?”

You can use the -g option with tsort to display the numeric IDs of the sorted strings along with their names.

Terminal window
tsort -g input.txt

Is it possible to reverse the sorting order with tsort?

Section titled “Is it possible to reverse the sorting order with tsort?”

Yes, you can reverse the sorting order of tsort output by using the -r option. This will display the sorted strings in reverse order.

Terminal window
tsort -r input.txt

Yes, tsort can process multiple input files simultaneously. Simply provide all the input file names separated by spaces as arguments.

Terminal window
tsort file1.txt file2.txt file3.txt

How can I ignore whitespace and empty lines in the input file?

Section titled “How can I ignore whitespace and empty lines in the input file?”

To ignore whitespace and empty lines in the input file provided to tsort, you can use the -d option. This will skip any whitespace or empty lines while processing the input.

Terminal window
tsort -d input.txt
  • Resolving dependencies in makefiles
  • Checking for circular dependencies
  • Generating execution order for tasks in a workflow
  • Sorting directed acyclic graphs