LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   using find to display ALL directories (https://www.linuxquestions.org/questions/linux-newbie-8/using-find-to-display-all-directories-4175601753/)

fanoflq 03-14-2017 12:24 PM

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.

szboardstretcher 03-14-2017 12:31 PM

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.

fanoflq 03-14-2017 12:39 PM

Quote:

Originally Posted by szboardstretcher (Post 5683364)
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.

Turbocapitalist 03-14-2017 01:11 PM

Quote:

Originally Posted by fanoflq (Post 5683369)
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


fanoflq 03-14-2017 01:16 PM

Quote:

Originally Posted by Turbocapitalist (Post 5683383)
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.

suicidaleggroll 03-14-2017 02:16 PM

Quote:

Originally Posted by fanoflq (Post 5683361)
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 */


fanoflq 03-14-2017 02:54 PM

Quote:

Originally Posted by suicidaleggroll (Post 5683418)
Code:

ls -dl {,.}*/
or
Code:

shopt -s dotglob
ls -dl */


Thanks.

Now I will have to read up on brace expansion!

cesarbergara 03-16-2017 01:17 PM

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 09:44 AM.