LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
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 01-08-2016, 11:37 PM   #1
nyloc
Member
 
Registered: Aug 2007
Location: Melbourne
Posts: 111

Rep: Reputation: 17
Unsure about results of find -type d on empty directory


As an exercise in writing bash scripts and using recursion I created a script to count the number of files and directories in my home directory. To check the result of my script I ran find -type f | wc -l and find -type d | wc -l to compare the count of files and directories. The results from the find runs differed from my scripts results. So after allowing for links and hidden files I still had a discrepancy.
The results of find -type f when run on an empty directory returns no file names and a wc count of 0, but when find -type d is run on an empty directory it returns the name of the directory giving wc a count of 1.
Is an answer of 0 or 1 the correct response for an empty directory?
 
Old 01-09-2016, 03:40 AM   #2
berndbausch
LQ Addict
 
Registered: Nov 2013
Location: Tokyo
Distribution: Mostly Ubuntu and Centos
Posts: 6,316

Rep: Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002
What do you mean by correct response?

To me, find's output is not surprising. The directory is not a regular file, thus nothing is printed when using -type f. It's a directory, thus its name is printed when using -type d.
 
Old 01-09-2016, 05:57 AM   #3
nyloc
Member
 
Registered: Aug 2007
Location: Melbourne
Posts: 111

Original Poster
Rep: Reputation: 17
I will try expressing this a different way. As I read the manual the command find enquiry_dir -type d should tell me how many directories are in enquiry_dir.
So I run this code
col@nyloc:~$
col@nyloc:~$ mkdir testdir
col@nyloc:~$ mkdir testdir/emptydir
col@nyloc:~$ find testdir/emptydir/ -type f
col@nyloc:~$ find testdir/emptydir/ -type b
col@nyloc:~$ find testdir/emptydir/ -type c
col@nyloc:~$ find testdir/emptydir/ -type p
col@nyloc:~$ find testdir/emptydir/ -type l
col@nyloc:~$ find testdir/emptydir/ -type s
col@nyloc:~$ find testdir/emptydir/ -type d
testdir/emptydir/
col@nyloc:~$ ls testdir/emptydir/
col@nyloc:~$ ls testdir/
emptydir/
col@nyloc:~$

Why does the test for how many directories are in an empty directory not come back with null as all the other tests do?
 
Old 01-10-2016, 12:26 AM   #4
berndbausch
LQ Addict
 
Registered: Nov 2013
Location: Tokyo
Distribution: Mostly Ubuntu and Centos
Posts: 6,316

Rep: Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002
Quote:
Originally Posted by nyloc View Post
As I read the manual the command find enquiry_dir -type d should tell me how many directories are in enquiry_dir.
I don't see this stated in the man page. As far as I understand it, find also processes the root of the directory tree, that is enquiry_dir itself.

However the -empty option may help:
Code:
find testdir/emptydir ! -empty -type d
 
1 members found this post helpful.
Old 01-10-2016, 02:58 AM   #5
nyloc
Member
 
Registered: Aug 2007
Location: Melbourne
Posts: 111

Original Poster
Rep: Reputation: 17
I must need a new book on Linux then.
From Linux in a Nutshel written by Jessica Perry Hekman published by O'Reilly Copyright 1997

find
-type c
Find files who type is c. c can be b, c, d, p, l, s, or f.

there are no files of type d in emptydir in the above example.
 
Old 01-10-2016, 04:20 AM   #6
berndbausch
LQ Addict
 
Registered: Nov 2013
Location: Tokyo
Distribution: Mostly Ubuntu and Centos
Posts: 6,316

Rep: Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002
Quote:
Originally Posted by nyloc View Post
I must need a new book on Linux then.
From Linux in a Nutshel written by Jessica Perry Hekman published by O'Reilly Copyright 1997

find
-type c
Find files who type is c. c can be b, c, d, p, l, s, or f.

there are no files of type d in emptydir in the above example.
There are no files in emptydir, but emptydir itself is a directory, and therefore printed. Find doesn't operate on files in a directory, but all files in a directory subtree, including the root of that subtree.
 
Old 01-10-2016, 10:41 AM   #7
rknichols
Senior Member
 
Registered: Aug 2009
Distribution: Rocky Linux
Posts: 4,783

Rep: Reputation: 2214Reputation: 2214Reputation: 2214Reputation: 2214Reputation: 2214Reputation: 2214Reputation: 2214Reputation: 2214Reputation: 2214Reputation: 2214Reputation: 2214
If you want find not to include the command line argument itself in its outut, use the "-mindepth 1" option:
Code:
find -mindepth 1 -type d
That should give a list of all directories under the current working directory (the CWD is the implied argument in the absence of any other path arguments), but not list the CWD itself.
 
  


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
[SOLVED] GCC and G++ build results if the -march=TYPE is not specified Dweeb2010 Programming 7 05-25-2010 03:53 PM
[crontab] emailing non-empty results G00fy Programming 5 09-20-2006 01:16 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
apt-get install results : non empty dir | and strange site eeried Linux - Newbie 0 06-16-2004 03:34 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 12:55 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