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 know that using "ls -R ." in bash you can recursively list the filenames.

Can I use some similar command to list the contents of each file just the way their names are displayed using the ls command. I mean to a single file or stream like the standard output.

Is there a way I can format the output from such a command? Like say, add something like a few blank lines between the contents of each of the files for better readbility?

share|improve this question
find . -type f | while read file;
do
  # Here you can do whatever you like
  # Like output a few empty lines
  echo
  echo

  # output filename
  echo $file      
  # this prints the contents of the file to STD OUT
  cat $file;

done > targetfile
share|improve this answer
    
But how do you get everything into a single file? When I tried this, it would go into an endless loop, cating the same files all over again. – slhck Aug 6 '12 at 13:12
    
@slhck - looping? I dont see how – Nifle Aug 6 '12 at 13:15
    
How do I use this: find . -type f -exec cat {} \; to put it into a single file? – Arpith Aug 6 '12 at 13:24
    
@Arpith - find . -type f -exec cat {} \; > /path/to/your/outputfile – Nifle Aug 6 '12 at 13:27
    
@Nifle: I think the command is listing the same files' contents recursively rather than going in depth through the filesystem heirarchy and listing the contents of various files. – Arpith Aug 6 '12 at 13:34

Create a meta-script with find and pipe it to sh.

$ find /home/jaroslav/tmp/su/  2>/dev/null \
    -printf 'echo -e \\\\n\\\\n%p\ncat "%p"\n' |sh

out:

/home/jaroslav/tmp/su/
cat: /home/jaroslav/tmp/su/: Is a directory


/home/jaroslav/tmp/su/diff.tar.gz
J▒▒P▒▒M
6▒M)FR▒▒▒▒▒▒F2▒/9▒e▒▒s]▒N▒h▒▒ޫzr▒▒hD▒▒Z&▒▒X▒▒|*▒o▒▒z▒▒▒▒|x߁▒▒E▒▒▒4▒▒Kऺ▒▒J▒-▒B▒▒▒Z▒▒▒?▒▒▒▒▒
        ▒▒P▒▒鿶▒▒JF▒j▒=Z▒?%▒▒▒▒▒▒▒▒▒▒▒{▒▒▒M▒▒▒▒$▒▒q(

/home/jaroslav/tmp/su/while
while read line; do
    login=$(echo $line | cut -d : -f 1)
        echo $login
done < /etc/passwd
share|improve this answer

You must log in to answer this question.

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