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?

2 Answers 2

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

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

done > targetfile
  • 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, 2012 at 13:12
  • @slhck - looping? I dont see how
    – Nifle
    Aug 6, 2012 at 13:15
  • How do I use this: find . -type f -exec cat {} \; to put it into a single file?
    – Arpith
    Aug 6, 2012 at 13:24
  • @Arpith - find . -type f -exec cat {} \; > /path/to/your/outputfile
    – Nifle
    Aug 6, 2012 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, 2012 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


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


while read line; do
    login=$(echo $line | cut -d : -f 1)
        echo $login
done < /etc/passwd

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.