Visit Jeremy's Blog.
Go Back > Forums > Linux Forums > Linux - Software
User Name
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.


  Search this Thread
Old 10-07-2005, 06:14 PM   #1
Registered: Jun 2004
Location: San Francisco
Distribution: Slackware, Ubuntu, RHEL, OS X
Posts: 159

Rep: Reputation: 30
Bash Script test for empty dir

I'm sure this has been answered, but I'll be damned if I can find it... anyway....

My questions: What I'm looking for is a quick way to tell if a dir has files in it or if it is empty.

A long winded version of the same: I'm writing a bash script which will do some stuff to all of the files in a folder. However I would like to start by looking to see if there are any files in the folder first. The problem is I have no prior knowledge of how they will be named. I can do something like test -e /dir/* but the issue with this is that I will sometimes encounter a dir with thousands of files in which case I get an error, along with the unnecessary overhead of checking to see if everyone on of those files exists. Is there an easy way to check this?
Old 10-07-2005, 06:56 PM   #2
Registered: Nov 2004
Location: BIOS
Distribution: RHEL3.0, FreeBSD 5.x, Debian 3.x, Soaris x86 v10
Posts: 379

Rep: Reputation: 30
Well here they have tons of script
Old 10-07-2005, 07:11 PM   #3
Senior Member
Registered: Oct 2003
Posts: 3,057

Rep: Reputation: 58
Find has a test for empty called ( -empty )
I haven't played with it much but I noticed a problem. Even an "empty" folder has . and ..
so the following gives me nothing
find /home/fred -type d -empty -exec ls {} \;
This command seems to work a little better....
find /home/fred -empty -exec ls {} \;

Last edited by homey; 10-07-2005 at 07:12 PM.
Old 10-07-2005, 07:45 PM   #4
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 23,047
Blog Entries: 11

Rep: Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908
A somewhat crude way ...
if [[ `du -bs $dir|cut -f1` -eq 48 ]]; then echo "$dir empty"; fi



Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
bash script 'test' question linmix Programming 5 11-29-2005 12:49 PM
Bash script to compare dir contents Boffy Programming 2 08-02-2005 07:08 AM
Bash script (untar *.tar files and then cd to the untared dir) k0ljat Linux - Newbie 2 12-30-2003 06:57 AM
benchmark test bash script PTBmilo Programming 3 02-14-2003 05:37 PM
bash script to rm all files in a dir keirobyn Programming 8 07-19-2002 08:53 AM

All times are GMT -5. The time now is 05:39 AM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration