I wanted to execute a script that picks out a random directory path:

find / -type d | shuf -n1 

Unfortunately I get error messages about the prohibition of entering certain directories.

How can I exclude a directory from the search with find ?

3 Answers 3


To exclude specific paths, on Linux:

find / -path /sys -prune -o -path /proc -prune -o -type d

Another approach is to tell find not to recurse under different filesystems.

find / -xdev -type d

You could also use locate to query a database of file names (usually updated nightly, you could also update it manually using updatedb) instead of the live system.

locate '*' | shuf -n 1
  • great, short and insightful. find / -xdev -type d itself works, but find / -xdev -type d | shuf -n 1 displays error messages. Mar 29, 2015 at 23:32
  • 1
    @AbdulAlHazred you haven't said what messages but I'm guessing they're "permission denied" errors. Either run the command as root or just ignore the errors by sending them to stderr: find / -xdev -type d 2>/dev/null.
    – terdon
    Mar 29, 2015 at 23:42
  • 1
    Couldn't you just do find / \( -path /sys -o -path /proc \) -prune -o -type d Would be useful if you want to exclude more dirs like /dev or /tmp, etc.
    – BrainStone
    Jun 30, 2018 at 14:04

with GNU find you may also use regex options, e. g. like this:

find / -regextype posix-extended -regex "/(sys|srv|proc)" -prune -o -type d
  • 1
    that's the nicest approach, by far (rant: so there's a gnu option that makes find / usable on gnu linux like on any other OS that didn't have a shitty /proc implementation ;-) Sep 7, 2019 at 10:17

One method is to only include real filesystems.

Determine all mount points for real filesystems, and put them on one line:

$ realfs=$(df -x tmpfs -x devtmpfs | tail -n +2 | awk '{print $6;}' | xargs)
$ echo $realfs
/ /home /dos /Data 

Run find only against those mount points.

$ find ${realfs} -type d |& grep -v "Permission denied" | shuf -n1

(|& is a bashism added to 4.x -- works for me on 4.4.)

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .