run ls but skip first 3 files

Let's say ls -t returns:

1
2
3
4
5
6

How can I skip the first 3 results? So the result is only:

4
5
6

I am aware I can run ls -t | head -3 but that will simply take the first 3 lines, but I need to skip the first 3 lines.

1
2
3
2

1 Answer

Use tail:

ls -tq | tail -n+4

Please note that I added -q option to ls to prevent issues with newlines in filenames. While this might be the default for ls output to terminal, it is not the default when piped.

Also note, that this is okay for visual output but not for scripting, as files with newlines won't work, because newlines get replaced by ?.

In general you should avoid parsing ls.

6

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

You Might Also Like