Re: Bash problems?

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



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
[Index of Archives]     [Older Fedora Users]     [Fedora Announce]     [Fedora Package Announce]     [EPEL Announce]     [Fedora Magazine]     [Fedora News]     [Fedora Summer Coding]     [Fedora Laptop]     [Fedora Cloud]     [Fedora Advisory Board]     [Fedora Education]     [Fedora Security]     [Fedora Scitech]     [Fedora Robotics]     [Fedora Maintainers]     [Fedora Infrastructure]     [Fedora Websites]     [Anaconda Devel]     [Fedora Devel Java]     [Fedora Legacy]     [Fedora Desktop]     [Fedora Fonts]     [ATA RAID]     [Fedora Marketing]     [Fedora Management Tools]     [Fedora Mentors]     [SSH]     [Fedora Package Review]     [Fedora R Devel]     [Fedora PHP Devel]     [Kickstart]     [Fedora Music]     [Fedora Packaging]     [Centos]     [Fedora SELinux]     [Fedora Legal]     [Fedora Kernel]     [Fedora OCaml]     [Coolkey]     [Virtualization Tools]     [ET Management Tools]     [Yum Users]     [Tux]     [Yosemite News]     [Gnome Users]     [KDE Users]     [Fedora Art]     [Fedora Docs]     [Asterisk PBX]     [Fedora Sparc]     [Fedora Universal Network Connector]     [Libvirt Users]     [Fedora ARM]

  Powered by Linux