0

I want to exclude some files in find command and it is not working very good! Here is what I did:

arg=$1
exclude="/etc/alternatives/awk 
    /usr/sbin/poweroff 
    /usr/bin/ls"

find $arg -type f,l,c,b -path /etc/alternatives/awk -prune -o -print

In this example I used only one file /etc/alternatives/awk and I don't know how to tell the script to act in my variable exclude.

Update:

Should I only use this structure?

find $arg -type f,l,c,b -path /usr/bin/pwd -prune -o -path /etc/alternatives/awk -prune -o -path /usr/sbin/poweroff -prune -o -path /usr/bin/ls -prune -o -print
M.J
  • 175
  • 1
  • 7
  • Could [this thread](https://stackoverflow.com/questions/4210042/how-to-exclude-a-directory-in-find-command) help? – vgersh99 Nov 03 '20 at 13:22

1 Answers1

1

The posted thread had multiple alternatives with pros/cons. One of (the slowest):

#!/bin/bash
arg=$1
exclude='^/etc/alternatives/awk$|^/usr/sbin/poweroff$|^/usr/bin/ls$'

find $arg -type f,l,c,b | grep -Ev "${exclude}"

or you could try to do it natively with find without grep-ing:

#!/bin/bash
arg=$1
exclude='^(/etc/alternatives/awk|/usr/sbin/poweroff|/usr/bin/ls)$'

find $arg -type f,l,c,b -regextype posix-extended -not -regex "${exclude}" -print
vgersh99
  • 877
  • 1
  • 1
  • 8
  • 1
    Better to use this pattern: `exclude='^/etc/alternatives/awk$|^/usr/sbin/powerof$|^/usr/bin/ls$'` – M.J Nov 03 '20 at 14:33
  • 1
    `egrep` is deprecated in favor of `grep -E` but that grep command will interpret regexp metachars so YMMV if any of the paths contain `.` or `*` or `?` or `[` or `(` or similar - you should really be using `grep -F -f ''`. @M.J `^ab$|^cd$` = `^(ab|cd)$` – Ed Morton Nov 03 '20 at 15:11
  • Thank you. could you rewrite the script in better way you mentioned? Maybe even as a seperate answer – M.J Nov 03 '20 at 15:23
  • @Vgersh99 Yes, that's it! +1 – M.J Nov 03 '20 at 15:58