1

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.

sandric
  • 2,310
  • 3
  • 21
  • 26
  • Is the missing whitespace between the `'sys'` and the closing paren real, or an artifact of your post? – Charles Duffy Jun 09 '15 at 19:52
  • Also, "pathfinding" is completely unrelated to filesystem paths; it refers to artificial intelligence algorithms. Whereas the "find" tag is overloaded, it's actually better than "pathfinding" for this query. – Charles Duffy Jun 09 '15 at 19:53
  • BTW, since you're writing a function, why not put conditional logic in the function itself? – Charles Duffy Jun 09 '15 at 19:57
  • `...function that will find files in directory I'm currently in` => are there any limits for maxdepth? – Eugeniu Rosca Jun 09 '15 at 19:59
  • I'm assuming that by "in" the OP meant "under", though indeed these aren't the same thing. – Charles Duffy Jun 09 '15 at 20:00
  • @CharlesDuffy yes, I missed space before closing parenthesis, now its fixed. Sorry if I was wrong with tag, thx for fixing. I'm not putting condition into function logic because I find its ugly and to be fair - its not what I'm asking in the first place in this question. – sandric Jun 09 '15 at 20:00
  • You tagged your question "bash", initially. Putting logic into a shell function is, well, bash. Otherwise, this is pure find, with no shell involvement. :) – Charles Duffy Jun 09 '15 at 20:05

2 Answers2

3

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.

that other guy
  • 116,971
  • 11
  • 170
  • 194
0

-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.

Charles Duffy
  • 280,126
  • 43
  • 390
  • 441