0

I have an obligation to provide a document which just contains every source code file, with the file's name before each file.

Example:

=============================
src/com/example/Factory.java
=============================

public class Factory {
...
}

=============================
src/com/example/Worker.java
=============================

public class Worker {
...
}

It must contain all *.java files in the current folder, recursively.

How to easily generate such a file, using commonly available commands or a small script?

2

First write a small script called print-with-header.sh with the content below, and make it executable:

echo ""
echo "=================================================================="
echo $1
echo "=================================================================="
echo ""
cat $1

Then run:

find . -name "*.java" | xargs -L 1 ./print-with-header.sh
2

If you don't mind the appearance of the header, here's a quick way. First run set -o globstar in ksh93 or shopt -s extglob in bash (or nothing in zsh). Then:

tail -n +1 **/*.java

If you'd like something nicer for printing, you can use programs such as a2ps or enscript.

  • Nice! The header appears like this: ==> src/com/example/Factory.java <== – Nicolas Raoul Dec 2 '13 at 5:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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