Formatting Output for Use As Input to Another Program

If you’ve used the UNIX command line for even five minutes, you’ve used ls. It shows you the names of files in a particular directory. Suppose you have a directory of images with numeric names. ls nicely formats it for you:

 $ ​​ls
 10_image.jpg 2_image.jpg 5_image.jpg 8_image.jpg
 11_image.jpg 3_image.jpg 6_image.jpg 9_image.jpg
 1_image.jpg 4_image.jpg 7_image.jpg

Suppose we want to see these files in numeric order. ls has no ability to do this; it sorts lexicographically, even when we use the invocation ls -1, which produces one file per line, in “sorted” order:[32]

 $ ​​ls​​ ​​-1
 10_image.jpg
 11_image.jpg
 1_image.jpg
 2_image.jpg
 3_image.jpg
 4_image.jpg
 5_image.jpg
 6_image.jpg ...

Get Build Awesome Command-Line Applications in Ruby 2 now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.