Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I often do something like this:

tail -f logs/`ls -rt logs | tail -1`/debug.log

I would like to have a macro for ls -rt <some_dir> | tail -1, but it needs to work in the context of the current command line (<some_dir> being pulled from the current command line). Is there a way to do that?

Something like:

tail -f logs/$(LAST)/debug.log
share|improve this question
What have you tried so far? Why didn't shell functions work for you? – Daniel Beck Jun 6 '12 at 22:09
So, I don't know how to get the current command line context outside of a bash completion function. Corrected my question to add "logs" in the ls -rt command to make this more clear. – dpb Jun 6 '12 at 22:19
And writing a script named (perhaps) taild that takes the directory name as an argument and then executes: tail -f logs/$(ls -rt "$1" | tail -1)/debug.log isn't an acceptable solution? – Fran Jun 8 '12 at 20:39
Yes, it's the only thing I've thought of. I just hit kind of a wall in my knowledge of bash and wanted to see if there was something out there I didn't know about. Thx. – dpb Jun 12 '12 at 17:22
up vote 1 down vote accepted

If you're not tied to determining <some_dir> within a sub-command, you can achieve the same effect with...

tail -f `ls -drt logs/* | tail -1`/debug.log

The -d causes ls to include full paths of matches, but you need the trailing /* so that it returns the files within the directory instead of the directory itself.

share|improve this answer
or even ls -drt logs/*/debug.log | tail -1 inside the subshell. either way. well, that simplifies the command line, thanks! I'll accept this for now as it appears there is no way to do exactly what I was asking for. – dpb Jun 12 '12 at 17:27

Using $0 will get you the current command and $* will get the parameters. Try echo $0 $* to see if that helps. Obviously you can assign those to other variable if necessary.

share|improve this answer
Good idea, but unfortuately this doesn't really help the case I'm looking for. echo foo bar | echo $0 $* prints bash (the $0 of the invoked shell, not of my current command line) – dpb Jun 12 '12 at 17:24

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.