Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

bash: fill file from another file

Tags:

linux

bash

I have a file named SOURCE, and I want to create a file named TARGET of a specific length, containing copies of SOURCE. TARGET's length is not necessarily an integer multiple of SOURCE's length. I want to do this using bash on Linux.

My first try was this:

while true; do cat SOURCE; done | head -c $TARGET_LENGTH > TARGET

That hangs after writing the specified number of bytes to TARGET. How do I make it not hang? (I suspect I'm missing a detail around how pipes and signals work.)

Edit: I know that while true runs forever, but I expected the head command to shut everything down after consuming the specified number of bytes.

like image 758
user100464 Avatar asked Mar 17 '26 04:03

user100464


1 Answers

How about this?

while cat SOURCE; do true; done | head -c "$TARGET_LENGTH" > TARGET
like image 106
gniourf_gniourf Avatar answered Mar 19 '26 00:03

gniourf_gniourf



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!