Solaris / OpenSolarisThis forum is for the discussion of Solaris, OpenSolaris, OpenIndiana, and illumos.
General Sun, SunOS and Sparc related questions also go here. Any Solaris fork or distribution is welcome.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
At work, we hired a consultant to help us with our shopping cart software (because the tmp files were never getting cleaned out). His solution was handing us a shell script that would do it for us. The only problem is that it's not working on Solaris 9. I think Solaris 9 doesn't support the -empty option, but I'm not sure. Could anyone help me fix this script?
It searches for all directories and tries to remove them using rmdir. rmdir will refuse to remove dirs that are not empty. Check to see if there isn't an alias defined that overrides this!!! (man rmdir for details).
The 2>/dev/null is there to get rid of all the error messages rmdir produces on non-empty dirs.
Distribution: Solaris 11.4, Oracle Linux, Mint, Debian/WSL
Posts: 9,789
Rep:
A cleaner approach would be to use the find command for which the original script was written, aka gnufind or gfind, which is certainly available for Solaris, at least on blastwave.
Distribution: Solaris 11.4, Oracle Linux, Mint, Debian/WSL
Posts: 9,789
Rep:
That's a interesting approach but it it should be adjusted if "cd" is not silent but removing it from the left part of the test or redirecting its stdout. Also, if a directory contains a large number of files, "echo *" might be quite slow and even output "arg list too long" messages.
That's a interesting approach but it it should be adjusted if "cd" is not silent but removing it from the left part of the test or redirecting its stdout. Also, if a directory contains a large number of files, "echo *" might be quite slow and even output "arg list too long" messages.
All quite true.
But other than "find -empty" which is not available on older UNIX's there are few ways to test for empty directories, such that they do not read the whole directory, especially "extremely large" directories.
However 'find' has to still read the whole directory anyway as part of its efforts to recurse into sub-directories. As such the ideal solution is make it part of find (EG: --empty and --delete). Which leaves you with the same problem of not being available on older UNIX machines.
Or implement a special perl command (or other modern language with looped directory reads), that lets you abort the read as soon as it reads a single item (directory not empty). But that means perl (or other) needs to be available. But then these days, perl (at least) is generally available and a standard install even on most older UNIX's, even if GNU-find isn't.
Perl::Find empty directory removal script anyone? Without using non-standard library modules that may not be installed!
Distribution: Solaris 11.4, Oracle Linux, Mint, Debian/WSL
Posts: 9,789
Rep:
There is no fundamental need to check if the directory is empty as rmdir will check it too and won't remove non empty directories anyway. There is no doubt rmdir kernel implementation will check the directories emptiness much faster than whatever userland code.
There is no fundamental need to check if the directory is empty as rmdir will check it too and won't remove non empty directories anyway. There is no doubt rmdir kernel implementation will check the directories emptiness much faster than whatever userland code.
That is true, as long as you don't mind removing all the error messages.
But then there can be more to it, depending on your definition on what constitutes a 'empty directory'.
Case in point. I had a directory structure containing more than a million files, and tens of thousands of diretories. An empty directory however was classed as being a directory that was really empty, or only contained a single Index/ReadMe type file. Any other file and the directory was not empty.
In that case I could not rely on 'rmdir' or even a 'find' to test for emptiness. Mind you I did not need to worry about recursion either, as all such directories were all at the same level.
Distribution: Solaris 11.4, Oracle Linux, Mint, Debian/WSL
Posts: 9,789
Rep:
Removing not only empty directories but also non empty empty ones depending on some characteristics is ruling out generic commands and then requires some scripting/programming.
Here is a variant of my Unix cleantmp script, tailored for your needs:
Code:
#!/bin/sh
dirs_to_clean="/usr/serverdirectory/tmp"
max_days_mtime=2
max_days_atime=0
#owner_to_keep="root nobody"
owner_to_keep=""
omit=""
for i in $owner_to_keep
do
omit="$omit ( ! -user $i )"
done
for dir in $dirs_to_clean
do
[ -d "$dir" ] &&
cd "$dir" &&
find . -depth \! -type d \( -mtime +$max_days_mtime -o -mtime -0 \) \
\( -atime +$max_days_atime -ctime +$max_days_atime -o -type l \) \
$omit -exec rm -f {} \; -o \
-type d -links 2 $omit -mtime +$max_days_mtime -exec rmdir {} \; 2>/dev/null
sleep 1
done
# Notes:
# A just emptied directory is unlikely deleted immediately - but after another max_days_mtime.
# -mtime -0 detects files with a future time stamp (e.g. from an obscure archive).
# -links 2 should be replaced by -empty - if find would support it.
# -exec rm -f {} \; and -exec rmdir {} \; should be replaced by -delete - if find would support it.
More Notes:
The atime lines protect recently accessed files - delete these lines if you want to only consider the mtime.
The -links 2 is true for directories that have no sub-directories, thus reduces the number of rmdir attempts.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.