Unix & Linux Stack Exchange
Q&A for users of Linux, FreeBSD and other Unix-like operating systems
Latest Questions
6
votes
1
answers
703
views
How to find files by size that are not divisible by 4096 and round them up
In Linux in a Folder there are many files, all created with `fallocate` with random size. How to find files whose size is not divisible by 4096 and correct the filesize (rounded up) to a multiple of 4096? They can be found with : find . -type f -printf '%s\t%p\n' | awk -F'\t' '{size=$1; file=$2; if...
In Linux in a Folder there are many files, all created with
fallocate
with random size.
How to find files whose size is not divisible by 4096 and correct the filesize (rounded up) to a multiple of 4096?
They can be found with :
find . -type f -printf '%s\t%p\n' | awk -F'\t' '{size=$1; file=$2; if (size % 4096 != 0) print file}'
But how to size them up that the filesize is divisible by 4096 ?
Banana
(189 rep)
Jun 16, 2025, 01:34 AM
• Last activity: Jun 16, 2025, 11:29 PM
7
votes
3
answers
2488
views
Can losetup be made efficient with sparse files?
So my setup is like this. $ truncate -s 1T volume $ losetup -f --show volume /dev/loop0 $ mkfs.ext4 /dev/loop0 $ ls -sh volume 1.1G volume $ mount /dev/loop0 /mnt/loop Now I have a 1.1TB volume, as expected. The overhead of ext4 expanded the sparse file to 1.1G, but that's fine. Now to add a file. $...
So my setup is like this.
$ truncate -s 1T volume
$ losetup -f --show volume
/dev/loop0
$ mkfs.ext4 /dev/loop0
$ ls -sh volume
1.1G volume
$ mount /dev/loop0 /mnt/loop
Now I have a 1.1TB volume, as expected. The overhead of ext4 expanded the sparse file to 1.1G, but that's fine. Now to add a file.
$ dd if=/dev/urandom of=/mnt/loop/file bs=1M count=10240
$ ls -sh volume
12G volume
Cool, now I don't want the file.
$ rm /mnt/loop/file
$ ls -sh volume
12G volume
The free space is still taking up space, as expected, and
$ fallocate -d volume
frees up 1gb.
My question is, how can I zero out the free space here without expanding the volume to the full size? $ dd if=/dev/zero
will expand it to full size, and with conv=sparse
makes it create a useless sparse file inside the volume.
TL;DR: Is there a way to make losetup
ignore writes of null blocks to null sectors, while allowing everything else?
Daffy
(465 rep)
Aug 23, 2018, 12:43 AM
• Last activity: Apr 11, 2025, 11:46 PM
0
votes
0
answers
175
views
create a file with fallocate, values for it are store in files
I am using a Linux system, and I want to create a file using `fallocate`. Not a full sized file, I just want to allocate the size I choose on the file system. I have the size value in `temp/size` and the name value in `temp/name`, and will store the file in `temp/decrypt/`. I tried this: fallocate -...
I am using a Linux system, and I want to create a file using
fallocate
. Not a full sized file, I just want to allocate the size I choose on the file system. I have the size value in temp/size
and the name value in temp/name
, and will store the file in temp/decrypt/
.
I tried this:
fallocate -l $(cat temp/size) temp/decrypt/"$(cat temp/name)"
but it fails with:
fallocate failed: Operation not supported
I don't know what is wrong.
$ cat temp/size
1073741824
and
$ cat temp/name
testname123.data
This works, but where is the error?
user447274
(539 rep)
Sep 19, 2024, 07:44 AM
• Last activity: Sep 19, 2024, 09:27 AM
2
votes
2
answers
2517
views
What kind of content/data does "fallocate" command generate?
When is executed the `sudo fallocate -l 2G /swapfile` command, then 2 GB is created, but with that kind of content or data? Can be it customized? If yes How?. I did do some research but there are no details about this. I want to know if is good use `fallocate` for `swap file` purpose. For example `s...
When is executed the
sudo fallocate -l 2G /swapfile
command, then 2 GB is created, but with that kind of content or data? Can be it customized? If yes How?. I did do some research but there are no details about this. I want to know if is good use fallocate
for swap file
purpose.
For example sudo dd if=/dev/zero of=/swapfile bs=1024 count=N
creates the data filled with Zeros, not sure if swap file
asks for mandatorily the data with zeros - in the swap file
tutorials did not mention of this.
Manuel Jordan
(2108 rep)
Mar 31, 2022, 12:18 AM
• Last activity: Mar 31, 2022, 03:05 AM
0
votes
0
answers
810
views
Can't resize mounted file to dir after increasing size (fallocate)
I created a file to mount as /tmp dir to limit it's size so it cant crash the system. I used mkfs.ext4 on it and it mounts in /etc/fstab. Now I need to increase the size, so I used fallocate again, and the /tmpfile shows larger now, but when mounted is the same size. I tried a bunch of things, like...
I created a file to mount as /tmp dir to limit it's size so it cant crash the system. I used mkfs.ext4 on it and it mounts in /etc/fstab. Now I need to increase the size, so I used fallocate again, and the /tmpfile shows larger now, but when mounted is the same size. I tried a bunch of things, like mounting manually and using resize2fs on the unmounted file and the mounted dir. The former has no errors, but still isnt larger, even after running the suggested
e2fsck -f /tmpfile
on it.
Any ideas?
I'm not using a loop device as with this answer, nor 2 partitions formated with parted. https://unix.stackexchange.com/questions/595641/how-to-increase-the-size-of-a-loop-virtual-disk
EDIT: As pointed out, mounting a file to a directory uses a loop device by default, even without the mount -o loop
option. Using fallocate -l 200M /tmpfile
and then resize2fs /dev/loop#
worked with the file mounting edited out of the /etc/fstab, rebooting, and then mounting it over the bootup /tmp to check the size, by undoing the fstab edit and using mount -a
. Sidenote, I could have mounted it to /mnt to check, but unix/linux deals with overmounting gracefully: https://unix.stackexchange.com/questions/198542/what-happens-when-you-mount-over-an-existing-folder-with-contents , though it seems there could be more conflicts when overmounting the home directory, but perhaps not.
alchemy
(757 rep)
Jan 13, 2022, 03:11 AM
• Last activity: Jan 13, 2022, 08:39 PM
13
votes
1
answers
3718
views
What are the holes in files created with fallocate?
If we create a swapfile with fallocate we are told that the file contains holes and therefore the operation is halted. **What are these holes?** - Is it unallocated disk space, which makes the whole file allocation a hole itself? Like this: - block1 -> empty - block2 -> empty - block3 -> empty or -...
If we create a swapfile with fallocate we are told that the file contains holes and therefore the operation is halted.
**What are these holes?**
- Is it unallocated disk space, which makes the whole file allocation a hole itself?
Like this:
- block1 -> empty
- block2 -> empty
- block3 -> empty
or
- is it the blocks preallocated contains holes in between, and
therefore they are not contiguous? Like this:
- block1
- block2
- hole in block3 [occupied by other file in the disk]
- block4
**Test**
fallocate -l 100MB /swap
chmod 600 /swap
mkswap /swap
swapon /swap
**Output**
swapon: swapfile has holes
swapon: /swap: swapon failed: Invalid argument
Nelssen
(305 rep)
May 12, 2019, 03:40 PM
• Last activity: May 12, 2019, 06:17 PM
2
votes
1
answers
981
views
Is it possible to zero the free space on a LUKS volume?
I want to have a LUKS volume as a loopback device pointing at a sparse file. Lets say I put a 20gb file on it, then later I delete it. How can I zero that area on the device itself so I can `fallocate -d` the loopback file to reclaim the space?
I want to have a LUKS volume as a loopback device pointing at a sparse file. Lets say I put a 20gb file on it, then later I delete it. How can I zero that area on the device itself so I can
fallocate -d
the loopback file to reclaim the space?
Daffy
(465 rep)
Aug 22, 2018, 11:53 PM
• Last activity: Aug 25, 2018, 12:59 AM
0
votes
1
answers
516
views
Why dd speed decrease significantly after fallocate once
Why `fallocate -c -l1GiB` **once** cause the second loop of dd speed decrease from 1.0 GB to ~200 MB/s ? xb@dnxb:~/Downloads/test$ fallocate -l 10.2GiB lala.mp4 xb@dnxb:~/Downloads/test$ ls -larthiF --context --color total 11G 41680908 drwxr-xr-x 121 xiaobai xiaobai ? 100K Dis 3 01:36 ../ 53086804 d...
Why
fallocate -c -l1GiB
**once** cause the second loop of dd speed decrease from 1.0 GB to ~200 MB/s ?
xb@dnxb:~/Downloads/test$ fallocate -l 10.2GiB lala.mp4
xb@dnxb:~/Downloads/test$ ls -larthiF --context --color
total 11G
41680908 drwxr-xr-x 121 xiaobai xiaobai ? 100K Dis 3 01:36 ../
53086804 drwxrwxr-x 2 xiaobai xiaobai ? 4.0K Dis 3 01:36 ./
53086805 -rw-rw-r-- 1 xiaobai xiaobai ? 11G Dis 3 01:37 lala.mp4
xb@dnxb:~/Downloads/test$ f='lala.mp4'; n=0; while (( "$(stat --printf="%s" $f)" > 1073741824 )); do ((n++)); echo "[dd...$n]"; dd bs=1G skip=0 count=1 if=$f of="$f.$n"; fallocate -c -l1GiB $f; done; ((n++)); mv $f $f.$n;
[dd...1]
1+0 records in
1+0 records out
1073741824 bytes (1.1 GB, 1.0 GiB) copied, 1.05744 s, 1.0 GB/s
[dd...2]
1+0 records in
1+0 records out
1073741824 bytes (1.1 GB, 1.0 GiB) copied, 3.24091 s, 331 MB/s
[dd...3]
1+0 records in
1+0 records out
1073741824 bytes (1.1 GB, 1.0 GiB) copied, 4.56858 s, 235 MB/s
[dd...4]
1+0 records in
1+0 records out
1073741824 bytes (1.1 GB, 1.0 GiB) copied, 4.02249 s, 267 MB/s
[dd...5]
1+0 records in
1+0 records out
1073741824 bytes (1.1 GB, 1.0 GiB) copied, 4.34307 s, 247 MB/s
[dd...6]
1+0 records in
1+0 records out
1073741824 bytes (1.1 GB, 1.0 GiB) copied, 4.27371 s, 251 MB/s
[dd...7]
1+0 records in
1+0 records out
1073741824 bytes (1.1 GB, 1.0 GiB) copied, 4.80721 s, 223 MB/s
[dd...8]
1+0 records in
1+0 records out
1073741824 bytes (1.1 GB, 1.0 GiB) copied, 4.17114 s, 257 MB/s
[dd...9]
1+0 records in
1+0 records out
1073741824 bytes (1.1 GB, 1.0 GiB) copied, 4.60627 s, 233 MB/s
[dd...10]
1+0 records in
1+0 records out
1073741824 bytes (1.1 GB, 1.0 GiB) copied, 4.13853 s, 259 MB/s
xb@dnxb:~/Downloads/test$
xb@dnxb:~/Downloads/test$ ls -larthiF --context --color
total 11G
41680908 drwxr-xr-x 121 xiaobai xiaobai ? 100K Dis 3 01:36 ../
53086806 -rw-rw-r-- 1 xiaobai xiaobai ? 1.0G Dis 3 01:37 lala.mp4.1
53086807 -rw-rw-r-- 1 xiaobai xiaobai ? 1.0G Dis 3 01:37 lala.mp4.2
53087049 -rw-rw-r-- 1 xiaobai xiaobai ? 1.0G Dis 3 01:37 lala.mp4.3
53087910 -rw-rw-r-- 1 xiaobai xiaobai ? 1.0G Dis 3 01:37 lala.mp4.4
53087911 -rw-rw-r-- 1 xiaobai xiaobai ? 1.0G Dis 3 01:37 lala.mp4.5
53087913 -rw-rw-r-- 1 xiaobai xiaobai ? 1.0G Dis 3 01:38 lala.mp4.6
53087914 -rw-rw-r-- 1 xiaobai xiaobai ? 1.0G Dis 3 01:38 lala.mp4.7
53087962 -rw-rw-r-- 1 xiaobai xiaobai ? 1.0G Dis 3 01:38 lala.mp4.8
53087963 -rw-rw-r-- 1 xiaobai xiaobai ? 1.0G Dis 3 01:38 lala.mp4.9
53087964 -rw-rw-r-- 1 xiaobai xiaobai ? 1.0G Dis 3 01:39 lala.mp4.10
53086805 -rw-rw-r-- 1 xiaobai xiaobai ? 200M Dis 3 01:39 lala.mp4.11
53086804 drwxrwxr-x 2 xiaobai xiaobai ? 4.0K Dis 3 01:39 ./
xb@dnxb:~/Downloads/test$
**[Update]**
I noticed added sync
before next dd
will make it consistent again (sleep 120
also work, but not sleep 20
), but the total time will slower since sync
take more time than **dd
without sync
**:
xb@dnxb:~/Downloads/test$ f='lala.mp4'; n=0; while (( "$(stat --printf="%s" $f)" > 1073741824 )); do ((n++)); echo "[dd...$n]"; dd bs=1G skip=0 count=1 if=$f of="$f.$n"; fallocate -c -l1GiB $f; sync; done; ((n++)); mv $f $f.$n;
[dd...1]
1+0 records in
1+0 records out
1073741824 bytes (1.1 GB, 1.0 GiB) copied, 0.904357 s, 1.2 GB/s
[dd...2]
1+0 records in
1+0 records out
1073741824 bytes (1.1 GB, 1.0 GiB) copied, 0.902471 s, 1.2 GB/s
[dd...3]
1+0 records in
1+0 records out
1073741824 bytes (1.1 GB, 1.0 GiB) copied, 0.896056 s, 1.2 GB/s
...
林果皞
(5546 rep)
Dec 2, 2017, 05:43 PM
• Last activity: Dec 2, 2017, 11:08 PM
13
votes
2
answers
4395
views
What is the difference between `fallocate --dig-holes` and `fallocate --punch-hole` in Linux?
I totally understand that `--dig-holes` creates a sparse file in-place. That is, if the file has holes `--dig-holes` options removes those holes: Let's take it in a very simplified way, let's say we have a huge file named non-sparse: *non-sparse:* aaaaaaaaaaaaaaaaaaaaaaaaaaaa \x00\x00\x00\x00\x00\x0...
I totally understand that
--dig-holes
creates a sparse file in-place. That is, if the file has holes --dig-holes
options removes those holes:
Let's take it in a very simplified way, let's say we have a huge file named non-sparse:
*non-sparse:*
aaaaaaaaaaaaaaaaaaaaaaaaaaaa
\x00\x00\x00\x00\x00\x00\x00
\x00\x00\x00\x00\x00\x00\x00
\x00\x00\x00\x00\x00\x00\x00
bbbbbbbbbbbbbbbbbbbbbbbbbbbb
\x00\x00\x00\x00\x00\x00\x00
\x00\x00\x00\x00\x00\x00\x00
cccccccccccccccccccccccccccc
*non-sparse* has many zeros in it, assume that the interleaving zeros are in Gigabytes. fallocate --dig-holes
de-allocates the space available for the zeros (holes) where the actual file size remains the same (preserved).
Now, there's --punch-hole
what does it really do? I read the man page, still don't understand:
-p, --punch-hole
Deallocates space (i.e., creates a hole) in the byte range
starting at offset and continuing for length bytes. Within
the specified range, partial filesystem blocks are zeroed,
and whole filesystem blocks are removed from the file.
After a successful call, subsequent reads from this range
will return zeroes.
Creating hole, that's the opposite of --dig-hole
option it seems like that, and how come that digging a hole isn't the same as creating a hole?! Help! we need a logician :).
The naming of the two options are synonymous linguistically which perhaps makes confusion.
What's the difference between --dig-holes
and --punch-holes
operationally (not logically or linguistically please!)?
direprobs
(1064 rep)
Sep 4, 2017, 12:49 PM
• Last activity: Sep 28, 2017, 11:18 PM
Showing page 1 of 9 total questions