Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's this is the place! |
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.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
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.
|
 |
03-14-2017, 12:24 PM
|
#1
|
Member
Registered: Nov 2015
Posts: 397
Rep: 
|
using find to display ALL directories
From man find:
Quote:
-name pattern
...
The metacharacters (`*', `?', and `[]')
match a `.' at the start of the base
name (this is a change in findutils-4.2.2;
see section STANDARDS CONFORMANCE below).
...
STANDARDS CONFORMANCE
...
-name This option is supported, but
POSIX conformance depends on the
POSIX conformance of the system's
fnmatch(3) library function.
As of findutils-4.2.2, shell
metacharacters (`*', `?' or `[]' for example)
will match a leading `.', because
IEEE PASC interpretation 126 requires this.
This is a change from previous
versions of findutils.
...
|
So it appears you can use metacharacters wildcard
for -name option.
I am trying to get the equivalent of
these combined commands
of listing ALL directories in
current directories using
find commmand:
Code:
> ls -dl */
> ls -dl .*/
BTW, can you use ls to display both
hidden and visible directories ONLY
(excluding files) in a single command?
Here is the find command I tried with no success:
Code:
find . -maxdepths 0 -type d -name *
Which gives an error
"find: paths must precede expression: ..... "
Why does -name option not work?
Thank you.
|
|
|
03-14-2017, 12:31 PM
|
#2
|
Senior Member
Registered: Aug 2006
Location: Detroit, MI
Distribution: GNU/Linux systemd
Posts: 4,278
|
If you want to list all directories in the current directory using the command and parameters you have mentioned in your post then:
Code:
find . -maxdepth 1 -type d
Should get you there. That will list all directory entries regardless of their name.
|
|
2 members found this post helpful.
|
03-14-2017, 12:39 PM
|
#3
|
Member
Registered: Nov 2015
Posts: 397
Original Poster
Rep: 
|
Quote:
Originally Posted by szboardstretcher
If you want to list all directories in the current directory using the command and parameters you have mentioned in your post then:
Code:
find . -maxdepth 1 -type d
Should get you there. That will list all directory entries regardless of their name.
|
Thank you.
I obviously misunderstood -maxdepth description in man page.
I still did not understand why option, -name *,
produces an error.
Last edited by fanoflq; 03-14-2017 at 12:42 PM.
|
|
|
03-14-2017, 01:11 PM
|
#4
|
LQ Guru
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,756
|
Quote:
Originally Posted by fanoflq
Thank you.
I obviously misunderstood -maxdepth description in man page.
I still did not understand why option, -name *,
produces an error.
|
The asterisk * is not quote or escaped, thus it is exposed raw to the shell which interprets it for you before passing it to find. See it in the context of a for loop using your default shell (which is probably bash)
Code:
for i in *;do echo "$i"; done
for i in '*';do echo "$i"; done
|
|
3 members found this post helpful.
|
03-14-2017, 01:16 PM
|
#5
|
Member
Registered: Nov 2015
Posts: 397
Original Poster
Rep: 
|
Quote:
Originally Posted by Turbocapitalist
The asterisk * is not quote or escaped, thus it is exposed raw to the shell which interprets it for you before passing it to find. See it in the context of a for loop using your default shell (which is probably bash)
Code:
for i in *;do echo "$i"; done
for i in '*';do echo "$i"; done
|
Thank you.
This works now.
Code:
find . -type d -name "*"
OR
find . -type d -name '*'
Reminder to self:
Add quotes if you want to prevent current shell
from globbing arguments to command or its options.
Last edited by fanoflq; 03-14-2017 at 01:19 PM.
|
|
1 members found this post helpful.
|
03-14-2017, 02:16 PM
|
#6
|
LQ Guru
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 5,573
|
Quote:
Originally Posted by fanoflq
I am trying to get the equivalent of
these combined commands
of listing ALL directories in
current directories using
find commmand:
Code:
> ls -dl */
> ls -dl .*/
BTW, can you use ls to display both
hidden and visible directories ONLY
(excluding files) in a single command?
|
or
Code:
shopt -s dotglob
ls -dl */
|
|
3 members found this post helpful.
|
03-14-2017, 02:54 PM
|
#7
|
Member
Registered: Nov 2015
Posts: 397
Original Poster
Rep: 
|
Quote:
Originally Posted by suicidaleggroll
or
Code:
shopt -s dotglob
ls -dl */
|
Thanks.
Now I will have to read up on brace expansion!
|
|
|
03-16-2017, 01:17 PM
|
#8
|
Member
Registered: Feb 2012
Location: Buenos Aires, Argentina
Distribution: Debian, Suse, Mandrake,
Posts: 92
Rep: 
|
Hi. fanoflq answer is a good method.
By the way, you can use it to find directories with pattern name:
find . -type d -name 'pattern*pattern2'
or must be to dont show directories with a pattern name:
find . -type d -prune 'pattern1*pattern2'
Have a nice day.
|
|
|
All times are GMT -5. The time now is 12:36 PM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|