In a folder containing X files, I need to concatenate Y files (where X > Y) together into a single text file. I have the filenames (of Y files) that I need to concatenate.
4 Answers
You can use the cat command (see man cat for more information) to concatenate the text files.
If you want to create a new file
cat [FILE1] [FILE2]... > new_file
or if you want to append to an existing file use it like this
cat [FILE1] [FILE2]... >> file
-
2This, however, does not show how to read the filenames from a list and apply
catto them.2018-04-11 17:18:27 +00:00Commented Apr 11, 2018 at 17:18
If the Y filenames are listed in a list file, a simple combination of xargs and cat is enough:
xargs cat <list >>concatenation_of_files
In the case you've been careful and you've listed files one per line (to avoid problems with spaces in filenames), then just add a -d delimiter option:
xargs -d'\n' cat <list >>concatenation_of_files
(This assumes concatenation_of_files is initially inexistent or empty).
-
Cool! Obvious but didn't see it.forcefsck– forcefsck2012-02-17 21:48:27 +00:00Commented Feb 17, 2012 at 21:48
-
1You may use
>outputfileto truncate the output file. This is ok as it's the output fromxargsthat is redirected, not from the individualcatinvocations.2018-04-11 17:15:15 +00:00Commented Apr 11, 2018 at 17:15
Answers are using bash.
Let's say you want to concat json files in your directory:
cat *json > all.json
Including subdirectories:
shopt -s globstar
cat **/*json > all.json
However, if you have thousands to millions of files, you will encounter:
bash: /bin/cat: Argument list too long
In which case, do the following which also outputs the current file being processed:
shopt -s globstar
rm concat.json
find . -path "**/*.json" | while read -r file; do
echo -ne "\\r$file"
cat "$file" >> concat.json
done
Posted on this question, as this other question became closed.
-
This does not read the filenames from a list.2018-04-11 17:16:03 +00:00Commented Apr 11, 2018 at 17:16
-
Thanks for posting an answer but, yes, this isn't answering the question. Also, you can just do
for f in **/*.json; do cat "$f" >> concat.jsonorfind . -path "**/*.json" -exec cat {} >> concat.json.2018-04-11 17:31:31 +00:00Commented Apr 11, 2018 at 17:31 -
Problem is the question that this answers was marked as a duplicate of this question, which it is not exactly. Seems this question is then intended to become the one for accomplish its aims in the most broadest of sense.balupton– balupton2018-04-11 19:22:43 +00:00Commented Apr 11, 2018 at 19:22
(I know this is REALLY late and not actually an answer but a suggestion if you are doing this.)
Do not use the same file extension as the files that you are concatenating.
cat *.json > all.json.txt
Then come back and...
mv all.json.txt all.json
Not actually sure if this solves the problem in the question but this will avoid the looping issue.
-
Welcome to the site, and thank you for your contribution. Unfortunately, it is not clear which "looping issue" is being avoided; please consider adding more explanation.AdminBee– AdminBee2021-12-01 09:11:16 +00:00Commented Dec 1, 2021 at 9:11