0

I am searching for certain files with certain extensions using find command. I would like to use grep command at the end to ignore certain files.

My uncomplete command is:

find . -type f \( -name "*.txt" -or -name "*.html" -or -name "*.css" -or -name "*.js" -or -name "*.yml" \) | grep .. 

Let's say I would like to ignore 2 files: cards.js and radios-and-checkboxes.css

How can I implement that with grep command? I am aware that there are ways without using a grep command but I would like to know how can I do it using grep.

I have looked for different solutions but most of them mentions excluding words or file extensions or using just find command to do a similar task.

0

5 Answers 5

1

You can use grep's -v flag to achieve this. In order to exclude one such item from results:

your commands here | grep -v "cards.js" 

And if you want to chain multiple grep matches, do this:

yourcommands here | grep -v -e "cards.js" -e "radios-and-checkboxes.css"` 

Please use the -w if you want EXACT match with the strings in grep. So for an exact match with "cards.js" use: grep -v -w -e "cards.js". Using the -w once will work for multiple extends.

Sign up to request clarification or add additional context in comments.

7 Comments

since . is used this will match cardsajs as well
@oguzismail as I use find command to find only .txt, .html, .css, .js and .yml there is no risk for cardsajs to be matched as well, is there?
@Marcin yeah. it will match cardajs.js as well
@Marcin it wouldn't match filenames containing spaces
and no, you need to specify -w just once. like grep -v -w -e file1 -e file2
|
1

You are asking for grep -v

 $ echo -e "a\nb\nc" | grep -v b 

This prints everything excluding b

Use "-e" to exclude multiple matches,

 $ echo -e "a\nb\nc" | grep -v -e a -e b 

This excludes both a and b

Comments

1

Use -E (extended) and -v (invert match) in grep:

find ... | grep -v -E 'cards\.js|radios-and-checkboxes\.css' 

The | character is the OR operator.

2 Comments

Nice explanation on the flags
since . is used this will match cardsajs as well
0

Pure find solution without grep :

find . -type f \( \( -name "*.txt" -o -name "*.html" -o -name "*.css" -o -name "*.js" -o -name "*.yml" \) -a ! -name "radios-and-checkboxes.css" -a ! -name "cards.js" \) 

2 Comments

OP says I am aware that there are ways without using a grep command but I would like to know how can I do it using grep.
Correct, I overlooked that.
0

This script lists files excluding a list of extensions. It helps when trying to spot garbage files. Common extensions are used in the example. It could be more compact, but less readable.

#!/bin/bash EXTENSIONS=$(cat <<'EOE' .exe .py .js .css .html EOE ) # extensions, 1 per line EXTENSIONS=$(echo "$EXTENSIONS"|tr "\n" "|") # convert to egrep pattern EXTENSIONS=${EXTENSIONS%?} # remove trailing '|' find . -type f | egrep -i -v "$EXTENSIONS" # all files lacking EXTENSIONS 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.