Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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?

share|improve this question
1  
It would be as easy as ls -l /path/to/dir | tail -n200 –  val0x00ff Nov 28 '13 at 11:25
    
@val0x00ff "Why one shouldn't parse the output of ls (mywiki.wooledge.org/ParsingLs) explains why this wouldn't be the best idea –  erch Nov 28 '13 at 11:54
2  
@chirp in scripts you should not use ls indeed. In this case you can because the target is the human eye not the system. –  val0x00ff Nov 28 '13 at 12:05
    
@val0x00ff yes-ish, if you could show me where this is/was defined in the original posting –  erch Nov 28 '13 at 12:08
1  
@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 used ls –  val0x00ff Nov 28 '13 at 12:16

2 Answers 2

up vote 4 down vote accepted

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.

share|improve this answer

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

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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