59

I want to cat a file in current folder and all files in all subfolders (and subsubfolders).

Here is my directory structure

$ tree
.
├── f
│   └── foo
└── yo

I want to cat foo and yo.

I've tried this command but did not work:

cat */*

It just cats foo.

2
  • Dup. unix.stackexchange.com/q/76418/6622 You search before asking Commented May 24, 2013 at 4:51
  • Super dangerous to do this because the files may contain commands. You should always run a file on the file first Commented Nov 16, 2016 at 21:46

2 Answers 2

67

try:

   find . -type f -exec cat {} +
3
  • 12
    What's the + for? I used to end these type of commands with \;. Commented Jun 1, 2017 at 20:31
  • 2
    @StephenRasku - stackoverflow.com/questions/6085156/… Commented Sep 11, 2018 at 19:29
  • 6
    -exec foo {} \; is foo x; foo y; foo z;, but -exec foo {} + is foo x y z. Commented Dec 8, 2020 at 0:04
25

cat accepts multiple arguments, so you can:

  cat * */*

to cat everything in the current directory and in all subdirectories. You can also

  cat * */* */*/*

and so on, if you want.

Note, of course, that your shell is translating those '*'s into a list of files then passing that whole list to cat.

3
  • 3
    With shopt -s globstar enabled on bash 4+, you can use cat ** instead of the second one. Commented May 25, 2013 at 11:07
  • 3
    That's not a useful solution, as you need to know how many layers deep to go. Commented Oct 25, 2016 at 15:06
  • Moreover, the directories will be processed Commented Nov 22, 2018 at 18:32

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.