How to display the file contents recursively?

8

2

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

user1022209

Posted 2013-01-10T14:53:22.710

Reputation: 203

Answers

14

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

cottonke

Posted 2013-01-10T14:53:22.710

Reputation: 156

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 - – graceman9 – 2019-10-27T23:41:51.007

8

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

Satish

Posted 2013-01-10T14:53:22.710

Reputation: 412

1The {} \; is not needed after cat ... those are used only in find's -exec command. – None – 2013-01-10T15:25:51.147

^^ Right. corrected... – anishsane – 2013-01-10T15:47:31.337

4

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

glenn jackman

Posted 2013-01-10T14:53:22.710

Reputation: 18 546

1

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

Danstahr

Posted 2013-01-10T14:53:22.710

Reputation: 111

1

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

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

okobaka

Posted 2013-01-10T14:53:22.710

Reputation: 305