Reputation: 177
This is NOT a homework question, this is a question from an old exam, so anyone giving an answer will not be contributing to academic dishonesty. For those still skeptical, I am simply seeking what command I could use for this.
You have a file called one_mb which is exactly 1 megabyte in size. You want to create from it a file of exactly 128 megabytes in size. Please write a shell script to do this with at most 9 lines and no loops, if statements, recursion, or any other logic control structures. Each command, including parameters, must be less than 100 characters in length.
I began to research xarg, but could not figure out a good way to use it to accomplish this.
Upvotes: 10
Views: 6156
Reputation: 15560
I think it would be enough with the constraints you have. You can define a function that takes an integer as parameter. If it's greater than 0, cat
s the file and calls the same function again, but with the parameter decreased.
Then you just call the function with the value needed, and you're done.
Good ol' recursion :)
(Sorry, too lazy for coding, and there are lots of other working answers, just wanted to avoid recursion being forgotten :))
Upvotes: -1
Reputation: 11469
dd oflag=append conv=notrunc if=/dev/zero of=one_mb bs=1MB count=127
This will retain the file content and add a bunch of "zero" records to make it 128 MB. Do
ls -ltrh one_mb
to check if it actually is 128MB, otherwise you might have to change the "count=127" parameter.
Upvotes: 2
Reputation: 363
Not sure if this counts, but this came to mind:
seq 1 128 | xargs -Inone cat one_mb >> 128_mb
No loops were used, just a pipe and xargs
.
Upvotes: 12
Reputation: 801
Assuming bash, you can use a one-line brace expansion hack:
cat one_mb{,}{,}{,}{,}{,}{,}{,} > 128_mb
Upvotes: 20
Reputation: 881303
At 100 characters per command, you could reduce it quite a bit:
cat one_mb one_mb one_mb one_mb one_mb one_mb one_mb one_mb >mb8
cat mb8 mb8 mb8 mb8 >mb32
cat mb32 mb32 mb32 mb32 >mb128
rm -f mb8 mb32
Upvotes: 4
Reputation: 54551
The big hint here is that it can be "no more than 9 lines". Since 2^7 = 128, you just need to double the file's size 7 times:
cat one_mb one_mb > two_mb
cat two_mb two_mb > four_mb
...
cat 64_mb 64_mb > 128_mb
Upvotes: 4