How to concatenate files from different sub-directories?
12,996
try with
find /path/to/source -type f -name '*.txt' -exec cat {} + >mergedfile
find all '*.txt' f
iles in /path/to/source
recursively for sub-directories and concatenate all into one mergedfile
.
To concatenate each sub-directories files within its directory, do:
find . -mindepth 1 -type d -execdir sh -c 'cat $1/*.txt >> $1/mergedfile' _ {} \;
Related videos on Youtube
![Admin](/assets/logo_square_200-5d0d61d6853298bd2a4fe063103715b4daf2819fc21225efa21dfb93e61952ea.png)
Author by
Admin
Updated on September 18, 2022Comments
-
Admin almost 2 years
I have a large folder containing many sub-directories each holding many
.txt
files. I want to concatenate all of these files into one.txt
file. I am able to do it for each of the sub-directories withcat *.txt>merged.txt
, but I am trying to do it for all of the files in the large folder. How do I do this? -
Kusalananda about 6 years
>>
can be>
in the firstfind
call. -
αғsнιη about 6 years@Kusalananda won't that truncate the mergedfile if ARG_MAX exceed?
-
Kusalananda about 6 yearsThe
>
redirects the output offind
, notcat
. Thecat
command ends at the+
, and you can't do redirections in-exec
without using a child shell (sh -c
). In your second example, you won't need it either as you do one directory at a time. -
Kusalananda about 6 yearsActually, that second example won't work. Since
-execdir
is already executing with the directory as the working directory, you should get rid of$1/
in the command. -
αғsнιη about 6 years@Kusalananda your first point about using
>
instead of>>
in first command is right but$1/
is needed in second command and that works I tested before. note that execdir is changing for the find not for the child-shell I used there -
Kusalananda about 6 yearsAh, you're absolutely correct. I didn't notice you were searching for directories!