Hmm. @don_crissti already gave the answer for grep in a comment. But since you said it wasn't really about grep
or ls
, I'm going to rewrite the command in question to not use those commands.
What I think you want is:
do-something-with $(produce-list-of-files)
where the output of one command should be dropped in as command line parameters to another command. There just happens to be a utility just for that, it is called xargs
(man page).
If the file names are "nice", we could do just
produce-list-of-files | xargs do-something-with
If the file names can contain spaces but are separated by newlines, we have to tell xargs
to not split on any whitespace, but only newlines:
produce-list-of-files | xargs -d '\n' do-something-with
If the file names can contain newlines too, the list has to be separated by NULs ('\0', byte with value zero), and we need an xargs
that supports it. At least some versions of various utilities support listing files separated by NULs instead of newlines, there's at least find -print0
, sort -z
and grep -Z
in the GNU versions of those tools. In xargs
the flag is --null
or -0
. So:
produce-list-of-files -0 | xargs -0 do-something-with
An example run with cat
and, well, ls -l
:
$ touch "abba acdc" "foo bar" $'new\nline'
$ echo -en "abba acdc\0foo bar\0new\nline\0" > list
$ cat list | xargs -0 ls -l
-rw-r--r-- 1 itvirta itvirta 0 Aug 2 01:23 abba acdc
-rw-r--r-- 1 itvirta itvirta 0 Aug 2 01:23 foo bar
-rw-r--r-- 1 itvirta itvirta 0 Aug 2 01:23 new?line
Unless GNU utilities are installed, your best bet is probably perl
on those traditional systems:
perl -MPOSIX -MFcntl -MFile::stat -le '
setlocale(LC_TIME, "C");
for (<*>) {
$s = lstat $_ or die "$_: $!\n";
print "$_ " . uc(strftime("%d-%b-%Y %I:%M:%S %p", localtime $s->mtime))
if S_ISDIR($s->mode)
}'
That's perl
's interface to the standard POSIX lstat()
system call that retrieves file metadata and strftime()
function to format dates.
See perldoc POSIX
, perldoc -f lstat
, perldoc -f stat
, man lstat
, man strftime
for details. We use the C locale for LC_TIME
so we get English month names and PM
/AM
regardless of the preferences of the user.
If zsh
is installed:
zsh -c 'zmodload zsh/stat
LC_ALL=C stat -nA times -LF "%d-%b-%Y %I:%M:%S %p" +mtime -- *(/) &&
for f t ($times) printf "%s\n" "$f: ${(U)t}"'
Above, we're using perl
's uc()
and zsh
's ${(U)var}
to convert the timestamps to uppercase. On GNU systems, you could have used %^b
for a all-uppercase month abbreviation, but it doesn't look like it's available on HP/UX.
Best Answer
Use
stat
on the-exec
action offind
:Change the format sequences of
stat
to meet your need.