how to ignore directory in find by full path

317 views Asked by At

I want to write a function that will find files in current directory I'm currently in. The thing is that if I'm in /home, I want to find files in sys, dev and var directories (which is in my home folder). But If I'm in root folder I wish them to be stripped. I tried this:

find -L . \( -path '/dev' -o -path '/var' -o -path '/sys' \) -prune \
  -o -type f -print \
  -o -type d -print \
  -o -type l -print

But its not working. If I set a dot (.) at beginning of every path I want to exclude - it works for root folder, but also excludes such dirs in other directories, which I do not want.

Is there a way to prune by full (global) file path? I tried a -wholename option, but It seems not work for me.

2

There are 2 answers

0
that other guy On BEST ANSWER

You can use find "$PWD", as in find "$PWD" -path '/dev' -prune -o -print, to search in the current directory by absolute path. Absolute path matches will then succeed.

0
Charles Duffy On

-path doesn't consider /dev and ./dev to be the same, even when running in /, because find performs string comparisons on names when using -path -- it doesn't look up inode numbers for comparison or perform normalization.

Now, if you do want to do an inode-number-based lookup, you can do that.

Consider:

# this relies on GNU stat for --format
listtree() {
  find -L . \( -inode "$(stat --format=%i /dev)" \
            -o -inode "$(stat --format=%i /var)" \
            -o -inode "$(stat --format=%i /sys) \) -prune \
    -o -type f -print \
    -o -type d -print \
    -o -type l -print
}

However, be aware that inode numbers can overlap between different filesystems.