Friday, April 19, 2024
 Popular · Latest · Hot · Upcoming
43
rated 0 times [  43] [ 0]  / answers: 1 / hits: 32526  / 3 Years ago, fri, july 2, 2021, 11:40:06

I have saved many documents as txt. I want to print them together so first I want them together in a single file. The order doesn't matter in this case.



I want a solution that does not involve typing the names of the files to be merged, but one that would just merge all txt files within the folder.



Can I do it with a command or some GUI?






I looked here. Don't know how to use join.


More From » filemanager

 Answers
1

Use cat with output redirection. Syntax: cat file [file] [[file] ...] > joined-file.



Example with just two files (you can have many more):



$ echo "some text in a file" > file1
$ echo "another file with some text" > file2
$ cat file1 file2 > mergedfiles
$ cat mergedfiles
some text in a file
another file with some text


In case you have "many documents", make use of shell globbing (patterns):



cat input-files-dir/* > joined-file


This will join all files in that directory to the current directory (preventing it to match the output file itself). It is totally independent to the use of cat and output redirection - it's just Bash providing all the files as arguments to cat.






File types



It will just glue (join) files together as you would do with paper and tape. It does not care about the actual file format being capable of handling this. It will work for text files, but not for PDFs, ODTs, etc. Well, it will glue them together, but it's not a valid PDF/ODT anymore.






Order of joining



As phoibos pointed out the shell globbing will result in alphabetical order of file names. This is how Bash and shell globbing works.






Addendum about input file is output file error



When the pattern of the input files matches the very same file as being output, this will cause an error. It's a safety feature. Example: cat *.txt > out.txt run the second time will cause this.



What you can do about it:




  • Choose a more specific pattern to match the actual input files, not matching the output name. Example: input files pattern *.txt with output file output.out will not collide.

  • Work in different directories. In the example above I've used a separate input-files-dir directory to place all files in, and output to the current working directory. This makes it impossible to get this error.


[#32875] Saturday, July 3, 2021, 3 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
fenddy

Total Points: 361
Total Questions: 103
Total Answers: 113

Location: Turkmenistan
Member since Sun, Aug 2, 2020
4 Years ago
fenddy questions
Tue, Nov 22, 22, 10:11, 1 Year ago
Tue, Sep 27, 22, 09:16, 2 Years ago
Wed, Dec 28, 22, 13:09, 1 Year ago
Fri, Jun 18, 21, 14:04, 3 Years ago
;