Sujet : Re: IFS=$'\n'
De : nn.throttle (at) *nospam* xoxy.net (Helmut Waitzmann)
Groupes : comp.unix.shellDate : 13. Aug 2024, 13:14:21
Autres entêtes
Organisation : A noiseless patient Spider
Message-ID : <83a5hgad4i.fsf@helmutwaitzmann.news.arcor.de>
References : 1
User-Agent : Gnus/5.13 (Gnus v5.13) Emacs/26.1 (gnu/linux)
Lawrence D'Oliveiro <
ldo@nz.invalid>:
However, if you change this to >
>
IFS=$'\n'
>
then this can make things much more convenient (provided you can be sure there are no newlines in your file names). For example, I can do >
>
ls -lt $(find . -type f -iname \*fred\*)
>
to search for all filenames containing “fred” in the hierarchy rooted at the current directory, and display them in reverse chronological order. >
Even if one is sure there are no linefeeds in the file names a cautious design could do (
IFS=$'\n' &&
ls -lt -- $(find . -name \*$'\n'\* ! -prune -o \
! -name \*$'\n'\* -iname \*fred\* -type f -print
)
)
That will work if there are really no linefeeds in file names. And if there inadvertently were any linefeeds in file names it would at least not list file names that don't match the “\*fred\*“ file name pattern. But – according to the new POSIX standard (<
https://pubs.opengroup.org/onlinepubs/9799919799/utilities/find.html#top> and <
https://pubs.opengroup.org/onlinepubs/9799919799/utilities/xargs.html#top>) – one can use “xargs“ to get rid of any linefeed trouble at all: find . -iname \*fred\* -type f -print0 |
xargs -0 -r -x -- ls -lt --
That will work with any file names, even those containing a linefeed character.