The Computer Oracle

How can I create a file with a specific size from a command line?

--------------------------------------------------
Hire the world's top talent on demand or became one of them at Toptal: https://topt.al/25cXVn
and get $2,000 discount on your first invoice
--------------------------------------------------

Take control of your privacy with Proton's trusted, Swiss-based, secure services.
Choose what you need and safeguard your digital life:
Mail: https://go.getproton.me/SH1CU
VPN: https://go.getproton.me/SH1DI
Password Manager: https://go.getproton.me/SH1DJ
Drive: https://go.getproton.me/SH1CT


Music by Eric Matyas
https://www.soundimage.org
Track title: Puzzle Game 3

--

Chapters
00:00 How Can I Create A File With A Specific Size From A Command Line?
00:24 Answer 1 Score 13
00:35 Answer 2 Score 21
01:08 Answer 3 Score 103
02:36 Accepted Answer Score 172
03:03 Thank you

--

Full question
https://superuser.com/questions/609020/h...

--

Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...

--

Tags
#linux #commandline

#avk47



ACCEPTED ANSWER

Score 172


Use truncate:

truncate -s 14M filename

From man truncate:

DESCRIPTION
   Shrink or extend the size of each FILE to the specified size

[...]

 -s, --size=SIZE
          set or adjust the file size by SIZE

Note: truncate may not be available on your system, e.g. on Mac OS X it's not installed by default (but you can easily install it, using macports for example). In these cases you may need to use dd or head instead.




ANSWER 2

Score 103


EDIT: The simplest way is probably the truncate of Ashutosh Vishwa Bandhu's answer, but as pointed out by @offby1 that creates sparse files which may not be what you want. The solutions below create normal, full files.


The following commands create a 14MB file called foo:

  1. fallocate (thanks to @Breakthrough who suggested it in the comments and vote up Ahmed Masud's answer below which also mentions it.)

    fallocate -l 14000000 foo
    

    This command is particularly impressive since it is as fast as truncate (instantaneous) irrespective of the desired file size (unlike the other solutions which will be slow for large files) and yet creates normal files, not sparse ones :

    $ truncate -s 14MB foo1.txt 
    $ fallocate -l 14000000 foo2.txt 
    $ ls -ls foo?.txt 
        0 -rw-r--r-- 1 terdon terdon 14000000 Jun 21 03:54 foo1.txt
    13672 -rw-r--r-- 1 terdon terdon 14000000 Jun 21 03:55 foo2.txt
    
  2. Create a file filled with random data

    dd if=/dev/urandom of=foo bs=14MB count=1
    

                                                                              or

    head -c 14MB /dev/urandom > foo
    
  3. Create a file filled with \0s:

    dd if=/dev/zero of=foo.txt bs=14MB count=1
    

                                                                              or

    head -c 14MB /dev/zero > foo
    
  4. Create a file filled with the first 14MB of data of another file:

    head -c 14MB bar.txt > foo
    
  5. Create a file filled with the last 14MB of data of another file:

    tail -c 14MB bar.txt > foo
    

In all of the above examples, the file will be 14*1000*1000 if you want 14*1024*1024, replace MB with M. For example:

dd if=/dev/urandom of=foo bs=14M count=1
head -c 14M /dev/zero > foo

fallocate only deals in bytes, so you'd have to do (14*1024*1024=14680064)

fallocate -l 14680064 foo



ANSWER 3

Score 21


dd bs=1MB count=14 if=/dev/zero of=<yourfilename>

WARNING dd will silently overwrite anything you have the rights to write to, if you specify it either willingly or by accident. Make sure you understand dd, eg. with reading dd --help, and you don't screw up your dd command lines or else you can easily create irreversible data loss - even total.

For start, if= is the input file and of= is the output file. Always double check that you got it right, before running the command. :)




ANSWER 4

Score 13


Use magic dd ;) man page

$ dd if=/dev/zero of=output.dat  bs=1024  count=14336

cause 14MB*1024 = 14336KB, or:

$ dd if=/dev/zero of=output.dat  bs=1MB  count=14

And you'll get 14mb output.dat file, filled with zeroes :D