unexpand - macOS


unexpand is a utility that removes leading whitespace and tabs from the text files. It is commonly used to convert text to a standard format without leading whitespace, making it easier to compare, merge, or apply further processing.


unexpand [-a] [-t] [file]


  • -a: Remove all whitespace and tabs, including trailing whitespace.
  • -t: Remove only leading whitespace and tabs. This is the default behavior.


Example 1: Remove all whitespace and tabs

unexpand -a text.txt

Example 2: Remove only leading whitespace and tabs

unexpand text.txt

Example 3: Process multiple files

unexpand file1.txt file2.txt file3.txt

Common Issues

  • Oversized files: If the input file is extremely large, unexpand may run out of memory during processing. To avoid this, use the -a option in conjunction with the tail command:
tail -c +20000000 large_file.txt | unexpand -a > new_file.txt


unexpand can be combined with other macOS commands for advanced tasks:

  • diff -c: Compare two files after removing extra whitespace:
diff -c <(unexpand file1.txt) <(unexpand file2.txt)

Related Commands

  • expand: Adds whitespace to a file.
  • fmt: Reformats text based on user-defined rules.
  • sed: A powerful stream editor for text processing.