2

I would like to know if there's any tool in bash that can exclude from find searching directories that match certain regex patterns

There's an example:

  • ./dokuments
  • ./dokuments/1.txt
  • ./dokuments/files/2.txt
  • ./documentsz/message.doc

When i run my script (that basicly explores the directories and creates a report on its contents) with this regex "ents$" for example - the result should be only message.doc which is in ./documentsz and not those other files.

I've seen many solution as find . -not -name, -regextype posix-egrep -regex, but it does not work for me, because i don't know the regex in advance, i don't know what name of directory(that match regex) will be used.

Arey Jeremy
  • 67
  • 1
  • 8
  • Possible duplicate of [Exclude directory from find . command](http://stackoverflow.com/questions/4210042/exclude-directory-from-find-command) – Arturo Herrero Mar 26 '16 at 12:06
  • Yeah i've read that, but it does not work for me, because i don't know what regex will be used. i don't even know the specific name of excluded directory in advance – Arey Jeremy Mar 26 '16 at 12:36
  • Please clarify your specific problem or add additional details to highlight exactly what you need. – Cyrus Mar 26 '16 at 12:40
  • Well, i need to make a script which should work as a dirstat, that means that i run the script with two parameters: the path and the regex that matches files and directories which are supposed to be ignored from the searching. – Arey Jeremy Mar 26 '16 at 12:56
  • Do you mean something like `find $1 -type f | grep -v "$2"` ? – Walter A Mar 26 '16 at 13:15
  • Yeah exactly, that's how i work with files, but when it comes to directories it is worse, because of the find output. https://ctrlv.cz/z48Q – Arey Jeremy Mar 26 '16 at 13:44

1 Answers1

0

I'm not sure what you expect your ctrlv.cz paste should output. The regex is the following:

Output anything that doesn't contain enty at the end of the string.

Do you want exclude every file that is in an directory ending in enry?

find . -type f | grep -v 'enry/'

If that's the case then you can use find all alone:

find . -not \( -path "*enry" -prune \) -o -type f  

If you want a dynamic regex and want it to match each directory/file instead of the full path, then you can use: (I'm not suggesting to use this in production code)

my_script.sh

#!/usr/bin/env bash
find -type f -print0 | perl -na0 -F/ -e 'print "$_\n" unless grep { /'"$1"'/ } @F'

Or with AWK:

find -type f | awk -F/ '{for(i=1;i<=NR;i++)if ($i ~ /'"$1"'/) next}1'

Usage:

$ bash my_script.sh "enry$"
./dirstat.sh
./dirstat.sh.save
...
./pokus/pokus
./a.out
Andreas Louv
  • 46,145
  • 13
  • 104
  • 123
  • As i said up there, i don't know the regex in advance, it should be working with every regex so when user puts as regex '^doku' it should ignore every file and directory(and files in it) that match this regex and print everythig else in dir that is being explored, When user puts as regex '*dok*' it should work the same way – Arey Jeremy Mar 26 '16 at 17:13
  • It seems to be working, thank you for your answers, bu i can't use it, because there would have been a problem with portability, (and it's kind of complex, at least for me) anyway it would be fine to know what the individual components of the command do – Arey Jeremy Mar 26 '16 at 18:54
  • Basically I split each line on slashes and try to match the regex on each part of the line – Andreas Louv Mar 26 '16 at 19:00
  • This is definitely better, if I could buy you a beer, I'll do it – Arey Jeremy Mar 26 '16 at 19:15
  • But i've got another little problem, when i am using your code it works perfectly out of my script, but when i use it in my script it doesn't .. don't you know how is that possible ? find $DIR -type f | awk -F/ '{for(k=1;k<=NR;k++)if ($k ~ /'"$FILE_ERE"'/) next}1' | grep -E "*.txt" | wc -l It seems like it does not take the value of variable FILE_ERE (which is regex) in my script – Arey Jeremy Mar 27 '16 at 11:04
  • even if Iput there the right value ($k ~ /'"der2$"'/) instead of the variable, it does not work, but out of the scrip it works fine – Arey Jeremy Mar 27 '16 at 11:11
  • @AreyJeremy Remember to quote `$DIR`: `find "$DIR" -type f | ...` – Andreas Louv Mar 27 '16 at 18:18
  • I want to save the result of the command output to the variable like this VAR="$(find . -type f -name "*.txt" |awk -F/ '{for(k=1;k<=NR;k++)if ($k ~ /'"$FILE_ERE"'/) next}1' | wc -l )" , maybe there is something wrong why it is not working – Arey Jeremy Mar 27 '16 at 21:42
  • Well I've just noticed that problem is in find . when i use find "dot" in my script it works, but when i use find /home/ev/ ... or $DIR or "$DIR" .. it's problem, any idea ? – Arey Jeremy Mar 27 '16 at 22:33