Before head, I used sed. These two commands do the same thing:
$ sed 5q fooI don't know a convenient sed synonym for tail, which does several, non-trivial things.
$ head -5 foo
$ tail -5 # last 5 lines of the fileI use tail -f all the time, for glancing at output that I've been saving, from long running processes.
$ tail -n +5 # all but the first 5 lines
$ tail -f # start with the last 10 lines of the file
# but keep on spitting out lines as they appear
$ make &> make.OUT &But what about the upside-down of tail -n +5? What if I want all but the last five lines of the file? I don't know how to do this without writing a program.
... # do some other stuff
$ tail -f make.OUT
... # watch it for a minute, to see how it's progressing
$ # go back to other stuff
(If you do, tell me.)
An exception to this, at my fingertips, is "all but the last one line."
If I have a directory full of logs, and I want to get rid of all but the most recent, here's how:
$ ls -rtBecause rm -rf won't complain if you invoke it without arguments, this last idiom is idempotent: if there's only one file in the directory, it leaves it alone, so it's still safe to use.
three-days-ago
day-before-yesterday
yesterday
today
$ ls -rt | sed '$d'
three-days-ago
day-before-yesterday
yesterday
$ rm -rf $(ls -rt | sed '$d')
$ ls
today
No comments:
Post a Comment