bash: listing files in date order, with spaces in filenames

Posted by Jason Judge on Stack Overflow See other posts from Stack Overflow or by Jason Judge
Published on 2011-01-03T10:48:37Z Indexed on 2011/01/03 10:54 UTC
Read the original article Hit count: 280

Filed under:
|
|
|
|

I am starting with a file containing a list of hundreds of files (full paths) in a random order. I would like to list the details of the ten latest files in that list. This is my naive attempt:

ls -las -t `cat list-of-files.txt` | head -10

That works, so long as none of the files have spaces in, but fails if they do as those files are split up at the spaces and treated as separate files.

I have tried quoting the files in the original list-of-files file, but the here-document still splits the files up at the spaces in the filenames.

The only way I can think of doing this, is to ls each file individually (using xargs perhaps) and create an intermediate file with the file listings and the date in a sortable order as the first field in each line, then sort that intermediate file. However, that feels a bit cumbersome and inefficient (hundreds of ls commands rather than one or two). But that may be the only way to do it?

Is there any way to pass "ls" a list of files to process, where those files could contain spaces - it seems like it should be simple, but I'm stumped.

© Stack Overflow or respective owner

Related posts about bash

Related posts about shell