LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
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


Reply
  Search this Thread
Old 03-14-2017, 01:24 PM   #1
fanoflq
Member
 
Registered: Nov 2015
Posts: 397

Rep: Reputation: Disabled
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.
 
Old 03-14-2017, 01:31 PM   #2
szboardstretcher
Senior Member
 
Registered: Aug 2006
Location: Detroit, MI
Distribution: GNU/Linux systemd
Posts: 4,237

Rep: Reputation: 1653Reputation: 1653Reputation: 1653Reputation: 1653Reputation: 1653Reputation: 1653Reputation: 1653Reputation: 1653Reputation: 1653Reputation: 1653Reputation: 1653
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.
Old 03-14-2017, 01:39 PM   #3
fanoflq
Member
 
Registered: Nov 2015
Posts: 397

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by szboardstretcher View Post
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 01:42 PM.
 
Old 03-14-2017, 02:11 PM   #4
Turbocapitalist
Senior Member
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 4,428
Blog Entries: 3

Rep: Reputation: 2205Reputation: 2205Reputation: 2205Reputation: 2205Reputation: 2205Reputation: 2205Reputation: 2205Reputation: 2205Reputation: 2205Reputation: 2205Reputation: 2205
Quote:
Originally Posted by fanoflq View Post
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.
Old 03-14-2017, 02:16 PM   #5
fanoflq
Member
 
Registered: Nov 2015
Posts: 397

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by Turbocapitalist View Post
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 02:19 PM.
 
1 members found this post helpful.
Old 03-14-2017, 03:16 PM   #6
suicidaleggroll
LQ Guru
 
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 5,573

Rep: Reputation: 2137Reputation: 2137Reputation: 2137Reputation: 2137Reputation: 2137Reputation: 2137Reputation: 2137Reputation: 2137Reputation: 2137Reputation: 2137Reputation: 2137
Quote:
Originally Posted by fanoflq View Post
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?
Code:
ls -dl {,.}*/
or
Code:
shopt -s dotglob
ls -dl */
 
3 members found this post helpful.
Old 03-14-2017, 03:54 PM   #7
fanoflq
Member
 
Registered: Nov 2015
Posts: 397

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by suicidaleggroll View Post
Code:
ls -dl {,.}*/
or
Code:
shopt -s dotglob
ls -dl */
Thanks.

Now I will have to read up on brace expansion!
 
Old 03-16-2017, 02:17 PM   #8
cesarbergara
Member
 
Registered: Feb 2012
Location: Buenos Aires, Argentina
Distribution: Debian, Suse, Mandrake,
Posts: 73

Rep: Reputation: Disabled
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.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
Find the directories which is older than x days delete and zip those directories ramesh pagadala Linux - Newbie 2 08-29-2013 09:17 AM
How can I find all directories with subdirectories with find? Ujjain Linux - Software 1 06-11-2011 06:57 PM
[SOLVED] Find a file in directories without using find command sikanders Linux - Newbie 14 08-06-2010 09:47 PM
directories first in ls display? gjhicks Linux - Newbie 9 07-15-2009 01:51 PM
I want ls to not display contents of directories GameboyHippo Linux - Newbie 2 01-26-2004 04:47 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 07:04 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration