I have a directory on my system which is used for a specific reason by applications and users, but I don't want its size to be allowed to exceed 2GB, is there a way of setting up some sort of limit which just doesn't allow the file size to exceed that or any other amount I decide to set for it in the future?

When the size limit is exceeded it should undo the last change (though there should be an option to have it so that it just stops the operation and doesn't care if half a file was copied and left there) and then display a warning to the user.

I am running Ubuntu GNOME 16.10 with GNOME 3.22.


Usual filesystem quota on ext4 is per-user/group, not per-directory. ZFS can sort-of set a directory quota, by creating a filesystem of a fixed size off a ZFS volume. A simple trick, though, is to create a 2GB file, create a filesystem on it, and mount it at the desired folder:

$ touch 2gbarea
$ truncate -s 2G 2gbarea
$ mke2fs -t ext4 -F 2gbarea
mke2fs 1.43.3 (04-Sep-2016)
Discarding device blocks: done                            
Creating filesystem with 524288 4k blocks and 131072 inodes
Filesystem UUID: bf1b2ee8-a7df-4a57-9d05-a8b60323e2bf
Superblock backups stored on blocks: 
    32768, 98304, 163840, 229376, 294912

Allocating group tables: done                            
Writing inode tables: done                            
Creating journal (16384 blocks): done
Writing superblocks and filesystem accounting information: done 

$ sudo mount 2gbarea up    
$ df -h up
Filesystem      Size  Used Avail Use% Mounted on
/dev/loop0      2.0G  6.0M  1.8G   1% /home/muru/up

In any case, filesystem quotas (or methods like this) aren't as user friendly as you want. This method is one-way flexible, in that you can increase the size online, but decreasing it would be hard.

The commands:

  • touch: touch 2gbarea creates an empty file named 2gbarea.
  • truncate: truncate is used to resize files (in this case, I resize the currently empty 2gbarea file to 2 GB using -s 2G).
  • mke2fs: mke2fs creates ext2/3/4 filesystems (in this case, ext4).
  • mount mounts the filesystem on the given directory.
  • df is used to list filesystem usage.
  • 1
    A new person might get confused with the commands. Should their purpose be explained/linked to? Oct 24 '16 at 16:18
  • @RohittVashishtha better?
    – muru
    Oct 24 '16 at 16:23
  • Would there be any way of getting it to launch a little popup warning message when the quota is reached?
    – user364819
    Oct 24 '16 at 17:25
  • @ParanoidPanda "it" being? I think the file browser does complain when there's no space left.
    – muru
    Oct 25 '16 at 6:39
  • @muru: Mainly I don't think it will be the file browser that will be writing to this location, more likely to be other specific applications. Is there no way to "watch" that folder so that when it has reached its capacity a graphical warning pops up to alert the user?
    – user364819
    Oct 25 '16 at 8:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy