How to display the file contents recursively?

30,541

Solution 1

You can use find (man page) to accomplish this:

find -name "*.java" -exec cat {} \;

You can also add a -print before the -exec to print the file name before each cat operation

Solution 2

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

Solution 3

shopt -s globstar
cat **/*.java >> all_course.txt

That all_course file will be a bit of a mess. You probably want to add in some headers or footers:

for f in **/*.java; do
    echo "/* *********************************"
    echo " * $f"
    echo " * *********************************/"
    echo ""
    cat "$f"
    echo ""
    echo "/* *********************************"
    echo " * $f"
    echo " * *********************************/"
    echo ""
    echo ""
done > all_course.txt

Solution 4

find . -name "*.java" -exec cat {} \;

Solution 5

 grep -R -win --include='*\.java' '' * | less

Will show line no. also, for easy reading. Manipulate with grep switches for better results.

Share:
30,541

Related videos on Youtube

user1022209
Author by

user1022209

Updated on September 18, 2022

Comments

  • user1022209
    user1022209 almost 2 years

    I always have to submit the source codes in my printed assignment report. I have to copy and paste my course codes into the document and I find that it is an annoying task.

    I want to solve this "copy and paste" problem. Therefore I did it with cat like that but it only works in the current directory. I hope it can display the file contents recursively.

    ls -R *.java | xargs cat >> all_course.txt
    
  • Cannon Sloan
    Cannon Sloan over 11 years
    The {} \; is not needed after cat ... those are used only in find's -exec command.
  • anishsane
    anishsane over 11 years
    ^^ Right. corrected...
  • graceman9
    graceman9 over 4 years
    Adding | vim - in the end will allow you to navigate/grep through text and also highlight syntax if needed, so find . -name "*.java" -exec cat {} \; | vim -
  • Raghavendra B N
    Raghavendra B N about 2 years
    To see the file name and to see more easily where a file ends: find "$1" -type f -print -exec cat {} \; -exec echo "###" \;