Show contents of many files

The two standard commands head and tail print a header with the file name if you pass them more than one file argument. To print the whole file, use tail -n +1 (print from the first line onwards, i.e. everything).

Here, it looks like you want to see every file except the single one whose name begins with a dot. Dot files are “hidden” under unix: they don't appear in the default output of ls or in a wildcard match. So matching every non-hidden files is done with just *.

tail -n +1 *

(Strictly speaking, tail -n +1 -- * is needed in case one of the file names begins with a -.)


You can do it all in one with find:

$ find . -type f -not -name .htaccess -printf "\n%p\n" -exec cat {} \;

That tells find to find all files (-type f) in the current directory (.) except (-not) one named .htaccess (-name .htaccess). Then it prints (-printf) a newline followed by the filename (%p), and then runs cat on the file (-exec cat {} \;). That will give you output like:

test/test3
Line 1

test/test2
Line 1

test/test1
Line 1
Line 2
Line 3

If you do this often it might be worth sticking it in a shell script or a function; I have one named cats that does exactly that:

#!/bin/bash
for filename; do
    echo "\033[32;1m$filename\033[0m"
    cat "$filename"
    echo
done

It loops over each filename argument, prints out the filename (in bold green), and then cats the file:

Example screenshot

So then the command would just be:

$ find . -type f -not -name .htaccess -exec cats {} \+

To show content of all files in the current folder, try:

grep -vI "\x00" -- *

and similar, but recursively:

grep -vIr "\x00" -- .

The format would be: filename: content.

To have similar format as suggested, it would be:

grep -rvl "\x00" -- * | while read file; do printf "\n\n#### $file ####\n"; cat $file; done

Side notes:

  • Using NUL (\x00) in above examples prevents displaying binary files (which -I is actually doing, but we've to still use some pattern).
  • Using wildcard (*), it automatically ignores hidden files such as .htaccess.

See also: grep: display filename once, then display context with line numbers at Unix SE