ls -ltr | tail -n 3

what this command stands for?

the ls -ltr is to list programs sorted by time
the tail -n 3 is to list the last three lines in the file

what does this mean by last three lines in file?
https://www.man7.org/linux/man-pages/man1/ls.1.html
https://www.man7.org/linux/man-pages/man1/tail.1.html

If you're still confused, start by entering the command
ls -ltr

Then try
ls -ltr | tail -n 3
and compare the output you get.


yep, I dont ask questions here before I try myself first,

the ls -ltr gives the ls -l files in the direrctory but with time sorted (reverse)

ls -ltr | tail -n 3
list all the lines sorted by time reverse and only show the last threee lines from this list ,

it seems easy while using the PC, but during exam we can not use PC

my question was not about what these commands show when we excute it (your links is far away from my questoin) , the issue was about lines and what the point of showing three lines


Thanks for your help anyway
> it seems easy while using the PC, but during exam we can not use PC
Good to know that you're sitting in an exam hoping gain an advantage over your peers.
I'll be less hasty in future.

> what does this mean by last three lines in file?
stdin and stdout are files of a sort - well file descriptors at any rate.
Pipes (aka |) are also file descriptors.

Files, pipes and redirection are all pretty much the same thing to a lot of Unix/Linux utilities.
tail -3 some_file.txt
tail -3 < some_file.txt
cat some_file.txt | tail -3


> the issue was about lines and what the point of showing three lines
Like exam questions have a point.

I suppose the point would be for you to demonstrate your understanding of what a pipe was, rather than worrying about the detail of what 'ls' and 'tail' do.

the tail -n 3 is to list the last three lines in the file
It would be more accurate to say it outputs the last 3 lines of the input. In this case, the input to tail is the output from ls -ltr. So the final result is the ls -l output for the 3 most recent files, from oldest to newest.

If you're still having trouble understanding this, try:
1
2
ls -ltr > tempFile
tail -n 3 < tempFile


tempFile contains what goes through the pipe when you do ls -ltr | tail -n 3

Registered users can post here. Sign in or register to post.