coolcommands.com - the search engine for unix sysadmins :: unix commands and scripts
Saturday May 25, 2013


 Home
 Search
 Advanced Search
 Category Search
 Affiliates
 Acknowledgements
 Cool Links

Username

Password

Remember me
Forgot your password?

Who's Online
We have 20 guests online

Newest Member

nbewwmbxog

Hit Counter
11571951 Visitors

cool Statistics
coolcommands:1338
Categories:83
Total queries:620363
Members:22813





Search coolcommands.com
Search Keyword:


coolcommand

Korn shell script to return if a directory is empty or not
Description

Here is a little ksh script to return whether a directory is empty or not.

Return code 0 = empty
Return code 1 = non empty
Return code 2 = not a directory

#!/bin/ksh

# Format: emptydir.ksh [dirname] - default "."

typeset file dir=${1:-.}

[[ -d $dir ]] || { print -u2 "$0: $dir is not a directory"; return 2; }

for file in $dir/.* $dir/*
do
case ${file#$dir/} in

.|..) ;;

[*]) [[ -a $file ]];let $?;return;;

*) return 1;;

esac
done


Sample runs:

1) Test current directory (.) - return code 1 = no
# ./emptydir.ksh
# echo $?
1

2) Test directory emptydir - return code 0 = empty
# ./emptydir.ksh ./emptydir
# echo $?
0

3) Test directory ./some/dir/non/existant - return code 2 = non existant directory
# ./emptydir.ksh ./some/dir/non/existant
./emptydir.ksh: ./some/dir/non/existant is not a directory
# echo $?
2
Example

emptydir.ksh

Return to search results

Random coolcommands

coolcommand: kill all the processes of a particular user
Example: ps -fU username | awk '{ system ("kill "$2)}'
[show me the details]
coolcommand: how to prepare older Linux systems for 2007 DST change
Example: rpm –u tzdata-2006m-3.el4.noarch.rpm; reboot
[show me the details]
coolcommand: how to determine the maximum number of user processes in Linux
Example: ulimit -u
[show me the details]
coolcommand: how to add a disk to a volume group in AIX
Example: extendvg rootvg hdisk3
[show me the details]

coolcommands.com - the search engine for UNIX sysadmins :: unix commands and scripts



FREE
myPMS

Password
Management
System

by

LVOware.com




Top 5 Submitters

lvo1160
audet37
kah00na32
Mariom24
lebjf00014

Top 5 Searches

Category Solaris51966
Category AIX49058
Category General UNIX47587
solaris23554
aix22739

Terms of Use
Privacy
Contact


 
coolcommands.com © 2013. All Right Reserved.
Programming by PRATTICO Consulting . Hosting by Apricusum