대용량 더미 파일 생성 (fallocate)

Joonas' Note

대용량 더미 파일 생성 (fallocate) 본문

개발

대용량 더미 파일 생성 (fallocate)

2018. 11. 3. 17:02 joonas 읽는데 1분

    파일 전송이나 수정(분할, 삭제...), 삭제 등의 파일을 다루는 개발에서 적당한 크기의 더미 파일이 종종 필요하다.

    그래서 매번 파이썬으로 제너레이터를 만들어서 사용했다. 가장 최근에 작성한 파일 생성 코드는 아래와 같다.

    # python 3.6.2
    # usage: [filesize] [B|KB|MB|GB]
    units = ['B', 'KB', 'MB', 'GB']
    print("[usage]")
    print(">> [filesize] [{}]".format('|'.join(units)))
    while True:
    try:
    print(">> ", end='')
    size, unit = input().split()
    size = int(size)
    unit = unit.upper()
    if unit not in units:
    print('wrong unit')
    continue
    actual_size = 2 ** (10 * (units.index(unit)))
    filename = "{}{}.dat".format(size, unit)
    with open(filename, 'w') as f:
    f.write('+' * (size * actual_size))
    except Exception as ex:
    print(ex)
    view raw gen-dummy.py hosted with ❤ by GitHub

    근데 리눅스 계열에서는 이미 명령어로 있었다. fallocate 라는 명령어인데, 사용 예시는 아래와 같다.

    $ fallocate -l 100m /path/filename
    $ fallocate -l 128Kib ./dummy-small
    $ fallocate -l 1gb ./dummy-large

    파일의 크기 단위는 GB, GiB, k 등 대소문자 구분이 없고, "iB" 는 생략해도 된다. 즉, k라고 적으면 킬로바이트(KiB)이다.

    속도는 엄청 빠르다. 10GB 크기의 더미 파일 하나를 만드는 데에 0.01초가 안 걸렸다.


    Comments