Tomas Larsson wrote:
Dear group.
How do I do to, within a scrip, check if a directory I empty or not.
Cant find a way to do this in a simple way.
Try these:
function isempty { test "`find "$1" -prune -empty`"; }
function isemptydir { test "`find "$1" -prune -type d -empty`"; }
The first returns true for either empty files or empty directories; the
second returns true only if the given file is an empty directory.
$ mkdir emptydir; touch emptyfile
$ if isempty emptydir; then echo 'yes'; else echo 'no'; fi
yes
$ if isempty emptyfile; then echo 'yes'; else echo 'no'; fi
yes
$ if isemptydir emptyfile; then echo 'yes'; else echo 'no'; fi
no
$ if isemptydir emptydir; then echo 'yes'; else echo 'no'; fi
yes
Using find to make a direct test is more efficient (and far more
efficient for large directories) than listing the contents of a
directory just to see if there's anything in it--not that efficiency in
shell scripts matters all that much.
Here's some more tests (and some things to watch out for)...
$ if isemptydir /tmp; then echo 'yes'; else echo 'no'; fi
no
$ if isempty /tmp; then echo 'yes'; else echo 'no'; fi
no
$ if isempty /lost+found; then echo 'yes'; else echo 'no'; fi
find: /lost+found: Permission denied
no
$ if isempty /usr/tmp; then echo 'yes'; else echo 'no'; fi
no
$ ls -a /usr/tmp
. ..
$ if isemptydir /usr/tmp; then echo 'yes'; else echo 'no'; fi
no
$ ls -l /usr/tmp
lrwxrwxrwx 1 root root 10 Jun 8 19:22 /usr/tmp -> ../var/tmp
$ if isemptydir /var/tmp; then echo 'yes'; else echo 'no'; fi
yes
<Joe
--
fedora-list mailing list
fedora-list@xxxxxxxxxx
To unsubscribe: https://www.redhat.com/mailman/listinfo/fedora-list