I just called this command ls -l | tail +3. Firstly, ls -s, produces 3 lines, adding piping strips first line and then prints each file names on a separate line. How does it do that? It doesn't make sense.

Also, I thought tail filename displays last 10 lines. How does + (and minus) plays into this equation?

Same thing about head? How does it work?

link|improve this question

50% accept rate
10  
What does your man page say? When you type man tail, what response do you get? What part of that do you need help understanding? – S.Lott Oct 28 '09 at 20:13
feedback

migrated from stackoverflow.com Oct 28 '09 at 20:49

This question came from our site for professional and enthusiast programmers.

3 Answers

From the tail man page:

The tail utility displays the contents of file or, by default, its standard input, to the standard output.

The display begins at a byte, line or 512-byte block location in the input. Numbers having a leading plus ("+") sign are relative to the beginning of the input, for example, "-c +2" starts the display at the second byte of the input. Numbers having a leading minus ("-") sign or no explicit sign are relative to the end of the input, for example, "-n 2" displays the last two lines of the input. The default starting location is "-n 10", or the last 10 lines of the input.

So in your case, tail +3 (the -n is implied) means start at the 3rd line of the input (ls -l) and print the rest. For example:

ls -l output:

total 0
-rw-r--r--  1 carl  staff  0 Oct 28 13:18 file1
-rw-r--r--  1 carl  staff  0 Oct 28 13:18 file2
-rw-r--r--  1 carl  staff  0 Oct 28 13:18 file3
-rw-r--r--  1 carl  staff  0 Oct 28 13:18 file4
-rw-r--r--  1 carl  staff  0 Oct 28 13:18 file5
-rw-r--r--  1 carl  staff  0 Oct 28 13:18 file6

ls -l | tail +3 output:

-rw-r--r--  1 carl  staff  0 Oct 28 13:18 file2
-rw-r--r--  1 carl  staff  0 Oct 28 13:18 file3
-rw-r--r--  1 carl  staff  0 Oct 28 13:18 file4
-rw-r--r--  1 carl  staff  0 Oct 28 13:18 file5
-rw-r--r--  1 carl  staff  0 Oct 28 13:18 file6

Same output, just with the first couple of lines stripped off.

link|improve this answer
feedback

From the manpage:

If the first character of N (the number of bytes or lines) is a `+', print beginning with the Nth item from the start of each file, other- wise, print the last N items in the file. N may have a multiplier suf- fix: b 512, k 1024, m 1024*1024.

link|improve this answer
Beaten by Carl, but liked the formulation in my man page better, so leaving it as is. – Anonymous Oct 28 '09 at 20:17
feedback

Answering:

Firstly, ls -s, produces 3 lines, adding piping strips first line and then prints each file names on a separate line. How does it do that? It doesn't make sense.

The answer is that the ls commands looks at its standard output and checks whether it is connected to a tty. If it is, then it formats for display purposes. If it is not (for example, the output is a file or pipe), then it prints one entry per line.

  • The option '-C' forces multi-column output (as if going to a terminal).
  • The option '-1' forces single-column output (as if going to a pipe).
link|improve this answer
feedback

Your Answer

 
or
required, but never shown

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