Find Files Larger Than a Given Size on Solaris with find -size
Purpose
find . -size +400 does not list files over 400 bytes. A bare number after -size counts 512-byte blocks, and files that exceed that size are listed
. So +400 means more than 204,800 bytes, about 200 KB. To set the limit in bytes, put a c after the number.
find -size walks a directory tree and prints the files that are over, under or exactly at a size you give. Admins use it when a file system such as /var fills up and they need to know which files hold the space. They also run it before cleaning out home directories or archiving a tree. It is used on Solaris 7 through 10 systems, and the -size test is defined the same way on Solaris 11.4 and illumos.
It is one of the commands collected on the Solaris commands and scripts page .
Code
1find . -size +10000c
This lists every file under the current directory that is larger than 10,000 bytes. It is the "Find Command for certain size files" entry on the Solaris commands page.
Code Breakdown
. (the start directory)
The . is where the search starts, which is the current directory. Change it to search somewhere else, for example find /var -size +10000c. find does not stop at the start directory's own files. It goes down through the tree below it, and it follows mount points into other file systems unless you tell it not to (see Safe usage).
-size n
-size is a test: it is true if the file is n blocks long, at 512 bytes per block
. The block is the default unit. Without a suffix, every number you give is a count of 512-byte blocks, not bytes.
The +, - and exact forms
The sign in front of the number sets the comparison:
| Form | Meaning |
|---|---|
+n | more than n |
n | exactly n |
-n | less than n |
-size +10000c means more than 10,000 bytes. -size -10000c means less than 10,000 bytes. -size 10000c matches only files of exactly 10,000 bytes.
The c suffix
If n is followed by c, the size is in bytes. That is why the landing-page command reads +10000c. Use c whenever you want an exact byte threshold. With no c, +10000 would mean more than 10,000 blocks, or 5,120,000 bytes, which is about 500 times the limit you meant.
To set a threshold in megabytes, do the arithmetic first. 10 MB is 10,485,760 bytes, so -size +10485760c works. 10 MB is also 20,480 blocks, so -size +20480 gives the same result.
The implied -print
The landing-page command has no action at the end. If no expression is present, find uses -print as the expression, and -print displays the output of the find command. So the command prints one path per line for each match.
The block form
The same kind of search, written in blocks:
1find . -size +400 -print
This lists files over 400 blocks (204,800 bytes) in the current directory tree. It writes out -print, which does the same as leaving it off here. Writing -print becomes important once you add more tests (see Troubleshooting).
Key Points
-sizemeans 512-byte blocks by default and bytes withcon Solaris 10, Solaris 11.4 and illumos . A-sizetest written for Solaris 10 works unchanged on 11.4 and illumos.- Solaris 11.4 lists
-mmin(data last modified n minutes ago) and-print0, which ends each name with a null character instead of a newline. illumos also lists-print0, and it adds-delete. - On Solaris 10,
-mountlimits the search to the file system that holds the start directory, and-xdevis the same as-mount. Use whichever one your other scripts already use. -sizeonly measures. It changes nothing. Any risk comes from what you add after it, such as-execor-delete.
Insights and Best Practices
Safe usage
Start with a listing, and act only after you have read it. Add -type f to match only plain files, and -mount to stay on one file system:
1find /var -mount -type f -size +20480 -print
That prints plain files on the /var file system larger than 20,480 blocks (10 MB).
Using -type is not enough on its own to control which files an -exec command acts on. Between the test and the action, a file can change. Do not tie a remove command directly to a broad size search on a live system. Save the list, check it, then act on the paths you have checked.
On illumos, -delete deletes the files and folders found recursively from the current directory. Run the exact same expression with -print first, and check the start directory before you swap in -delete.
Alternatives
For a single directory, ls -s shows sizes in blocks
, and a sort puts the largest first:
1ls -s | sort -nr | more
This ranks files rather than filtering them by a threshold. It does not go down into subdirectories the way find does.
du reports which directories hold the space, with these options on illumos
:
du -kreports sizes in 1024-byte units instead of the default 512-byte units.du -sreports only the total for each file or directory you name.du -ddoes not cross file system boundaries.du -Acounts the apparent size of a file instead of the disk blocks it uses.
Run du -sk on the top-level directories to find the heavy one, then find -size inside it to name the files.
Scripting the command
A bare path list does not show sizes. Two ways to add them on Solaris 10:
1find /var -mount -type f -size +20480 -exec ls -ld {} +
2find /var -mount -type f -size +20480 -ls
When -exec ends with +, find calls the command fewer times and replaces {} with groups of path names. That is faster than running ls once per file. -ls prints each path along with its details and needs no extra process.
On Solaris 11.4 and illumos, -print0 ends each name with a null. Use it when the output goes to a tool that reads null-terminated names, so file names with spaces or newlines stay whole.
What to log
Keep the -ls output of a cleanup run. Each line gives, in order, the inode number, the size in kilobytes (1024 bytes), the protection mode, the number of hard links, the user, the group, the size in bytes and the modification time. Save one copy before the cleanup and one after, so you have a record of what was there, who owned it and how large it was. Write the log to a file system other than the one that is full.
When to Run This
/varis filling up. List the large files on/varwith-mountand remove or move the ones you can. If you run out of space in/var, do not symbolically link/varto a directory on another file system with more space.- A home directory tree needs a cleanup. Run a size search to find old core files, archives and copies before asking users what to keep.
- A tree includes NFS mounts. On illumos,
-localis true only when the file system type is not a remote type defined in/etc/dfs/fstypes. It only filters the output; find still descends remote directories. To skip them, prune:find /export ! -local -prune -o -type f -size +20480 -print. - A tree is about to be archived. Look for unexpected large files first, so the archive does not grow far beyond what you planned.
- A disk usage alert fires and
duhas narrowed it to one directory. Usefind -sizethere to name the files.
Troubleshooting Common Issues
The list is empty or far too short. The number was read as blocks, not bytes. -size +10000 means more than 10,000 blocks, and -size +100 means more than 51,200 bytes. Add c for bytes:
1find . -size +10000c
Every file shows up. A test placed after -print comes too late. Put the test before the -print argument; otherwise find gives all files:
1find . -size +400 -print
The search goes onto NFS mounts or other file systems. Add -mount or -xdev to stay on the starting file system. On illumos, -local only filters the output; find still descends remote directories. To skip them, prune: find /export ! -local -prune -o -type f -size +20480 -print. The portable form stays on the starting file system:
1find /export -mount -type f -size +20480 -print
The sizes do not match du. By default du counts the disk blocks a file uses. On compressed or sparse files, that differs from the file's length. On illumos, compare with the apparent size:
1du -Ak /var
On Solaris 10, /usr/bin/du
has no -A option. Use ls -l there instead. It shows each file's length in bytes, so you can compare that with what du -k reports for the same file.
Directories you cannot read are left out without a warning. /usr/bin/du says nothing when it cannot read a file or directory unless you give -r:
1du -rsk /var
References
- How to Find Files That Exceed a Specified Size Limit (System Administration Guide: Advanced Administration)
- 512-byte blocks for
-size +nnn, thefind . -size +400 -printexample, what-printdoes - Checking the Size of Files (System Administration Guide: Advanced Administration)
-
ls -sblock sizes,ls -lbyte sizes,ls -s | sort -nr | more, the/varsymlink warning - find(1), Oracle Solaris 10 1/13 Information Library
-
-sizeand thecsuffix,+n/n/-n, the implied-print,-mountand-xdev,-exec ... {} +,-lsfields - find(1), Oracle Solaris 11.4 Reference Library
- unchanged
-sizedefinition,-mmin,-print0 - find(1), illumos
-
-sizedefinition,-delete,-print0,-type, the-execwarning,-local, test order before-print - du(1), illumos
-
du -k,-s,-d,-Aand-r - du(1), Oracle Solaris 10 1/13 Information Library
- Solaris 10
/usr/bin/duoptions:-a,-d,-h,-k,-o,-r,-s,-Hand-L, with no-A