LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Is it possible? (https://www.linuxquestions.org/questions/linux-newbie-8/is-it-possible-4175434204/)

karthilin 10-26-2012 09:22 AM

Is it possible?
 
I am a Linux user.I know little about grep commands.I want to search a string in one folder all files except one file.I used this command (grep -ir "string" folder path) but it checks all files in that folder.Can anyone tell the solution?



Thanks

malekmustaq 10-26-2012 11:43 AM

Quote:

Originally Posted by karthinila (Post 4815492)
I am a Linux user.I know little about grep commands.I want to search a string in one folder all files except one file.I used this command (grep -ir "string" folder path) but it checks all files in that folder.Can anyone tell the solution?



Thanks

Hi, yes you can.

Make a list (file) inside the folder where you run your grep(pings); example

Code:

~$ touch ./myexclude
then edit that file by writing therein the names of the file you do not want grep to read when it runs. You can glob the filenames if they are too many to identify for you but be careful grep has no wide taste on * wildcards. Then you can proceed to issue your command:

Code:

~$ grep -lir 'pattern' * --exclude-from=./myexclude
The return should exclude now the files you identified (listed) in the 'myexclude' file.

If you want to exclude a directory you can do the same with '--exclude-dir=DIRECTORY'.

Hope that helps.

Good luck.

m.m.

TobiSGD 10-26-2012 11:59 AM

If it is only one file that you want to exclude (or one specific pattern) you don't need to create a file, you can use the --exclude= option directly.
As always, more infos with
Code:

man grep

shivaa 10-26-2012 12:50 PM

You can take help from man pages using cmd "man grep".
Anyway, let you have following files:
ls -la <directory>
... A.txt
... B.txt
... C.txt
... D.txt
... E.txt
Then invoke following cmd to igonre C.txt file:
ls -la <directory> | grep -v "C.txt"
Use -v option with grep to ignore the string.

David the H. 10-28-2012 05:00 PM

Two more (bash-specific) options for you. They both involving globbing, and can be used with all commands.

1) Use extended globbing.

Code:

shopt -s extglob
grep 'pattern' !(unwantedfile.txt|*.sh)

2) Add the names or patterns that you want to exclude (colon-separated) to the GLOBIGNORE shell variable.

Code:

GLOBIGNORE=.*:unwantedfile.txt:*.sh
Note that when GLOBIGNORE is set to a non-null value, it also automatically enables DOTGLOB as well, so hidden files will expand too. You need to include the ".*" pattern if you want to exclude them as well. Also don't forget to unset GLOBIGNORE, or otherwise change it, when you're finished.


All times are GMT -5. The time now is 02:02 PM.