39

Using cat command as follows we can display content of multiple files on screen

cat file1 file2 file3

But in a directory if there are more than 20 files and I want content of all those files to be displayed on the screen without using the cat command as above by mentioning the names of all files.

How can I do this?

bvb
  • 633
  • 2
  • 7
  • 15

7 Answers7

50

You can use the * character to match all the files in your current directory.
cat * will display the content of all the files.
If you want to display only files with .txt extension, you can use cat *.txt, or if you want to display all the files whose filenames start with "file" like your example, you can use cat file*

Munim
  • 6,310
  • 2
  • 35
  • 44
  • 1
    It is working for current directory but If I want to display the content of files under a sub directory of current directory then how? – bvb Jan 22 '14 at 08:55
  • 4
    If it's just one level of subdirectory, use `cat * */*` – rojomoke Jan 22 '14 at 10:01
  • 2
    Otherwise do `find . -type f -exec cat {} \;` Be careful you don't have any non-character files, as you might screw up your display if you cat those. – rojomoke Jan 22 '14 at 10:08
  • 1
    @bvb that wasn't in your question, but yes, if you want to go to one subdirectory below too, you can do a `cat * */*`. – Munim Jan 22 '14 at 11:04
27

If it's just one level of subdirectory, use cat * */* Otherwise,

find . -type f -exec cat {} \;

which means run the find command, to search the current directory (.) for all ordinary files (-type f). For each file found, run the application (-exec) cat, with the current file name as a parameter (the {} is a placeholder for the filename). The escaped semicolon is required to terminate the -exec clause.

rojomoke
  • 3,765
  • 2
  • 21
  • 30
  • 1
    In cat * */* command for one level of subdirectory why to give * */* and how it would be interpreted? – bvb Jan 22 '14 at 11:59
  • `*` by itself expands to all files in your current directory. `*/*` (no spaces) expands to all files in all subdirectories in your current directory. `*/*/*` would match all files in directories within directories, and so on. This is all very basic stuff if you're going to be using Unix. – rojomoke Jan 22 '14 at 16:55
22

I also found it useful to print filename before printing content of each file:

find ./ -type f | xargs tail -n +1

It will go through all subdirectories as well.

Oleg Oleg
  • 525
  • 6
  • 10
2

If you want to do more then just one command called for every file, you will be more flexible with for loop. For example if you would like to print filename and it contents

for file in parent_dir/*.file_extension; do echo $file; cat $file; echo; done
gnom1gnom
  • 735
  • 7
  • 15
2

Have you tried this command?

grep . *

It's not suitable for large files but works for /sys or /proc, if this is what you meant to see.

name
  • 29
  • 2
1

You could use awk too. Lets consider we need to print the content of a all text files in a directory some-directory

awk '{print}' some-directory/*.txt
SuperNova
  • 25,512
  • 7
  • 93
  • 64
0

try this will work

cat Folder/*.txt

adel
  • 354
  • 3
  • 4