LinuxQuestions.org
LinuxAnswers - the LQ Linux tutorial section.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
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.

Notices

Reply
 
Search this Thread
Old 10-07-2005, 05:14 PM   #1
uopjohnson
Member
 
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, 05:56 PM   #2
nixcraft
Member
 
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 http://groups.google.com/group/comp....5cf99ad2fdc1c8
 
Old 10-07-2005, 06:11 PM   #3
homey
Senior Member
 
Registered: Oct 2003
Posts: 3,057

Rep: Reputation: 56
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
Code:
find /home/fred -type d -empty -exec ls {} \;
Code:
This command seems to work a little better....
find /home/fred -empty -exec ls {} \;

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

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

Cheers,
Tink
 
  


Reply


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 11:49 AM
Bash script to compare dir contents Boffy Programming 2 08-02-2005 06:08 AM
Bash script (untar *.tar files and then cd to the untared dir) k0ljat Linux - Newbie 2 12-30-2003 05:57 AM
benchmark test bash script PTBmilo Programming 3 02-14-2003 04:37 PM
bash script to rm all files in a dir keirobyn Programming 8 07-19-2002 07:53 AM


All times are GMT -5. The time now is 02:47 AM.

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