0

This question already has an answer here:

to be more specific ,I want to display contents of files from output of find command,I tried the following commands but they don't get my work done

  • cat < find . -name "*.txt"
  • find . -name "*.txt" | cat

marked as duplicate by don_crissti, Romeo Ninov, Stephen Rauch, roaima, G-Man Nov 24 '17 at 22:11

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

4

Either

find . -name "*.txt" | xargs cat --

or (better, if you have GNU find)

find . -name "*.txt" -print0 | xargs -0 cat --

or

find . -name "*.txt" -exec cat -- {} +
  • 1
    +1; or cat $(find ...) – Videonauth Nov 24 '17 at 4:07
  • It might make sense to use cat -- to prevent unwanted file option matches. – Raphael Ahrens Nov 24 '17 at 9:34
  • @RaphaelAhrens thanks, good point - will edit – steeldriver Nov 24 '17 at 13:12
  • @RaphaelAhrens - how could that happen when all file names returned by find start with . ? – don_crissti Nov 24 '17 at 13:47
  • @Videonauth (and whoever upvoted your comment) - that will fail if filenames contain any IFS chars - see the answers here - newlines in file names exist only in theory but spaces are pretty common in real life... – don_crissti Nov 24 '17 at 13:51
-1

You can use below the below command to display contents of files

Method 1:

find . -type f -iname "*.txt" -exec cat {} \;

Method 2:

ls -ltr *.txt | awk '{print "cat" " " $9}' | sh
  • 1
    The second method has some issues. First ls behaves very differently on different OSs. Second building shell scripts on the run is a little bit of over doing it. – Raphael Ahrens Nov 24 '17 at 9:37

Not the answer you're looking for? Browse other questions tagged or ask your own question.