Unsure about results of find -type d on empty directory
Linux - SoftwareThis 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
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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?
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.
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?
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.
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.