LinuxQuestions.org
Latest LQ Deal: Complete CCNA, CCNP & Red Hat Certification Training Bundle
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 10-26-2012, 10:22 AM   #1
karthilin
LQ Newbie
 
Registered: Sep 2012
Location: Chennai
Posts: 13

Rep: Reputation: Disabled
Question 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
 
Old 10-26-2012, 12:43 PM   #2
malekmustaq
Senior Member
 
Registered: Dec 2008
Location: root
Distribution: Slackware & BSD
Posts: 1,613

Rep: Reputation: 433Reputation: 433Reputation: 433Reputation: 433Reputation: 433
Quote:
Originally Posted by karthinila View Post
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.
 
Old 10-26-2012, 12:59 PM   #3
TobiSGD
Moderator
 
Registered: Dec 2009
Location: Germany
Distribution: Whatever fits the task best
Posts: 17,130
Blog Entries: 2

Rep: Reputation: 4825Reputation: 4825Reputation: 4825Reputation: 4825Reputation: 4825Reputation: 4825Reputation: 4825Reputation: 4825Reputation: 4825Reputation: 4825Reputation: 4825
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
 
Old 10-26-2012, 01:50 PM   #4
shivaa
Senior Member
 
Registered: Jul 2012
Location: Grenoble, Fr.
Distribution: Sun Solaris, RHEL, Ubuntu, Debian 6.0
Posts: 1,800
Blog Entries: 4

Rep: Reputation: 286Reputation: 286Reputation: 286
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.
 
Old 10-28-2012, 06:00 PM   #5
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957
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.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off




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

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration