Friday, January 11, 2013

Find command - find all files excluding some matching one path

Objective: So to list all files in current directory but those under .git.

I am often using find in combination to grep to exclude some of them:

    find . -type f | grep -v .git
but today I needed to move the logic under the find expression. It's a bit confusing to go from one tool to another when regular expressions don't work exactly the same. I spent a while getting this right so here's my findings.

This won't work:

    find . -type f -and -not -path ".git" -print
It should be -path "./.git*"

In other words this will work

    find . -type d -and -name .git -prune -o -print
    find . -type d -and -path "./.git" -prune -o -type f -print
    find . -type f -and -not -path "./.git*" -print
Now I can list all extensions of our project files in one line
    find . -type f -name "*.*" -and -not \( -path "./.git*" -or -path "./Library*" \) -print0 | xargs -0 -L1 basename | sed 's/.*\.//' | sort -u
Useful before starting to update a .gitattributes file.