Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Join all files in a directory, with a separator

Tags:

bash

unix

I have a directory containing hundreds of files (each having several chars). I want to join them into a single file with a separator, "|".

I tried

find . -type f | (while read line; do; cat $line; echo "|"; done;) > output.txt 

But that created an infinite loop.

like image 701
Dogbert Avatar asked Sep 28 '11 14:09

Dogbert


2 Answers

You can exclude output.txt from the output of find using -not -name output.txt (or as you already pointed out in the comments below, simply place the output file outside the target directory).

For example:

find . -type f -not -name output.txt -exec cat {} \; -exec echo "|" \; > output.txt

I've also taken the liberty to replace your while/cat/echo with a couple of -exec params so we can do the whole thing using a single find call.

like image 199
Shawn Chin Avatar answered Oct 23 '22 18:10

Shawn Chin


*To answer the title of the question, since it's the first in google results (the output.txt problem is actually unrelated):

This is what I use to join .jar files to run Java app with files in lib/:

EntityManagerStoreImpl

ondra@lenovo:~/work/TOOLS/JawaBot/core$ ls
catalog.xml  nbactions.xml  nb-configuration.xml  pom.xml  prepare.sh  resources  run.sh  sql  src  target  workdir

ondra@lenovo:~/work/TOOLS/JawaBot/core$ echo `ls -1` | sed 's/\W/:/g'
catalog:xml:nbactions:xml:nb:configuration:xml:pom:xml:prepare:sh:resources:run:sh:sql:src:target:workdir

The file listing may be of course replaced with find ... or anything.
The echo is there to replace newlines with spaces.

Final form:

java -cp $(echo `ls -1 *.jar` | sed 's/\W/:/g') com.foo.Bar
like image 34
Ondra Žižka Avatar answered Oct 23 '22 19:10

Ondra Žižka