Linux make file size

Linux / UNIX: Create Large 1GB Binary Image File With dd Command

  1. fallocate command – Preallocate space to a file.
  2. truncate command – Shrink or extend the size of a file to the specified size.
  3. dd command – Convert and copy a file i.e. clone/create/overwrite images.
  4. df command – Show free disk space.
  5. du command – Show disk usage statistics.
  6. ls command – List file size.
Tutorial details
Difficulty level Easy
Root privileges No
Requirements fallocate or dd command
Est. reading time 3 minutes

fallocate command syntax

The basic syntax is:
fallocate -l Image_Size_Here /path/to/image.img
Let us see how to create large 1GB binary file with fallocate.

Creating a large file on a Linux using fallocate command

The following command will create 1G file:

  • No ads and tracking
  • In-depth guides for developers and sysadmins at Opensourceflare✨
  • Join my Patreon to support independent content creators and start reading latest guides:
    • How to set up Redis sentinel cluster on Ubuntu or Debian Linux
    • How To Set Up SSH Keys With YubiKey as two-factor authentication (U2F/FIDO2)
    • How to set up Mariadb Galera cluster on Ubuntu or Debian Linux
    • A podman tutorial for beginners – part I (run Linux containers without Docker and in daemonless mode)
    • How to protect Linux against rogue USB devices using USBGuard

Join Patreon

Verify new disk image with the ls command:
$ ls -lh test.img
Sample outputs:

You can use stat or du command to verify block allocation:

dd command syntax

Note : The following information only exists for older version of Linux and/or when fallocate command is not available. If possible use fallocate command only for creating binary images. dd command based method is considered as old and not recommended, but presented here for historical reasons only.

The basic syntax is:
dd if=/path/to/input of=/path/to/output [options] ## OR ##
dd if=/dev/zero of=/path/to/output.img [options] ## OR ##
dd if=/dev/zero of=YOUR-IMAGE-FILE-NAME-HERE bs=1 count=0 seek=Size-HERE

Creating an image file with dd command

First, make sure you’ve sufficient disk space to create a image file using dd:
$ df -H

To create 1MB file (1024kb), enter:
$ dd if=/dev/zero of=test.img bs=1024 count=0 seek=1024

You will get an empty files (also known as “sparse file”) of arbitrary size using above syntax. To create 10MB file , enter:
$ dd if=/dev/zero of=test.img bs=1024 count=0 seek=$[1024*10]

To create 100MB file , enter:
$ dd if=/dev/zero of=test.img bs=1024 count=0 seek=$[1024*100] $ ls -lh test.img

To create 1GB, file:
$ dd if=/dev/zero of=1g.img bs=1 count=0 seek=1G
Sample output:

Verify file size (note bs factor in original dd command):
$ ls -lh 1g.img
$ stat 1g.img
$ du -h 1g.im

dd tip: Create a file with a given size in Unix or Linux

If truncate command not available, try the following syntax:
dd if=/dev/zero of=/path/to/fiie.img bs=YOUR-FILE-SIZE-HERE count=1
In this example, create a file with 1G (on *BSD/OS X use 1g) size:
$ dd if=/dev/zero of=1g.bin bs=1G count=1
Here is what I see:

Verify file size/blocks:
$ stat 1g.bin
Verification outputs:

Summing up

You learned how to create large 1GB (or any size of your choice) binary image file with dd and other command on Linux or Unix-like systems. See man pages for more info:
man dd
man fallocate

🐧 Get the latest tutorials on Linux, Open Source & DevOps via

Category List of Unix and Linux commands
Documentation help • mandb • man • pinfo
Disk space analyzers df • duf • ncdu • pydf
File Management cat • cp • less • mkdir • more • tree
Firewall Alpine Awall • CentOS 8 • OpenSUSE • RHEL 8 • Ubuntu 16.04 • Ubuntu 18.04 • Ubuntu 20.04
Linux Desktop Apps Skype • Spotify • VLC 3
Modern utilities bat • exa
Network Utilities NetHogs • dig • host • ip • nmap
OpenVPN CentOS 7 • CentOS 8 • Debian 10 • Debian 8/9 • Ubuntu 18.04 • Ubuntu 20.04
Package Manager apk • apt
Processes Management bg • chroot • cron • disown • fg • glances • gtop • jobs • killall • kill • pidof • pstree • pwdx • time • vtop
Searching ag • grep • whereis • which
Shell builtins compgen • echo • printf
Text processing cut • rev
User Information groups • id • lastcomm • last • lid/libuser-lid • logname • members • users • whoami • who • w
WireGuard VPN Alpine • CentOS 8 • Debian 10 • Firewall • Ubuntu 20.04

Comments on this entry are closed.

I don’t think this works correctly unless the “count” argument is changed to something greater than 0.

Try something like this:

dd if=/dev/zero of=10Gtest bs=1M count=10000

I don’t think you have read and tested this yourself.

If using “count=0”, as the article suggests, you will still have the file of the specified size.

However, the output will show something like “0+0 records in” and “0+0 records out”.

Then if you check the file size:

it should show the proper size.

It’s even easier, using the format that the commenter Indie suggest:

JW is correct. Without a count setting – its generating a 0 byte file.

ls -alh will show a file size; but compare that to df -h – and you’ll see it doesn’t actually consume any space on disk

Bullshit the 10g one above doesnt create the proper file size at all. Check with the du command it replys the file size is 0.

I think the commenter “Indie” has already clarified this in a previous comment (in agreement with the author).

@JW, @sonassi, @stfu_and_die
If you think you’re right, why don’t tell the author of the article to either remove or edit the article? or write your own article?

Indie August 26, 2011 at 3:02 pm

The ls command will tell you the size of the file while du will tell you the Disk Usage. When you create the empty disk image with the above command it doesn’t write any data so it doesn’t actually allocate any blocks to the file.

If you type “stat disk.img” you’ll see that it’s using 0 blocks and thus no disk space is being consumed by the file, it’s only when you then write data to it that the blocks will then be allocated.

The FAQ has been updated with new and accurate information. However, I kept dd command based method for historical reasons only. I recommend using fallocate command on Linux.

Hope this helps!

can I supress the output of dd command?

Yes, send it to /dev/null.

dd args >/dev/null
OR
dd args >/dev/null 2>&1

Slightly easier version is to use a suffix for the size instead of trying to do the maths.

dd if=/dev/zero of=disk.img bs=1 count=0 seek=1G

to create a 1G file.

Yes this is work but there is concern .

When do ls -lh disk.img file its showing as 1G size but when u run the command

du -h disk.img it shows 0 bytes of size…..

Which one we can believe…i dont know…please clarify if you know about this…

The ls command will tell you the size of the file while du will tell you the Disk Usage. When you create the empty disk image with the above command it doesn’t write any data so it doesn’t actually allocate any blocks to the file.

If you type “stat disk.img” you’ll see that it’s using 0 blocks and thus no disk space is being consumed by the file, it’s only when you then write data to it that the blocks will then be allocated.

I have 2 tb harddrive and i want to clone only 1 tb to my other drive?

what command should i do

Hi, the question is old – but I’m happy to provide a nice answer – maybe this basic advice will help other people as well.
This isn’t exactly possible with dd. If you’d only dd 1tb of the 2tb drive and you’re lucky to be able to mount it, you’ll end up seeing correct filenames – but files will be corrupted entirely.
I’d rather suggest you:
? format your second hard-drive
? look which files/directories are using how much space, recommended tool is ncdu
? create a list of files you want to copy and use rsync to copy the files

How to check if the created file is empty, is there a way to check that?

Using /dev/zero will create a file for you, but the free space seems to remain the same on the disk. If you’re looking to make a filler file as I am in /var/lib/mysql so when the database fills up, I still have some space to work with.. you’ll want to substitute /dev/zero to /dev/random or something like that.

Here’s my attempt at creating a 1G filler file.
df output before:
/dev/mapper/VG01-LV_mysql
30959612 27519608 3439984 89% /var/lib/mysql
command:
dd if=/dev/zero of=1gig-filler.img bs=1000 count=0 seek=$[1000*1000*1]
0+0 records in
0+0 records out
0 bytes (0 B) copied, 3e-05 seconds, 0.0 kB/s

file:
-rw——- 1 root root 1000000000 Feb 13 12:26 1gig-filler.img

df output after (space is slightly different because the db is being used):
/dev/mapper/VG01-LV_mysql
30959612 27521072 3438520 89% /var/lib/mysql

I don’t see any use for using /dev/zero in this scenario. I’ve used it to wipe a disk clean… dd if=/dev/zero of=/dev/sda will clear a disk, but this file doesn’t take up real space.

Actually it might be the count=0 part that is making it not use any real space.. I can’t seem to get df to show me the space actively being used even with /dev/random.. I know I’ve done this successfully before.

How to delete that test file that is being created?

Источник

6 способов создать файл в Linux определенного размера

Если вы хотите создать файл определенного размера в Linux, чтобы проверить скорость загрузки или скачивания на файловом сервере. Или же

Если вы хотите создать определенный размер файла в Linux, чтобы расширить раздел подкачки, то вы можете использовать один из следующих методов для его создания.

Я уже знаю несколько команд для выполнения этого, но я включил все возможные варианты в этой статье.

Вы можете проверить все команды, которые доступны в этой статье, и выбрать предпочитаемую для вас.

Я собираюсь создать файл 10M.

Это можно сделать с помощью следующих 6 методов.

  • fallocate: fallocate используется для предварительного выделения или освобождения места в файле.
  • truncate: truncate используется для сокращения или расширения размера файла до указанного размера.
  • dd: может копировать файл, конвертировать и форматировать в соответствии с операндами.
  • head: head используется для отображения первой части файлов.
  • Команда xfs_mkfile: xfs_mkfile позволяет нам создавать файлы определенного размера в Linux.
  • perl: Perl – это язык программирования, специально разработанный для редактирования текста.

Как создать файл определенного размера в Linux с помощью команды fallocate?

Fallocate используется для манипулирования выделенным дисковым пространством для файла, чтобы освободить или предварительно выделить его.

Для файловых систем, которые поддерживают системный вызов fallocate, предварительное распределение выполняется быстро, выделяя блоки и помечая их как неинициализированные, не требуя ввода-вывода для блоков данных.

Это гораздо быстрее, чем создавать файл, заполняя его нулями.

Используйте команду ls, чтобы проверить заданный размер файла.

Как создать файл определенного размера в Linux с помощью команды truncate?

truncate используется для уменьшения или расширения размера файла до указанного размера.

Используйте команду ls, чтобы проверить заданный размер файла.

Как создать файл определенного размера в Linux с помощью команды dd?

Команда dd означает дубликатор данных.

Он используется для копирования и преобразования данных (по умолчанию из стандартного ввода в стандартный вывод).

Используйте команду ls, чтобы проверить заданный размер файла.

Как создать файл определенного размера в Linux с помощью команды head?

Команда head считывает первые несколько строк любого текста, переданного ему в качестве ввода, и записывает их в стандартный вывод (который по умолчанию является экраном дисплея).

Используйте команду ls, чтобы проверить заданный размер файла.

Как создать файл определенного размера в Linux с помощью команды xfs_mkfile?

xfs_mkfile создает один или несколько файлов. Файл дополняется нулями по умолчанию.

Размер по умолчанию – в байтах, но его можно пометить как килобайты, блоки, мегабайты или гигабайты с суффиксами k, b, m или g соответственно.

Используйте команду ls, чтобы проверить заданный размер файла.

Как создать файл определенного размера в Linux с помощью команды perl?

Perl означает «Practical Extraction and Reporting Language».

Perl – это язык программирования, специально разработанный для редактирования текста.

В настоящее время он широко используется для различных целей, включая системное администрирование Linux, сетевое программирование, веб-разработку и т. д.

Используйте команду ls, чтобы проверить заданный размер файла.

Источник

Читайте также:  Где вы берете windows
Оцените статью