开发者

Join all files in a directory, with a separator

开发者 https://www.devze.com 2023-04-08 02:44 出处:网络
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 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.


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.


*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


I reused Ondra's answer, but with absolute path instead.

Command :

echo $( \find '/home/user/[path-to-webapp]/WEB-INF/lib' -name '*.jar' -print0) |  \
sed 's#\.jar/#.jar:#g'

Note: I use # as sed's separator to not match the last jar in the list.

Results:

/home/user/[path-to-webapp]/WEB-INF/lib/jar1.jar:home/user/[path-to-webapp]/WEB-INF/lib/jar2.jar[... and so on...]:/home/user/[path-to-webapp]/WEB-INF/lib/last-jar.jar

Then, I can use this output in a javac -classpath command.

0

精彩评论

暂无评论...
验证码 换一张
取 消