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

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

Robert Thorne

Robert Thorne

Automotive & Future Transportation Editor

Robert Thorne covers electric vehicle innovations, autonomous driving systems, global mobility trends, and automotive engineering developments.

Share this article
Twitter Facebook Pinterest