Show tail of files in a directory?
Solution 1
Barring your files don't include strange characters in their names, such as spaces, new lines, etc. A simple pipe to tail -n 200
should suffice.
Example
Sample data.
$ touch $(seq 300)
Now the last 200:
$ ls -l | tail -n 200
You might not like the way the results are presented in that list of 200. For that you can control the order of the results that ls
outputs through a variety of switches. For example, the data I've generated is numeric. Using the above method the results are shown like so:
$ ls -l | tail -n 12
-rw-rw-r-- 1 saml saml 0 Nov 28 11:50 89
-rw-rw-r-- 1 saml saml 0 Nov 28 11:50 9
-rw-rw-r-- 1 saml saml 0 Nov 28 11:50 90
-rw-rw-r-- 1 saml saml 0 Nov 28 11:50 91
-rw-rw-r-- 1 saml saml 0 Nov 28 11:50 92
-rw-rw-r-- 1 saml saml 0 Nov 28 11:50 93
-rw-rw-r-- 1 saml saml 0 Nov 28 11:50 94
-rw-rw-r-- 1 saml saml 0 Nov 28 11:50 95
-rw-rw-r-- 1 saml saml 0 Nov 28 11:50 96
-rw-rw-r-- 1 saml saml 0 Nov 28 11:50 97
-rw-rw-r-- 1 saml saml 0 Nov 28 11:50 98
-rw-rw-r-- 1 saml saml 0 Nov 28 11:50 99
But maybe you want this in true numeric order, so you could use the -v
switch:
$ ls -lv | tail -n 12
-rw-rw-r-- 1 saml saml 0 Nov 28 11:50 289
-rw-rw-r-- 1 saml saml 0 Nov 28 11:50 290
-rw-rw-r-- 1 saml saml 0 Nov 28 11:50 291
-rw-rw-r-- 1 saml saml 0 Nov 28 11:50 292
-rw-rw-r-- 1 saml saml 0 Nov 28 11:50 293
-rw-rw-r-- 1 saml saml 0 Nov 28 11:50 294
-rw-rw-r-- 1 saml saml 0 Nov 28 11:50 295
-rw-rw-r-- 1 saml saml 0 Nov 28 11:50 296
-rw-rw-r-- 1 saml saml 0 Nov 28 11:50 297
-rw-rw-r-- 1 saml saml 0 Nov 28 11:50 298
-rw-rw-r-- 1 saml saml 0 Nov 28 11:50 299
-rw-rw-r-- 1 saml saml 0 Nov 28 11:50 300
You have to pay special attention to ls
implementations, since some may include one switch while another may not. Consult the man pages on the respective systems to get the full list of available switches.
Solution 2
In zsh:
print -lr -- *([1,200]) # print the name of the first 200 files in alphabetical order
print -lr -- *(om[1,200]) # print the name of the 200 most recent files
print -lr -- *(oN[1,200]) # print the name of 200 files
The oN
variant reads files in directory order, so it doesn't waste time sorting them.
If you want to peek at the file contents as well:
tail *(oN[1,200])
With any shell, to print the names of 200 files without reading all the file names and sorting them:
ls -lU | head -n 200
To print the names of the 200 most recent files:
ls -lt | head -n 200
If you want to move the files into a bunch of directories, see Batch copy to multiple directories, How can I find files and then use xargs to move them?, Batch copying/moving files in unix?, Batch file move to directory structure based on filename
Related videos on Youtube
![blarg](https://i.stack.imgur.com/qjDLD.gif?s=256&g=1)
blarg
Updated on September 18, 2022Comments
-
blarg almost 2 years
I have a directory that is too huge to browse by any means. Grepping returns argument lists too long, WINSCP crashes when trying to open the directory.
Is there a way I can view just the last 200 files in the directory. Like 'tailing' the directory?
-
erch over 10 years@val0x00ff "Why one shouldn't parse the output of ls (mywiki.wooledge.org/ParsingLs) explains why this wouldn't be the best idea
-
Marek Zakrzewski over 10 years@chirp in scripts you should not use
ls
indeed. In this case you can because the target is the human eye not the system. -
erch over 10 years@val0x00ff yes-ish, if you could show me where this is/was defined in the original posting
-
Marek Zakrzewski over 10 years@chirp
ls
is perfectly fine for listing files and directories in combination with other flags. It is however not suited to be used in scripts because it's purpose is to make a human-readable listing. Also the OP doesn't mention it needs a script. That is the reason I usedls
-
slm over 10 yearsGuys,
ls
is fine to use. I know ppl make a big deal of it on this site, and it can be problematic to parse it, but it's a perfectly fine tool to use andgrep
the output from. The warnings from that wiki are things that you should appreciate about "potential" problems you might run into when parsing it. -
slm over 10 yearsCan you explain the nature of the files in this directory? I find it odd that such a trivially simple Q has had no answers, other than yours that you deleted. This tells me that your Q is poorly being asked. A simple
ls -l | tail -n 200
will do what you want. -
blarg over 10 yearsIs there a remote file-browser that can handle huge directories?
-