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've seen many questions and answers here using a construction along the lines of

list_dir=`ls -t /path/to/dir/`
for i in $list_dir; do

or

ls -t | while read i; do

Now, I know that you shouldn't use ls in scripts because it breaks easily; but I can't find a better way of operating on files in order from last-modified to most-recently-modified (or vice versa).

I can use something like:

find . -type f -printf '%T@ %p\n' | sort -n | cut -d ' ' -f 2- | while read i; do...

...but this will still break with any files that have newlines in their names, and is much uglier to boot. Is there a better way?

share|improve this question
add comment

2 Answers 2

up vote 6 down vote accepted

"Don't use ls in scripts" is a problem with POSIX ls "only"; for GNU ls see --quoting-style=.

GNU sort solves the problem with --zero-terminated.

If it must be compatible then you could use find ... -exec for passing one file name at a time to a script which does the escaping. If at least bash is available:

start cmd:> testfunc () { echo "${1//$'\n'/\n}"; }
start cmd:> testfunc a$'\n'b
a\nb
share|improve this answer
    
Ahh, --zero-terminated - thank you for that, I wish I could give you even more points! –  evilsoup May 27 '13 at 16:38
    
...OK, I'm a little confused by info ls: if I wanted a bulletproof script with ls, which --quoting-style should I use? –  evilsoup May 27 '13 at 16:48
    
@evilsoup I think --quoting-style=escape is the way to choose. –  Hauke Laging May 27 '13 at 16:57
    
Now, to create some files with newlines in their names to test it :P –  evilsoup May 27 '13 at 17:05
    
@evilsoup touch filename. You can simply press Enter after " or ' (and close the quotes in one of the following lines). Or do this: touch a$'\n'b (in bash) –  Hauke Laging May 27 '13 at 17:10
show 2 more comments

The easiest way by far is to use zsh. The glob qualifier om sorts matches in reverse chronological order; use Om for chronological order.

for x in /path/to/dir/*(Nom); do …

The N glob qualifier causes the pattern to expand to an empty list if the directory is empty. Make this *(DNom) to match dot files.

share|improve this answer
    
Dang, this is the first time I've been tempted to bother looking at other shells - that's very nice. –  evilsoup May 27 '13 at 23:01
add comment

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.