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' files 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' _ {} \;
Share:
12,996

Related videos on Youtube

Admin
Author by

Admin

Updated on September 18, 2022

Comments

  • Admin
    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 with cat *.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
    Kusalananda about 6 years
    >> can be > in the first find call.
  • αғsнιη
    αғsнιη about 6 years
    @Kusalananda won't that truncate the mergedfile if ARG_MAX exceed?
  • Kusalananda
    Kusalananda about 6 years
    The > redirects the output of find, not cat. The cat 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
    Kusalananda about 6 years
    Actually, 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нιη
    αғ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
    Kusalananda about 6 years
    Ah, you're absolutely correct. I didn't notice you were searching for directories!