Linux – How to create a file with a specific size from a command line

command linelinux

How can I create a file (e.g. using the touch command or another similar way), but with a specific size (e.g. 14 MB)?

The use case is to test FTP with various file sizes. If we can create a file with a specific size, it will be useful to verify FTP transfer etc.

Best Answer

Use truncate:

truncate -s 14M filename

From man truncate:

   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.