Assume I have unknown number of files in a directory. I want to get the contents inside each file along with the file-name as in below example.

file1 "content in file one"
file2 "content in file two"
file3 "content in file three"
....... .......................
filen "content in file n"

How can I do this?


Assume the files are textual, and not too big (e.g. less than 10000 lines).

Then you might try using head(1) (maybe as head -vn -0 file* with GNU head) like this

head -10000 file1 file2 file3

Remember that some files are binary, that is they contain something else than text. For example, a core dump, an sqlite database file, a PNG image, an executable file, or even some OpenDocument file created with LibreOffice. On such files, using head (or cat(1)) don't give nice results, because they are not text files but contain a sequence of almost arbitrary bytes.

You might be also interested by the file(1) command. It tries to guess what is the content type of a given file. Read also about media types (or MIME types)

Remember that on Unix like system a file is actually an i-node and could have several names (see inode(7), link(2), ln(1) for more). The name of a file don't means much. A file basically is a sequence of bytes with some way to name it. How that sequence is used depends upon the program accessing the file.

You could be interested by xdg-open which tries to show, in a graphical manner, various kind of files (it is adapting to the content of those files).

Read also about globbing. See glob(7). Maybe learn about the for builtin of your shell. Read about bash looping constructs.

  • 2
    With GNU head, -n -0 for all lines – muru Apr 18 at 5:25
  • 1
    With GNU head, better with head -vn -0 file*, so the header is displayed even if there's only one file. – Stéphane Chazelas Apr 18 at 5:40
  • @HirSK: I don't have that much time to teach you Linux command line. Get a book about it. BTW Linux has directories (not folders). Folders are just the thing you see in your GUI – Basile Starynkevitch Apr 18 at 5:41

For that specific output format, you can do:

awk '{print substr(FILENAME, 3)" \""$0"\" "}' ./file*

Which would print the name of the file followed by the content of the line inside quotes for each line of the file.

We add a ./ prefix which we strip with substr() to work around the problem that awk considers arguments like filex=y.txt as a variable assignment instead of a file name.

If the files already contain " characters and you want to display them as \" (and \ as \\ to remove ambiguities):

awk '{gsub(/[\\"]/, "\\\\&"); print substr(FILENAME, 3)" \""$0"\" "}' ./file*

(that doesn't address " in the file name itself).


This does the trick:

for f1 in *; do echo -n "$f1"" "\"; cat "$f1"; echo "\""; done

It loops through all files in the directory, then, for each file shows its name followed by its contents in quotation marks.

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.