Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pass hundreds of files from multiple subdirectories to cat?

Tags:

linux

bash

unix

I have a directory full of subdirectories and each subdirectory has some text files inside of them (i.e. depth is 1).

I'd like to cat all these files (in no particular order) into one file:

cat file1 file2.... fileN >new.txt

Is there a bash shell one-liner that could list all the files inside of these directories and pass them to cat?

like image 871
user1064903 Avatar asked Oct 29 '25 09:10

user1064903


2 Answers

find . -type f -print0 | xargs -0 cat

find will recursively search for files (-type f) and print their names as null-terminated strings (-print0).

xargs will read null terminated strings (-0) from stdin and pass them as arguments to cat

like image 123
Martin Avatar answered Nov 01 '25 06:11

Martin


How about this?

find . -name '*.txt' -exec cat {} \; > concatenated.txt

Granted it calls cat a bunch of times rather than just once, but the effect is the same.

like image 25
Chris Eberle Avatar answered Nov 01 '25 06:11

Chris Eberle



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!