Tail multiple files and output as additional column with 'find' results
Solution 1
Here you go:
find . -iname 'updated.txt' -exec ls -l {} \; -exec tail -n1 {} \;
A simpler alternative but with less control over the formatting of ls
:
find . -iname 'updated.txt' -ls -exec tail -n1 {} \;
UPDATE
As you said in comments, you prefer to have the ls
output and the tail
output side by side for each file.
You could run a subshell for each match to echo
the output of the two commands side by side:
find . -iname 'updated.txt' -exec sh -c 'echo $(ls -l "{}") $(tail -n1 "{}")' \;
Similarly, you could use paste
:
find . -iname 'updated.txt' -exec bash -c 'paste <(ls -l "{}") <(tail -n1 "{}")' \;
Or you could run the two find
commands in parallel and paste their output side by side like this:
paste \
<(find . -iname 'updated.txt' -exec ls -l {} \;) \
<(find . -iname 'updated.txt' -exec tail -n1 {} \;)
Solution 2
$ find . -iname 'updated.txt' -printf '%M %u %g %s %Tc %p\t' -exec tail -n1 {} \;
Output will be like:
-rw-r--r-- user group 4853 2013-12-22T00:58:32 MSK ./path/updated.txt Last line of the updated.txt.
Related videos on Youtube
Abrar
Updated on September 18, 2022Comments
-
Abrar over 1 year
Would like to pull the last line of text from a specific (multiple) files in a directory. Additionally, I'd like the results to be in a more traditional column format (like
ls
) with path, rather than the typical format oftail
.From the following results {1}, I'd like to append as an additional column the last row of data {2}
Where {1} =
find . -iname 'updated.txt'
, and {2} =tail -n1
I'm using
bash
in Mavericks -
Abrar over 10 yearsIs there any way to keep all the results on the same line however? Currently each time tail runs, it places each result on a new line.
-
janos over 10 yearsYou want first a listing of
ls -l
, one line per file, plus at the end one more line with the content of the last lines pasted together like columns, like that? -
Abrar over 10 yearsAll inline: I'd like
tail
to be appended tols -l
as an additional column rather than havetail
add a newline. -
Dmitry Alexandrov over 10 yearsAnd why downvoted, may I ask?
-
Abrar over 10 yearsOne thing to note is that this won't work out of the bag on Mac: printf isn't supported in find. After installing findutils it works like a champ. Thanks for the help.
-
Dmitry Alexandrov over 10 years@Kris You’re welcome. :) As for me, now I know that ‘Mavericks’ is OS X release (yes, it was worth mentioning).
-
janos over 10 yearsTrue, I fixed the issues with spaces. The
<(...)
inpaste <(cmd1) <(cmd2)
is called Process Substitution. The idea is replacing<(...)
with a named pipe, which contains the output of the commands inside. You can read about it inman bash
under the Process Substitution section.