Sysop: | Amessyroom |
---|---|
Location: | Fayetteville, NC |
Users: | 43 |
Nodes: | 6 (0 / 6) |
Uptime: | 108:24:46 |
Calls: | 290 |
Files: | 905 |
Messages: | 76,683 |
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.
OK, print0 is going to become standard ...
But – according to the new POSIX standard
([links to 2024 opengroup specs for find and xargs}) –
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.
find . -iname \*fred\* -type f -print0 |
xargs -0 -r -x -- ls -lt --
OK, print0 is going to become standard, but nowadays I already prefer
(when I use iname for my comfort)
find . -iname \*fred\* -type f -exec ls -lt -- {} +
If sufficiently many files accrue, find(1) will invoke ls(1) several
times, which will not produce the expected result. That may be unlikely
in this specific example, but it can happen in the general case.
Wait, you say, xargs(1) will also split its input across multiple invocations. I mean, that's very much the point of xargs. Which is why Helmut added the -x flag, which is supposed to prevent this behavior.
On 2024-08-13, Ralf Damaschke <rwspam@gmx.de> wrote:
find . -iname \*fred\* -type f -exec ls -lt -- {} +
If sufficiently many files accrue, find(1) will invoke ls(1) several
times, which will not produce the expected result. That may be
unlikely in this specific example, but it can happen in the general
case.
Wait, you say, xargs(1) will also split its input across multiple invocations. I mean, that's very much the point of xargs. Which
is why Helmut added the -x flag, which is supposed to prevent this
behavior.