Creating an Index of Many Files

Problem

You have a number of files for which you’d like to create an index.

Solution

Use the find command in conjunction with head, grep, or other commands that can parse out comments or summary information from each file.

For example, if the second line of all your shell scripts follows the format “name— description” then this example will create a nice index:

$ for i in $(grep -El '#![[:space:]]?/bin/sh' *); do head -2 $i | tail -1; done

Discussion

As noted, this technique depends on each file having some kind of summary information, such as comments, that may be parsed out. We then look for a way to identify the type of file, in this case a shell script, and grab the second line of each file.

If the files do not have easily parsed summary information, you can try something like this and manually work through the output to create an index:

for dir in $(find . -type d); do head -15 $dir/*; done

Warning

Watch out for binary files!

See Also

  • man find

  • man grep

  • man head

  • man tail

Get bash Cookbook 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.