Tip for find: List all directory in current path without search sub-directory (ls,find只列出当前目录下的所有目录,不检索子目录)

Solaris find (ls) tip: List all directory in current path without search sub-directory

Last week, one of my colleague ask me how to list all directories which only in current path. I made a litter research, and have learned these find tips.

  •  List current directory name without slash "/"

[myPC]$find * -prune  -type d

dir1

dir2

  • List current directory name with slash "/"

[myPC]$ls -F|grep ‘/’

dir1/

dir2/

  • List current directory name in long format with slash "/"

[myPC]$ls -l |grep ^d

 Sometimes when you maybe need list all files in current path without search sub-directory

[myPC]$find .  \( -type d ! -name . -prune \) -o \( -type f -print \) 

 or use:

[myPC]$find * -prune  -type f

by using -type you can list any type file which you want. It can be followed :b,  c,  d, D, f, l, p, or s for block special file,  character  special  file,   directory, door,  plain file, symbolic link, fifo (named pipe), or socket, respectively. For more details, please see find man page.

2 thoughts on “Tip for find: List all directory in current path without search sub-directory (ls,find只列出当前目录下的所有目录,不检索子目录)

  1. All of your ls|grep examples suffer from the usual problem, they fail if there is a \n in one of the directory names. That is ok when you look at a directory by hand, but it should never be used in a script.

发表评论

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / 更改 )

Twitter picture

You are commenting using your Twitter account. Log Out / 更改 )

Facebook photo

You are commenting using your Facebook account. Log Out / 更改 )

Google+ photo

You are commenting using your Google+ account. Log Out / 更改 )

Connecting to %s