LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 07-13-2011, 08:17 PM   #1
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,008

Rep: Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193
Technique - alternatives to tell if directory empty or not


So this is just a query on personal preferences about what method you would use in a bash script to tell if a directory contained any (this needs to include hidden) files or directories?
The current method I am using is:
Code:
anyford=$(ls -A | wc -l)
if (( anyford > 0 ))
then
    <do stuff here>
fi
I am normally abhorred to use ls if I can help it so would appreciate any feedback on what others would use in this scenario?

Cheers
grail
 
Old 07-13-2011, 09:00 PM   #2
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by grail View Post
So this is just a query on personal preferences about what method you would use in a bash script to tell if a directory contained any (this needs to include hidden) files or directories?
The current method I am using is:
Code:
anyford=$(ls -A | wc -l)
if (( anyford > 0 ))
then
    <do stuff here>
fi
I am normally abhorred to use ls if I can help it so would appreciate any feedback on what others would use in this scenario?

Cheers
grail
Maybe 'stat', specifically, "Links: 2":

Code:
sergei@amdam2:~/junk> /usr/bin/stat .
  File: `.'
  Size: 233472          Blocks: 464        IO Block: 4096   directory
Device: 803h/2051d      Inode: 5325145     Links: 78
Access: (0755/drwxr-xr-x)  Uid: ( 1000/  sergei)   Gid: (  100/   users)
Access: 2011-07-14 04:53:22.000000000 +0300
Modify: 2011-07-14 04:52:40.000000000 +0300
Change: 2011-07-14 04:52:40.000000000 +0300
sergei@amdam2:~/junk> cd empty_dir/
sergei@amdam2:~/junk/empty_dir> /usr/bin/stat .
  File: `.'
  Size: 4096            Blocks: 8          IO Block: 4096   directory
Device: 803h/2051d      Inode: 5382458     Links: 2
Access: (0755/drwxr-xr-x)  Uid: ( 1000/  sergei)   Gid: (  100/   users)
Access: 2011-07-14 04:52:43.000000000 +0300
Modify: 2011-07-14 04:52:40.000000000 +0300
Change: 2011-07-14 04:52:40.000000000 +0300
sergei@amdam2:~/junk/empty_dir> ls -ltrA
total 0
sergei@amdam2:~/junk/empty_dir>
?

Though I've never studied internals of Linux file systems and can't tell whether "Links: 2" is correct/portable.
 
Old 07-13-2011, 09:16 PM   #3
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Rep: Reputation: Disabled
Quote:
Originally Posted by Sergei Steshenko View Post
Though I've never studied internals of Linux file systems and can't tell whether "Links: 2" is correct/portable.
Each directory contains 2 hard-linked directories: . and .. (I'm not sure if this is technically true for the base of a filesystem, but at least the kernel pretends it's that way). If a directory has 2 links that only means it has no subdirectories; it could still have files.

ls might be the way to go, except it will fail if your user has --x access and not r-x. Then again, why would you need to know that if you had --x?
Kevin Barry
 
Old 07-13-2011, 09:40 PM   #4
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,008

Original Poster
Rep: Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193
I have reviewed options for stat and confirmed ta0kira's information is correct that Links: only refers to the number of
directories (or hard links) within a directory, when used in this manner, so not appropriate for my current task.

Thanks for the feedback though
 
Old 07-13-2011, 09:51 PM   #5
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by grail View Post
I have reviewed options for stat and confirmed ta0kira's information is correct that Links: only refers to the number of
directories (or hard links) within a directory, when used in this manner, so not appropriate for my current task.

Thanks for the feedback though
Then you have "Blocks: 8" too.
 
Old 07-13-2011, 10:10 PM   #6
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Rep: Reputation: Disabled
Quote:
Originally Posted by Sergei Steshenko View Post
Then you have "Blocks: 8" too.
This will stay the same up to a certain limit of number of contained files. It also might differ with filesystem type.
Kevin Barry
 
Old 07-13-2011, 10:10 PM   #7
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,008

Original Poster
Rep: Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193
hmmm ... now I think you are stretching. I ran stat on an empty directory and one with files in it and the Blocks count
is the same ... so no real value to that measurement
 
Old 07-13-2011, 10:13 PM   #8
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian, Arch
Posts: 3,784

Rep: Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083
Code:
shopt -s dotglob nullglob
dir_or_files=(*)
if ((${#dir_or_files[@]} > 0))
Unfortunately it requires specific globbing options.
 
Old 07-27-2011, 07:04 AM   #9
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian, Arch
Posts: 3,784

Rep: Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083
find can also identify empty directories:
Code:
find . -type d -empty
 
Old 07-27-2011, 10:59 AM   #10
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,008

Original Poster
Rep: Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193
Thanks for the new idea I would probably have to add maxdepth as well as only interested i current directory.

the idea I am trying to use it for is when a user removes an application I need to test a directory that may need to be removed as other applications
may also have installed files in the directory and hence it should not be removed so as to not break the other applications.
 
Old 07-27-2011, 01:36 PM   #11
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Rep: Reputation: Disabled
Quote:
Originally Posted by grail View Post
Thanks for the new idea I would probably have to add maxdepth as well as only interested i current directory.

the idea I am trying to use it for is when a user removes an application I need to test a directory that may need to be removed as other applications
may also have installed files in the directory and hence it should not be removed so as to not break the other applications.
Maybe you just need to rmdir /path || true.
Kevin Barry
 
Old 07-27-2011, 02:23 PM   #12
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,008

Original Poster
Rep: Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193
Thanks for the suggestion Kevin. May I ask what purpose the '|| true' serves here? It does not seem to alleviate any error messages (which of course I can through to /dev/null).
 
Old 07-27-2011, 03:22 PM   #13
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Rep: Reputation: Disabled
Quote:
Originally Posted by grail View Post
Thanks for the suggestion Kevin. May I ask what purpose the '|| true' serves here? It does not seem to alleviate any error messages (which of course I can through to /dev/null).
Just a habbit from writing makefiles and controlling the return value of shell functions, I suppose.
Kevin Barry
 
Old 07-27-2011, 03:31 PM   #14
gnashley
Amigo developer
 
Registered: Dec 2003
Location: Germany
Distribution: Slackware
Posts: 4,928

Rep: Reputation: 612Reputation: 612Reputation: 612Reputation: 612Reputation: 612Reputation: 612
Here's a pure-shell version:
Code:
set $(echo .* *)
[ $3 ] # non-empty or maybe you'd like `[ -n $3 ]` better
 
1 members found this post helpful.
Old 07-27-2011, 03:58 PM   #15
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
Another bash approach:
Code:
[ dir/* == "dir/*" ] 2> /dev/null && echo empty
This is true only if the directory is empty, otherwise you get an error. Single square brackets are mandatory, since filename expansion doesn't take place inside double square brackets.
 
  


Reply



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
rm: cannot remove directory `ff': Directory not empty blancs Linux - General 17 08-29-2011 01:47 PM
Empty a directory fhsm Linux - Newbie 3 04-16-2009 11:19 PM
I deleted my /etc/alternatives directory. Now a lot of programs do not work. Help? tpsomers Linux - Newbie 3 06-05-2008 09:43 AM
The postgresql data directory content in the pgsql directory is lost (empty data dir) kisembo Linux - Software 1 02-13-2006 01:11 PM
How to check if a directory is empty? Aziz Linux - Software 2 09-27-2004 04:46 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:04 PM.

Main Menu
Advertisement
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
Open Source Consulting | Domain Registration