How to Set Limit on Directory Size in Linux

How to set limit on directory size in Linux?

Quota is based upon filesystems, but you can always create a virtual filesystem and mount it on a specific (empty) directory with the usrquota and/or grpquota flags.

In steps this will be:

  1. create the mount point
  2. create a file full of /dev/zero, large enough to the maximum size you want to reserve for the virtual filesystem
  3. format this file with an ext3 filesystem (you can format a disk space even if it is not a block device, but double check the syntax of every - dangerous - formatting command)
  4. mount the newly formatted disk space in the directory you've created as mount point, e.g.
    Code:
    mount -o loop,rw,usrquota,grpquota /path/to/the/formatted/disk/space /path/of/mount/point
  5. Set proper permissions
  6. Set quotas
    and the trick is done.

Tutorial here.
Original answer here

How can I limit the max numbers of folders that user can create in linux

This is what quotas are designed for. You can use file system quotas to enforce limits, per user and/or per group for:

  • the amount of disk size space that can be used
  • the number of blocks that can be used
  • the number of inodes that can be created.

The number of inodes will essentially limit the number of files and directories a user can create.

There is extensive, very good quality documentation about how to configure file system quotas in many sources, which I suggest you read further:

  • https://access.redhat.com/documentation/en-US/Red_Hat_Enterprise_Linux/6/html/Storage_Administration_Guide/ch-disk-quotas.html
  • https://wiki.archlinux.org/index.php/disk_quota
  • http://www.ibm.com/developerworks/library/l-lpic1-v3-104-4/
  • http://www.firewall.cx/linux-knowledgebase-tutorials/linux-administration/838-linux-file-system-quotas.html

Bash script to limit a directory size by deleting files accessed last

Here's a simple, easy to read and understand method I came up with to do this:

DIRSIZE=$(du -s /PATH/TO/FILES | awk '{print $1}')
if [ "$DIRSIZE" -gt "$SOMELIMIT" ]
then
for f in `ls -rt --time=atime /PATH/TO/FILES/*.tar`; do
FILESIZE=`stat -c "%s" $f`
FILESIZE=$(($FILESIZE/1024))

DIRSIZE=$(($DIRSIZE - $FILESIZE))
if [ "$DIRSIZE" -lt "$LIMITSIZE" ]; then
break
fi
done
fi

Folder size linux

If you need to enforce limits then use quotas

Maximum number of files/directories on Linux?

ext[234] filesystems have a fixed maximum number of inodes; every file or directory requires one inode. You can see the current count and limits with df -i. For example, on a 15GB ext3 filesystem, created with the default settings:

Filesystem           Inodes  IUsed   IFree IUse% Mounted on
/dev/xvda 1933312 134815 1798497 7% /

There's no limit on directories in particular beyond this; keep in mind that every file or directory requires at least one filesystem block (typically 4KB), though, even if it's a directory with only a single item in it.

As you can see, though, 80,000 inodes is unlikely to be a problem. And with the dir_index option (enablable with tune2fs), lookups in large directories aren't too much of a big deal. However, note that many administrative tools (such as ls or rm) can have a hard time dealing with directories with too many files in them. As such, it's recommended to split your files up so that you don't have more than a few hundred to a thousand items in any given directory. An easy way to do this is to hash whatever ID you're using, and use the first few hex digits as intermediate directories.

For example, say you have item ID 12345, and it hashes to 'DEADBEEF02842.......'. You might store your files under /storage/root/d/e/12345. You've now cut the number of files in each directory by 1/256th.

Folder size as variable

The size needs to be read from the du < <(command) stream, with tab and new-line as fields separator and no records delimiter, because the output from dc contains multiple lines to be read as a single record.

Here is an example of output from du --bytes --summarize --total /home/user1/testfolder:

4954626   /home/user1/testfolder
4954626 total

Your code modified:

#!/usr/bin/env bash

declare -i limit=200000
declare -- test_folder=/home/user1/testfolder

declare -i folder_size
IFS=$' \t\n' read -r -d '' _ _ folder_size _ < <(
du --bytes --summarize --total "$test_folder"
)

if [[ "$folder_size" -le "$limit" ]];
then
printf 'Folder size is small\n'
else
printf 'Folder size is big\n'
fi


Related Topics



Leave a reply



Submit