You are not logged in.

#1 2012-10-01 14:44:01

From: Baltimore, MD (US)
Registered: 2009-04-16
Posts: 987

[SOLVED] Proper syntax for "find everything but rc files"

So as we all know, some applications put rc files in the home directory, preceded by a dot, like .bashrc or .vimrc, and some applications (also) have their own "dot-directory" under which they store multiple files, like .minecraft or .mozilla. I want a find command that skips those files but lists all the other files under my home directory, but the apparently obvious solution isn't working.

Here's an example directory layout:

% tree -a
|-- .foorc
|-- .rcfiles
|   |-- barrc
|   `-- bazrc
|-- birds
|   |-- chickadee
|   |-- goldfinch
|   `-- nuthatch
`-- trees
    |-- birch
    |-- chestnut
    |-- oak
    `-- poplar

3 directories, 10 files

I could easily find all the files and folders that contain the letter 'i' in their names:

% find . -name '*i*'

That listing includes two directories, ./birds and ./rcfiles. I could omit all these files, and all the files under those two directories, by using -prune:

% find . -name '*i*' -prune -o -print

That command lists only the files that do not contain an 'i', and are not nested inside any folder that contains an 'i'. Now let's try with dotfiles. Finding all the dotfiles and dot-folders is easy enough:

% find . -name '.*'

But when I use the same syntax as above to [1] omit those files and contents of those directories, I don't get any output at all. I was just getting around to asking why not, but figured it out. Keep reading if interested, I might as well finish this since I've put so much work into it so far.

[1] This is the exact point where I realized my mistake. `-name '.*'` matches the current directory (.) and prunes the whole tree, so find never looks at any of the files. To avoid matching ., you could use -mindepth or ensure the length of the name is longer than one character with the ? metacharacter:

% find . -mindepth 1 -name '.*' -prune -o -print
% find . -name '.?*' -prune -o -print

You could also run find on non-dotted files, with the same output but none preceded by ./:

% find * -name '.*' -prune -o -print

(For my example, this does the same thing as `find *`, but in my actual home directory I have a lot of deeply nested .git directories that would unnecessarily clutter the output.)

Hope this helps somebody who was experiencing the same problem.


#2 2012-10-01 14:46:33

Registered: 2009-05-06
Posts: 25,440

Re: [SOLVED] Proper syntax for "find everything but rc files"

ack and similar apps help with .git etc:


Board footer

Powered by FluxBB