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 ?
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
find / -xdev -type d 2>/dev/null.
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.
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
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
/Data/share/source/pan2/.git/refs/tags
(|& is a bashism added to 4.x -- works for me on 4.4.)