Removing leading dots from find command output when used with -exec echo {} option

Use * instead of . and the leading ./ disappears.

find * -type f

Assuming that you don't want the filename alone, but also the full relative path without the leading ./, if you have GNU find you can try this:

find . -type f -printf '%P\n'

Else, you can try one of these:

find . -type f -print | cut -d/ -f2-

find .[^.]* * -type f -print

You should avoid this last one (think of the excessively long command line if you have a lot of entries in your directory).

-print does the same thing as your -exec echo {} \; but is much better: no external process call so lower overhead and no undesirable side-effects with filenames beginning with a dash.


find . -type f -exec echo {} \;

Default action of find is to print results. When not explicitly told, find defaults to searching current directory. Your command can be simplified to find -type f.

Why/when does this matter? Only when you run this command on a sufficiently large directory, you will start to see the performance difference. -exec echo {} \; will make your computer work needlessly more because it has to start an external process like /bin/echo to print the filenames. One instance of echo executable is run per file found by find.

To answer your question about removing ./, a more efficient method would be to use cut. You know that the first two characters are always ./. cut -c3- will only retain characters from position 3 and beyond.

> cd /etc/fonts/conf.d
> find -type f | cut -c3-
99pdftoopvp.conf
00kde.conf
65-khmer.conf
README

This fails to work as expected if your filenames contain new line character, but that is another whole new story.

Tags:

Find