-
-
Notifications
You must be signed in to change notification settings - Fork 2
Linux tsort Guide
Mattscreative edited this page Dec 5, 2025
·
2 revisions
Complete beginner-friendly guide to tsort on Linux, covering Arch Linux, CachyOS, and other distributions including topological sorting, dependency ordering, and graph sorting.
tsort performs topological sort.
Uses:
- Dependency ordering: Order items by dependencies
- Graph sorting: Sort directed acyclic graphs
- Build ordering: Determine build order
- Task scheduling: Schedule dependent tasks
Why it matters:
- Dependency resolution: Resolve dependencies
- Build systems: Determine build order
- Task scheduling: Schedule tasks
Basic usage:
# Sort dependencies
tsort dependencies.txt
# Input: pairs of items (A B means A depends on B)Pair format:
# Input format: item1 item2
# Means item1 depends on item2
echo -e "A B\nB C\nC D" | tsort
# Output: D C B A (reverse dependency order)Basic dependencies:
# Simple chain
echo -e "task1 task2\ntask2 task3" | tsort
# Output: task3 task2 task1Complex graph:
# Multiple dependencies
echo -e "A B\nA C\nB D\nC D" | tsort
# Sorts by dependenciesGraph input:
# Graph edges
cat graph.txt | tsort
# Sorts nodes by dependenciesDetects cycles:
# Circular dependency
echo -e "A B\nB A" | tsort
# Error: cycle detectedCheck installation:
# tsort is part of coreutils
# Usually pre-installed
# Check tsort
which tsortThis guide covered tsort usage, topological sorting, and dependency ordering for Arch Linux, CachyOS, and other distributions.
- sort Guide - General sorting
- Graph Algorithms - Graph processing
-
tsort Documentation:
man tsort
This guide covers Arch Linux, CachyOS, and other Linux distributions. For distribution-specific details, refer to your distribution's documentation.