-3

In Linux with Bash, there is the File numbers_in_one_line.

In this file there is only one line with several numbers, all separated by spaces.

These numbers are the value in bytes for creating files with the program fallocate.

The numbers are random and can change, in this example there are 4 numbers: 24997117901 4848337945 541113465 446445181

Sometimes, the numbers can be more than 4.

If a number over 1GB, so i want to create with fallocate 1GB Parts.

If a number under 1GB, so i want to create with fallocate 100MB Parts.

For Example by these 4 Numbers : 24997117901 4848337945 541113465 446445181

I want to create files like these :

first number (24997117901) every file in 1GB parts.

file1.01

file1.02

file1.03

file1....


second number (4848337945) every file in 1GB parts.
file2.1

file2.2

file2.3

file2....


third number (541113465) every file in 100MB parts.

file3.1

file3.2

file3.3

file3....


fourth number (446445181) every file in 100MB parts.

file4.1

file4.2

file4.3

file4....

How to sperate if MB or GB -range and how many parts and how to give this to fallocate for creating the files by this naming scheme?

I want not create the files with full size first and split them than with split becouse that is to much read/write access by split.

:edit

by exakt 1GB, its for me not important to let it be in 1 part, or to split, what would you recommend?

10
  • 1
    I don't understand half of what you're trying to achieve...gigabyte/megabyte range restsize (the restsize is the entire number)? Commented Apr 25 at 23:41
  • 5
    What have you tried so far and where did it failed? Commented Apr 25 at 23:43
  • tink, i have edit my post. jetchisel i dont know how to start this. Commented Apr 25 at 23:50
  • I think they mean something like this - with the echos taken out, but the question is still quite unclear. Commented Apr 26 at 2:15
  • From the mention of "split", it sounds like 24997117901 should create files file1.01 to file1.23 (each 1GB), and file1.24 of 301055949 bytes. Commented Apr 26 at 10:13

1 Answer 1

4

It sounds like you might want to do something like this:

$ cat ./tst.sh
#!/usr/bin/env bash

while read -r idx sfx size; do
    echo fallocate "file${idx}.${sfx}" "$size"
done < <(
    awk '{
        for ( idx=1; idx<=NF; idx++ ) {
            remainSz = $idx
            chunkSz = 1000000000
            if ( remainSz < chunkSz ) {
                chunkSz = 100000000
            }
            sfx = 1
            while ( remainSz > chunkSz ) {
                print idx, sfx++, chunkSz
                remainSz -= chunkSz
            }
            print idx, sfx, remainSz
        }
    }' "${@:--}"
)

$ ./tst.sh numbers_in_one_line
fallocate file1.1 1000000000
fallocate file1.2 1000000000
fallocate file1.3 1000000000
fallocate file1.4 1000000000
fallocate file1.5 1000000000
fallocate file1.6 1000000000
fallocate file1.7 1000000000
fallocate file1.8 1000000000
fallocate file1.9 1000000000
fallocate file1.10 1000000000
fallocate file1.11 1000000000
fallocate file1.12 1000000000
fallocate file1.13 1000000000
fallocate file1.14 1000000000
fallocate file1.15 1000000000
fallocate file1.16 1000000000
fallocate file1.17 1000000000
fallocate file1.18 1000000000
fallocate file1.19 1000000000
fallocate file1.20 1000000000
fallocate file1.21 1000000000
fallocate file1.22 1000000000
fallocate file1.23 1000000000
fallocate file1.24 1000000000
fallocate file1.25 997117901
fallocate file2.1 1000000000
fallocate file2.2 1000000000
fallocate file2.3 1000000000
fallocate file2.4 1000000000
fallocate file2.5 848337945
fallocate file3.1 100000000
fallocate file3.2 100000000
fallocate file3.3 100000000
fallocate file3.4 100000000
fallocate file3.5 100000000
fallocate file3.6 41113465
fallocate file4.1 100000000
fallocate file4.2 100000000
fallocate file4.3 100000000
fallocate file4.4 100000000
fallocate file4.5 46445181

Remove the echo and update fallocate... to be however you really call fallocate.

3
  • that is what i am looking for! thank you! dont know and understand what for details i have forgot to tell? .... is there a way to incorporate some randomness, see edit 2? Commented Apr 27 at 5:53
  • Please don't add requirements to your question after you got answers as that makes it a Chameleon Question which is strongly discouraged on this site. If you have additional requirements then just ask a new, followup question. Commented Apr 27 at 12:20
  • ok, i forgot this, thanks for the info. Commented Apr 27 at 20:14

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.