LinuxQuestions.org
Register a domain and help support LQ
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 06-20-2008, 03:00 AM   #1
linuq
LQ Newbie
 
Registered: Jun 2008
Posts: 10

Rep: Reputation: 0
How to delete files tat contains a specific word??


Hi All,

I am new to linux.
I have a folder tat contains a lot of text files (more than 1000).
Some of these files contains similar word example: "error"
I need to delete these files.
My question is, is there a command/script that i can use to perform this?

Any help is appreciated.

Last edited by linuq; 06-20-2008 at 03:08 AM.
 
Old 06-20-2008, 03:22 AM   #2
nx5000
Senior Member
 
Registered: Sep 2005
Location: Out
Posts: 3,307

Rep: Reputation: 52
Code:
grep -lw error * | xargs rm
 
Old 06-20-2008, 03:28 AM   #3
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947
Be careful with that command though. It will delete everything with the word "error" in it, no questions asked. You should first run the grep alone ("grep -lw error *") to list the files it's going to affect before you delete them.
 
Old 06-29-2008, 11:26 PM   #4
linuq
LQ Newbie
 
Registered: Jun 2008
Posts: 10

Original Poster
Rep: Reputation: 0
Thx for the info.
But when i run on folder with 15000 files i get 'Argument list too long' error.
Any idea why?
 
Old 06-30-2008, 12:12 AM   #5
chrism01
Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.5, Centos 5.10
Posts: 16,261

Rep: Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028
That's because the shell expands the '*' to 15,000 args before grep sees it.
Try a loop instead:
Code:
for file in `ls`
do
    grep -lw error $file>dev/null
    if [[ $? -eq 0 ]]
    then
        #matched
        echo $file
        #rm $file
    fi
done
don't activate the 'rm' until you are sure you are happy with the file matching
 
  


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


Similar Threads
Thread Thread Starter Forum Replies Last Post
Delete/move/copy files of specific date imsajjadali Red Hat 26 11-07-2013 11:34 PM
how can delete certain word from file Barq Linux - General 11 01-06-2008 07:06 PM
ow do I delete all files which were created on a specific date marsguy Linux - General 9 08-21-2007 08:15 AM
deleting files containing a specific word Sleb Programming 5 05-25-2006 01:19 PM
Domain and specific 'word' blocking in firewall2 hiplainsdrifter Linux - Newbie 5 04-07-2004 03:54 PM


All times are GMT -5. The time now is 06:33 AM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration