Tomas Lapsansky
Tomas Lapsansky

Reputation: 95

Ignoring files from find utility using grep -v

I have to sort files in my directory and subdirs by file utility and make diagram of 10 most frequent file types. It runs by

bash myfile -i [FILE_ERE]

so the FILE_ERE should be regex for every file and directory that should be ignored in my diagram. For example, when find out is

testdirectory/hello1
testdirectory/hello2
testdirectory/newdir/hello3
hello4

and my $FILE_ERE from arguments is "dir", it should ignore every file with "dir" and the output is

hello4

I have this

input_name=$(file -b `find $DIR -type f | grep -v "$FILE_ERE"` | sort | uniq -c | sort -n -r | head | sed 's/^[ \t]*//' | cut -d' ' -f 2-)
input_number=$(file -b `find $DIR -type f | grep -v "$FILE_ERE"` | sort | uniq -c | sort -n -r | head | sed 's/^[ \t]*//' | cut -d' ' -f1)

But my regex isn't working correctly for some inputs, like FILE_ERE="ˆh" etc.

Upvotes: 0

Views: 117

Answers (1)

jraynal
jraynal

Reputation: 517

One solution is to use find directly to ignore your pattern:

Using globing expresssion: FILE_ERE="h*" (all files starting with h)

find "${DIR}" -type f ! -name "${FILE_ERE}" -exec file -b {} +

Using regex: FILE_ERE="^./h.$" (all files starting with h):

find "${DIR}" -type f ! -regex "${FILE_ERE}" -exec file -b {} +

Note that it is usually better to use -exec to process find output.

EDIT: Thanks @chepner for the suggestions.

NOTE: The regex much match the all file path, so to match all the files starting with 9, you need to use the regex ^.*/9.*. You can also use -name "9*". This requires a globing expression, not a regex.

Upvotes: 1

Related Questions