LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 03-11-2020, 12:46 PM   #16
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: Slackware (15 current), Slack15, Ubuntu studio, MX Linux, FreeBSD 13.1, WIn10
Posts: 10,342

Rep: Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242

Quote:
Originally Posted by petelq View Post
I did a brief test with "$(ls -A)=0" and it worked. But your way's good also.
I'd think that the -z in if [ -z "$(ls -A)" ]; then is basically doing what "$(ls -A)=0" is doing behind the scenes. it checks if empty somehow then pits it against the condition, returns true or false.
 
Old 03-11-2020, 05:53 PM   #17
rknichols
Senior Member
 
Registered: Aug 2009
Distribution: Rocky Linux
Posts: 4,779

Rep: Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212
Quote:
Originally Posted by petelq View Post
I did a brief test with "$(ls -A)=0" before my original post and it worked. But your way's good also.
What happens if you have a single file named "0" there?
 
Old 03-11-2020, 08:49 PM   #18
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: Slackware (15 current), Slack15, Ubuntu studio, MX Linux, FreeBSD 13.1, WIn10
Posts: 10,342

Rep: Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242
Quote:
Originally Posted by rknichols View Post
What happens if you have a single file named "0" there?
Quote:
Originally Posted by bash page
string comparison

=

is equal to

if [ "$a" = "$b" ]

Caution

Note the whitespace framing the =.

if [ "$a"="$b" ] is not equivalent to the above.
==

is equal to

if [ "$a" == "$b" ]

This is a synonym for =.

Note

The == comparison operator behaves differently within a double-brackets test than within single brackets.

[[ $a == z* ]] # True if $a starts with an "z" (pattern matching).
[[ $a == "z*" ]] # True if $a is equal to z* (literal matching).

[ $a == z* ] # File globbing and word splitting take place.
[ "$a" == "z*" ] # True if $a is equal to z* (literal matching).

# Thanks, Stéphane Chazelas

---------------
A binary comparison operator compares two variables or quantities. Note that integer and string comparison use a different set of operators.

integer comparison

-eq

is equal to

if [ "$a" -eq "$b" ]
source:
https://tldp.org/LDP/abs/html/comparison-ops.html

Last edited by BW-userx; 03-11-2020 at 08:50 PM.
 
Old 03-11-2020, 10:46 PM   #19
rknichols
Senior Member
 
Registered: Aug 2009
Distribution: Rocky Linux
Posts: 4,779

Rep: Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212
@BW-userx: Yes, I am well aware of that. I was wondering if @petelq had considered that $(ls -A) could return "0" for a non-empty directory.
 
Old 03-12-2020, 11:39 AM   #20
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: Slackware (15 current), Slack15, Ubuntu studio, MX Linux, FreeBSD 13.1, WIn10
Posts: 10,342

Rep: Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242
Quote:
Originally Posted by rknichols View Post
@BW-userx: Yes, I am well aware of that. I was wondering if @petelq had considered that $(ls -A) could return "0" for a non-empty directory.
yeah I gave thought to who I was posting that too after the fact. it was like wait a min he should know that ... but decided to leave it for others.
 
Old 03-12-2020, 11:44 AM   #21
rtmistler
Moderator
 
Registered: Mar 2011
Location: USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 9,882
Blog Entries: 13

Rep: Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930
Quote:
Originally Posted by Skaperen View Post
the purpose is simply to increase efficiency in a file scan generator, to avoid trying to read the list of names if there are none. it appears that the filesytem code or kernel reads at least one empty 4k block from the directory when trying to read names. that is probably good evidence that there is no way to determine that, at least for filesystems i have tried (ext2,ext3,ext4,btrfs,reiserfs). it's not a critical need. i can just go ahead and read the list of names and see if it is empty, or just not deal with being empty.

this project is a generator in python3 that yields each path in name sorted order with the file type (regular file vs directory, etc) included in the yielded tuple.
@Skaperen,

I've seen this thread as active a lot, but haven't followed along.

Is this solved, or are you still making a determination as to what your next move is?
 
Old 03-12-2020, 02:50 PM   #22
dugan
LQ Guru
 
Registered: Nov 2003
Location: Canada
Distribution: distro hopper
Posts: 11,225

Rep: Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320
This is how "find -empty" is implemented. The implementation just lists the directory and checks if it's empty:

https://git.savannah.gnu.org/cgit/fi...nd/pred.c#n349
 
2 members found this post helpful.
Old 03-14-2020, 06:27 PM   #23
GazL
LQ Veteran
 
Registered: May 2008
Posts: 6,897

Rep: Reputation: 5019Reputation: 5019Reputation: 5019Reputation: 5019Reputation: 5019Reputation: 5019Reputation: 5019Reputation: 5019Reputation: 5019Reputation: 5019Reputation: 5019
for (dp = readdir (d); dp; dp = readdir (d))

That duplication of readdir(d) calls looks funky; I wonder what the author has against while ( )?


POSIX has this to say regarding readdir():
Quote:
If entries for dot or dot-dot exist, one entry shall be returned for dot and one entry shall be returned for dot-dot; otherwise, they shall not be returned.
I couldn't find anywhere where it says that the entries *must* exist, so checking for only two entries, as I did above, is likely making unsafe assumptions about the filesystem implementation, and is why they're doing it the way they are in the code dugan linked.

Last edited by GazL; 03-14-2020 at 06:34 PM.
 
Old 03-15-2020, 08:13 AM   #24
MadeInGermany
Senior Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 2,793

Rep: Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201
The for (dp = readdir (d); dp; dp = readdir (d)) is shorter than a
dp = readdir (d); while (dp) {
...
dp = readdir (d);
}

Further, there is code that sorts out . and ..
so it only finds other entries.
--
I still don't believe that
[ $(ls -A)=0 ]
can work. It is a conatenation of $(ls -A) and the string "=0".
Where the latter makes it always non-empty so the [ ] gives always true.
I only can imagine there might be a [ version out there stat can give error "two many arguments".
 
Old 03-15-2020, 11:08 AM   #25
GazL
LQ Veteran
 
Registered: May 2008
Posts: 6,897

Rep: Reputation: 5019Reputation: 5019Reputation: 5019Reputation: 5019Reputation: 5019Reputation: 5019Reputation: 5019Reputation: 5019Reputation: 5019Reputation: 5019Reputation: 5019
Quote:
Originally Posted by MadeInGermany View Post
The for (dp = readdir (d); dp; dp = readdir (d)) is shorter than a
dp = readdir (d); while (dp) {
...
dp = readdir (d);
}
Yes, but it's not shorter than:
Code:
while ( dp = readdir(d) ) {
...
}
... which was what I was getting at.
 
2 members found this post helpful.
Old 03-15-2020, 03:11 PM   #26
MadeInGermany
Senior Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 2,793

Rep: Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201
That is indeed shorter!
Perhaps to be augmented with a comment "assign to dp and test for nonzero".
 
Old 03-16-2020, 06:34 AM   #27
GazL
LQ Veteran
 
Registered: May 2008
Posts: 6,897

Rep: Reputation: 5019Reputation: 5019Reputation: 5019Reputation: 5019Reputation: 5019Reputation: 5019Reputation: 5019Reputation: 5019Reputation: 5019Reputation: 5019Reputation: 5019
Quote:
Originally Posted by MadeInGermany View Post
That is indeed shorter!
Perhaps to be augmented with a comment "assign to dp and test for nonzero".
Personally, I wouldn't comment that. If you want to be more explicit then you could write it as:
Code:
while ( (dp = readdir(d)) != NULL ) {
...
}
... but the implicit comparison in my previous example is unlikely to throw any experienced C programmers a curveball. gcc does prefer you to wrap the assignment in an additional but unnecessary set of parenthesis(when using the implicit form), otherwise it will throw out a warning: supposedly, it helps catch common '=' vs '==' substitution errors.


I know some folks prefer for() over while() because a "while(something);" when looked at out of context can be confused with the end of a "do/while(something);", but that's always struck me as a pretty weak-minded rationale. I am genuinely interested in why they chose a for() there.

Last edited by GazL; 03-16-2020 at 06:38 AM.
 
Old 03-16-2020, 07:10 AM   #28
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,849

Rep: Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309
Quote:
Originally Posted by Skaperen View Post
the purpose is simply to increase efficiency in a file scan generator, to avoid trying to read the list of names if there are none.
That is just nonsense. You need to read the directory to know if there were any object inside. If something found you can walk thru, if not you can just skip the given dir.
Any attempt to calculate the number of objects will slow down the execution: if there was anything inside (because that would be just a superfluous extra step) and will not speed up anything if that dir was empty.
 
Old 03-26-2020, 01:10 AM   #29
Skaperen
Senior Member
 
Registered: May 2009
Location: center of singularity
Distribution: Xubuntu, Ubuntu, Slackware, Amazon Linux, OpenBSD, LFS (on Sparc_32 and i386)
Posts: 2,684

Original Poster
Blog Entries: 31

Rep: Reputation: 176Reputation: 176
if there was a way to determine that a directory is empty from its inode, then that would avoid reading the directory's data block(s) for instances of an empty directory. what if this is a huge tree with millions of directories that could be empty. my intent was to determine this from a stat() or the like, which would get the inode from the cache if not reading it for the very first time. since the inode does not record this information, this is all a moot effort.
 
Old 03-26-2020, 03:05 AM   #30
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,849

Rep: Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309
still nonsense: creating an external counter/status info will be a huge overhead on directory handling (not to speak about the concurrent access to it) - just to make directory reading slightly and occasionally faster.
But anyway: you are allowed to design your own filesystem.
 
  


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 On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] F27 - Empty Trash Bin icon is showing the non-empty icon Connor84 Fedora 2 02-17-2018 01:18 AM
LXer: How To Empty a File, Delete N Lines From a File, Remove Matching String From a File, And Remove Empty/Blank Lines From a File In Linux LXer Syndicated Linux News 0 11-22-2017 12:30 PM
Scripting Help--Check empty string condition (not null, but empty!) sungchoiok Linux - Newbie 4 01-01-2012 03:46 PM
[SOLVED] [BASH] non-empty variable before loop end, is empty after exiting loop aitor Programming 2 08-26-2010 09:57 AM
Gentoo VNC, empty dialog box !! "Question" window is empty ! TheHushedCaskeT Linux - Software 0 02-01-2005 10:14 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

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

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