0

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
0

2 Answers 2

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 -- {} +
6
  • 1
    +1; or cat $(find ...)
    – Videonauth
    Commented Nov 24, 2017 at 4:07
  • It might make sense to use cat -- to prevent unwanted file option matches. Commented Nov 24, 2017 at 9:34
  • @RaphaelAhrens thanks, good point - will edit Commented Nov 24, 2017 at 13:12
  • @RaphaelAhrens - how could that happen when all file names returned by find start with . ? Commented Nov 24, 2017 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... Commented Nov 24, 2017 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
  • 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. Commented Nov 24, 2017 at 9:37

This site is temporarily in read-only mode and not accepting new answers.

Not the answer you're looking for? Browse other questions tagged .