Shell script for search in BTRFS filesystem (exclude snapshots)

One thing that bothers me about BTRFS is when I’m going to search for a file… So many hits I get because the file exists in so many snapshots…

I’m trying to make a shell program to search files and not include hits when files also exist inside snapshots folders, such as, /home/.snapshots and /.snapshots.

I finally got one working which one is this:

read -p "Search for: " argument
sudo find / \( -path "/.snapshots" -o -path "/mnt" -o -path "/home/.snapshots" -o -path "/proc" -o -path "/run" -o -path "/sys" -o -path "/usr/share/polkit-1/rules.d" -o -path "/tmp" -o -path "/root" \) -prune -o -name $argument -print

But this is kind of a mess, so my question is, is there a better way to accomplish this?


Don’t use path.

Try this:

find . -type d -name '.snapshots' -prune -o -name ${argument}

Although, to be honest, since the snapshots are generally only readable by root unless you are using sudo with your find command they should not be found anyway.


Thanks Dalto, but if I don’t use sudo, I get a lot of permissions denied, so sudo is just to get a clean output for the search.
This is working, changed the . to / and it is working the way I need. Thanks!

Did a test using resolv.con* (using sudo)

Search for: resolv.con*
find: ‘/run/user/1000/gvfs’: Permissão negada
1 Like

If you add -print to the end it will stop the .snapshots output from being reported.

1 Like

If I want to exclude /mnt directory as well, is it doable using this method?

Oh, good, will add it

Yes, although it will start to get longer again.

find / \( -type d -name '.snapshots' -o -path "/mnt" \) -prune -o -name ${argument} -print
1 Like

That is perfect, really thanks Dalto.
Search is so clean and fast now… :slight_smile:

1 Like

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.