How to set a file size limit for a directory?

20,390

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.
Share:
20,390

Related videos on Youtube

Admin
Author by

Admin

Updated on September 18, 2022

Comments

  • Admin
    Admin over 1 year

    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.

  • Rohitt Vashishtha
    Rohitt Vashishtha over 7 years
    A new person might get confused with the commands. Should their purpose be explained/linked to?
  • muru
    muru over 7 years
    @RohittVashishtha better?
  • Admin
    Admin over 7 years
    Would there be any way of getting it to launch a little popup warning message when the quota is reached?
  • muru
    muru over 7 years
    @ParanoidPanda "it" being? I think the file browser does complain when there's no space left.
  • Admin
    Admin over 7 years
    @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?
  • muru
    muru over 7 years
    @ParanoidPanda What will be those other specific applications? Sure, you can write a cron job or something, or install nagios and configure email notifications
  • Admin
    Admin over 7 years
    @muru: Well, for instance apt-get source <package> may run on request into a certain sub-directory of that directory. When it is used for certain purposes anyway. But it will be an automated system running it.
  • muru
    muru over 7 years
    @ParanoidPanda if it's automated, you should be using monitoring systems like nagios.
  • ken_oy
    ken_oy about 5 years
    "mkdir up", in case you don't have an "up" folder in that directory, and get a "mount point up does not exist" error message