Title: Remove "." from find results
Author: Sandro Tosi
Last modified: 2006-11-18
A typical use of ``find'' is to return the list of top-directory in
the current one. The command to obtain this is:
$ find -type d -maxdepth 1
(``-maxdepth'' reduce breadth search to the nth level of directory). A
sample output is:
.
./a_dir1
./a_dir2
./a_dir3
<etc...>
Sadly, that "." as the first result could cause some strange behavior
you in a script that uses that command. In the man-page for ``find''
the option ``-noleaf'' seemed interesting:
$ find -type d -maxdepth 1 -noleaf
.
./a_dir1
./a_dir2
./a_dir3
<etc...>
but it returns the same output, so it does not solve our task.
``find'' allows to write a sort of boolean expressions, to output only
result that match the whole expression. So, writing the command this
way:
$ find -type d -not -name "." -maxdepth 1
./a_dir1
./a_dir2
./a_dir3
<etc...>
you are able to remove the first result, "." .
Moreover, you may would like to remove the leading "./" from every
result: ``sed'' will help us do this:
$ find -type d -not -name "." -maxdepth 1 | sed 's/^\.\///g'
a_dir1
a_dir2
a_dir3
<etc...>
(``sed'' search for "^\.\/", so the exact char "." followed by the
exact char "/" at the beginning of the line, "^", and replace it with
the string between "//", so the empty string). |