Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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
share|improve this question

migrated from stackoverflow.com Jan 10 '13 at 18:42

This question came from our site for professional and enthusiast programmers.

up vote 12 down vote accepted

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

share|improve this answer
find . -name "*.java" -print0 | xargs -0 cat 

corrected command and removed {} \;

share|improve this answer
1  
The {} \; is not needed after cat ... those are used only in find's -exec command. – cottonke Jan 10 '13 at 15:25
    
^^ Right. corrected... – anishsane Jan 10 '13 at 15:47
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
share|improve this answer
find . -name "*.java" -exec cat {} \;
share|improve this answer
 grep -R -win --include='*\.java' '' * | less

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

share|improve this answer

You must log in to answer this question.

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