LinuxQuestions.org
Help answer threads with 0 replies.
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 05-28-2014, 09:42 PM   #1
battles
Member
 
Registered: Apr 2014
Distribution: Debian GNU/Linux 7.5 (wheezy)
Posts: 231

Rep: Reputation: 5
Delete files


I am trying to delete all files in a particular folder that contain a single word. I am not sure that I have the string correct. Not ready to try it as it is.

find /home/my/dir -type f -exec grep -l "searchword" {} \; | xargs /bin/rm -f
 
Old 05-29-2014, 12:20 AM   #2
potato_farmer
Member
 
Registered: May 2014
Posts: 55

Rep: Reputation: Disabled
You are searching for files that contain "searchword" in the filename or in the file itself?
 
Old 05-29-2014, 01:25 AM   #3
eklavya
Member
 
Registered: Mar 2013
Posts: 619

Rep: Reputation: 136Reputation: 136
It will delete all files of searched pattern in defined directory, it will not delete it recursively (inside sub-directories) as well as it does not delete the files which have same word with capitals.
Suppose you want to delete all files which have word events or Events or EVents or EVENts and files are in current directory as well as subdirectories, use -i and -r in your command with grep.

Try this!
Quote:
IFS=$'\n'
lst=$(grep -ilr "searchedword" /absolute/path/of/the/directory/* )
for i in $lst; do rm $i; done

Last edited by eklavya; 05-29-2014 at 01:35 AM.
 
Old 05-29-2014, 07:41 AM   #4
battles
Member
 
Registered: Apr 2014
Distribution: Debian GNU/Linux 7.5 (wheezy)
Posts: 231

Original Poster
Rep: Reputation: 5
Quote:
Originally Posted by potato_farmer View Post
You are searching for files that contain "searchword" in the filename or in the file itself?
Find "searchword" within the file itself. I will try eklavya method, it looks good.

Thanks all.
 
Old 05-29-2014, 07:45 AM   #5
battles
Member
 
Registered: Apr 2014
Distribution: Debian GNU/Linux 7.5 (wheezy)
Posts: 231

Original Poster
Rep: Reputation: 5
IFS=$'\n'
lst=$(grep -ilr "searchedword" /absolute/path/of/the/directory/* )
for i in $lst; do rm $i; done

What does IFS=$'\n' do?
 
Old 05-29-2014, 08:08 AM   #6
rtmistler
Moderator
 
Registered: Mar 2011
Location: Sutton, MA. USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu
Posts: 4,087
Blog Entries: 10

Rep: Reputation: 1521Reputation: 1521Reputation: 1521Reputation: 1521Reputation: 1521Reputation: 1521Reputation: 1521Reputation: 1521Reputation: 1521Reputation: 1521Reputation: 1521
Here's how I do that; however FIRST I ensure that the find works the way intended:
Code:
find . -type f -name "*pattern*"
And once validated that it finds the correct files, I then use exec:
Code:
find . -type f -name "*pattern*" -exec rm -f {} \;
Note: All between "-exec" and "\;" should be a valid shell command sequence.
 
Old 05-29-2014, 10:11 AM   #7
eklavya
Member
 
Registered: Mar 2013
Posts: 619

Rep: Reputation: 136Reputation: 136
Quote:
Originally Posted by battles View Post
IFS=$'\n'
lst=$(grep -ilr "searchedword" /absolute/path/of/the/directory/* )
for i in $lst; do rm $i; done

What does IFS=$'\n' do?
IFS is Internal field separator.
If there are spaces in filename, '$' tells at the end of the line and \n separates field when new line is appeared because default field separator is 'space'.
 
Old 05-29-2014, 10:13 AM   #8
eklavya
Member
 
Registered: Mar 2013
Posts: 619

Rep: Reputation: 136Reputation: 136
Quote:
Originally Posted by rtmistler View Post
Here's how I do that; however FIRST I ensure that the find works the way intended:
Code:
find . -type f -name "*pattern*"
And once validated that it finds the correct files, I then use exec:
Code:
find . -type f -name "*pattern*" -exec rm -f {} \;
Note: All between "-exec" and "\;" should be a valid shell command sequence.
I think it will find pattern in filename and OP is talking about the text pattern inside file.
 
Old 05-29-2014, 01:35 PM   #9
rtmistler
Moderator
 
Registered: Mar 2011
Location: Sutton, MA. USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu
Posts: 4,087
Blog Entries: 10

Rep: Reputation: 1521Reputation: 1521Reputation: 1521Reputation: 1521Reputation: 1521Reputation: 1521Reputation: 1521Reputation: 1521Reputation: 1521Reputation: 1521Reputation: 1521
Quote:
Originally Posted by eklavya View Post
I think it will find pattern in filename and OP is talking about the text pattern inside file.
Thanks. Sorry, that does make sense. Looks like they got to the right solution from your posts.
 
  


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
[SOLVED] Delete regular files (not hidden files) with find + rm in one line older than 15 Virtuose Linux - Newbie 1 01-08-2011 06:24 PM
need to rsync only selected files (--files-from) also need to delete files on dest. ? BrianK Linux - General 5 10-22-2009 10:52 PM
Can I delete files in /mnt/tmp? and Files in the trash can will not delete? M$ISBS Slackware 15 10-03-2009 12:56 AM
Script help - delete files older than 45 days but exclude the system files jojothedogboy Linux - Software 3 06-13-2008 04:43 PM
How to delete files that won't delete? di11rod Linux - Security 7 10-19-2005 10:14 PM


All times are GMT -5. The time now is 10:21 AM.

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