Tail multiple files and output as additional column with 'find' results

6,297

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.
Share:
6,297

Related videos on Youtube

Abrar
Author by

Abrar

Updated on September 18, 2022

Comments

  • Abrar
    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 of tail.

    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
    Abrar over 10 years
    Is 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
    janos over 10 years
    You 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
    Abrar over 10 years
    All inline: I'd like tail to be appended to ls -l as an additional column rather than have tail add a newline.
  • Dmitry Alexandrov
    Dmitry Alexandrov over 10 years
    And why downvoted, may I ask?
  • Abrar
    Abrar over 10 years
    One 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
    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
    janos over 10 years
    True, I fixed the issues with spaces. The <(...) in paste <(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 in man bash under the Process Substitution section.