Attach all files in the directory, with a delimiter

I have a directory containing hundreds of files (each of which has several characters). I want to merge them into a single file with a delimiter, "|".

I tried

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

But this created an endless cycle.

+6
source share
2 answers

You can exclude output.txt from find output with -not -name output.txt (or, as you mentioned in the comments below, just put the output file outside the target directory).

For instance:

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

I also took the liberty of replacing your while/cat/echo with a couple of -exec options so that we can do all this with a single find call.

+9
source

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

This is what I use to combine .jar files to run a Java application 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 list of files can be replaced by find ... or something else.
echo used to replace newlines with spaces.

Final form:

 java -cp $(echo `ls -1 *.jar` | sed 's/\W/:/g') com.foo.Bar 
0
source

Source: https://habr.com/ru/post/898209/


All Articles