Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

Re: spaces in filenames should be a crime.



Ray Andrews wrote on Sun, Mar 26, 2017 at 13:22:24 -0700:
> Gentlemen:
> 
> This function:
> 
>     mostrecent ()
>     {
>         ls -l ${(f)$( find . -type f -printf "%T@ %p\n" | sort -n | cut -d'
> ' -f 2- | tail -n 1 )}
>     }

You could replace the whole function with:

    mostrecent() { ls -l -- **/*(.om[1]) }

That glob qualifier restricts the match to files ('.'), sorts them by
modified date ('om'), and selects the first result ('[1]').

You can then use [1,3] to select several files.

> Testing it on this dummy directory tree:
> 
> ├── [   0]  a
> ├── [4.0K]  absolute junk
> │   └── [   0]  junk
> ├── [   0]  b
> └── [   0]  c
> 
> If the most recent file is under 'absolute junk', the function works, but it
> requires the ' ${(f) ... ' treatment, otherwise it barfs at the space in the
> directory name.  However, if I change things to ' tail -n 3 "  it shows me:
> 
> ls: cannot access './a ./absolute junk/junk ./c': No such file or directory
> 
> So whereas the ' ${(f) ' treatment fixes the one problem, it creates the
> other, which is obviously that everything is one string.  Can I have it both
> ways?  Protected spaces in the directory name, and still have multiple items
> for the listing? I've done stuff like this before, but using arrays.
>
> Or can printf handle the situation by itself?

As Vadim said, putting the $() in double quotes makes this example work.

Another option here is to use find -print0 with ${(0)}.  That only
matter if you need to DTRT when filenames contain NL characters.

Cheers,

Daniel



Messages sorted by: Reverse Date, Date, Thread, Author