Advertisement
tbrowder

create-large-file.pl

Feb 3rd, 2016
150
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.11 KB | None | 0 0
  1. #!/usr/bin/env perl
  2.  
  3. # notes:
  4. # a run on juvat2 (2016-01-31) with 10 Gb was ~2 minutes
  5. # a run on bigtom (2016-01-31) with 10 Gb was 1m37s
  6.  
  7. use v5.14; # 'say'
  8.  
  9. use autodie;
  10. use File::Basename;
  11.  
  12. # 100 char string (counting the ending newline)
  13. my $str = "012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678\n";
  14. my $gb = 1_000_000_000;
  15.  
  16. my $prog = basename($0);
  17. if (!@ARGV) {
  18. say "Usage: $prog <file size in Gb (an integer > -1)>\n";
  19. exit;
  20. }
  21.  
  22. # desired size in gigabytes
  23. my $ngb = shift @ARGV;
  24.  
  25. die "FATAL: '$ngb' is not an integer.\n"
  26. if $ngb =~ /[^\d]+/;
  27.  
  28. my $ofil = "large-${ngb}-gb-file.txt";
  29.  
  30. # how many lines (iterations) needed?
  31. my $slen = length $str;
  32. my $nlines = $ngb * $gb / $slen;
  33.  
  34. # if we create a small file for testing
  35. my $snlines = 100_000;
  36. my $ssize = $snlines * $slen / 1_000_000; # Mb
  37.  
  38. my $ostr = $ngb == 0 ? "0 ($ssize Mb)" : $ngb;
  39. $nlines = $snlines if $ngb == 0;
  40.  
  41. say "Requested file size (Gb): $ostr";
  42. say "String size: $slen";
  43. say "Number lines: $nlines";
  44.  
  45. open my $fp, '>', $ofil;
  46.  
  47. for (1 .. $nlines) {
  48. print $fp $str;
  49. }
  50.  
  51. say "see output file '$ofil'";
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement