LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   combine multiple files into one file? (https://www.linuxquestions.org/questions/linux-newbie-8/combine-multiple-files-into-one-file-446299/)

baddah 05-19-2006 12:15 AM

combine multiple files into one file?
 
Hi is there a easy way to combine all files,say in a directory, into one big file that contains all the information of the files together?i know something like

>>cat file1 file2 file3 .... >> bigfile

might work,but what if the directory has 1000 files in int?i dont want to type every file's name..It's just normal files containing text i want to combine.Thanks

bigrigdriver 05-19-2006 12:23 AM

for i in /home/docs/text/*
do
cat $i >> bigfile
done

or something along those lines.

gilead 05-19-2006 12:28 AM

Or:
Code:

cat * >> bigfile

baddah 05-19-2006 12:38 AM

brilliant,thanks.feel a bit stupid that i could not figure that one out by myself,its early in the morning that's my excuse.

gilead 05-19-2006 12:43 AM

It's not immediately obvious that it'll work until you see that the '*' is expanded by bash to an alphabetical list of the files in the directory. At least that was my feeling when someone pointed it out to me... ;)

baddah 07-12-2006 04:42 AM

Hi,another question on this i have.Please reply if you know the answer.what if i want to store the filename of the small file in the bigfile as well?how can i do this.

For instance say in directory /test there are 2 files test1 and test2.I want to combine them into one big file,but append after each line the filename that the original data was in.Is there an easy enough way to do this??

Thanks

OdieQ 07-12-2006 06:40 AM

Edit: deleted irrelevant post

OdieQ 07-12-2006 06:49 AM

Sorry, I misread your question. I assume now that you want something like in my example below.

That can be achieved by writing:
Code:

(for r in *;do sed s/\$/\ $r/ < "$r";done) > bigfile
file1
Quote:

Albert
August
Arnold
file2
Quote:

Bertha
Brad
Bart
bigfile
Quote:

Albert file1
August file1
Arnold file1
Bertha file2
Brad file2
Bart file2

baddah 07-12-2006 07:34 AM

Perfect!!(the second one..)Thank you very much.I must really force myself to get into bash scripting,but the syntax is so confusing.but anyway,thanks

shyamsandeep 09-06-2011 10:59 AM

could some one please help me with code on how to merge two files as belowm, the delimiter could be any thing such as tab, space , coma anything, below is a tab

file1
Quote:
Albert
August
Arnold

file2
Quote:
Bertha
Brad
Bart

bigfile
Quote:
Albert Bertha
August Brad
Arnold Bart

Thanks
Sandeep

colucix 09-06-2011 11:22 AM

Please don't resurrect old threads with a new question. Furthermore you've posted a new thread here: please see the answers and follow discussion there.


All times are GMT -5. The time now is 06:25 PM.