Your question is unclear (we can just guess what "limit" means, but we don't understand what it is), and lacks motivation.
At the system call level (see syscalls(2)), the relevant system calls are setrlimit(2) with RLIMIT_FSIZE
then signal(7) related ones (e.g. sigaction(2)) with SIGXFSZ
.
So you might use the ulimit
builtin and the trap
one. However, a program executed by your shell script might change the file size limit, and could catch the SIGXFSZ
signal.
Beware, the size of a directory (as given by stat(2), so stat(1) & ls(1)) is not the accumulated size of its files (since some files could be hard-linked in several directories; files are inodes - see inode(7)). It is just the size of directory entries.
Alternatively, you might compute the accumulated size of files (using du(1) or find(1) or gawk(1)) and take some action according to it, maybe deleting some big files (But consider -perhaps by deciding to ignore them- other processes writing files or directories during the execution of your script).
Perhaps you could be interested by disk quotas (see quotactl(2), quota(1), quotacheck(8))
You could do incremental backups. You might use other utilities than tar(1) for that purpose (e.g. dar, afio, ...). Notice that mv(1) & cp(1) & tar have some --backup
option. Perhaps you might consider logrotate(8) to deal with growing log files.