Home Blog CV Projects Patterns Notes Book Colophon Search

Really Useful Shell Command

23 Jun, 2014

Vlad (my colleague at Hogarth) was trying to find a simple way of scanning a directory and printing out the name of each file, followed by the contents of line 482 if it existed. In searching for a solution we came up with this useful line of bash:

find  . | xargs -n 1 sh -c "echo \$0; sed -n '482p' \$0"

The really nice thing about this is that you can modify the find for different constraints, and easily add different things to do with each file instead of the sed command, you leave the structure and the echo \$0 in place and then this pattern can be used for other cases.

There are obviously lots of ways of solving the original problem, but this bit of command line foo seemed worth keeping for its potential for reuse.

Copyright James Gardner 1996-2020 All Rights Reserved. Admin.