LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Delete files (https://www.linuxquestions.org/questions/linux-newbie-8/delete-files-4175506338/)

battles 05-28-2014 08:42 PM

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

potato_farmer 05-28-2014 11:20 PM

You are searching for files that contain "searchword" in the filename or in the file itself?

eklavya 05-29-2014 12:25 AM

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

battles 05-29-2014 06:41 AM

Quote:

Originally Posted by potato_farmer (Post 5178465)
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.

battles 05-29-2014 06:45 AM

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

What does IFS=$'\n' do?

rtmistler 05-29-2014 07:08 AM

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.

eklavya 05-29-2014 09:11 AM

Quote:

Originally Posted by battles (Post 5178643)
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'.

eklavya 05-29-2014 09:13 AM

Quote:

Originally Posted by rtmistler (Post 5178656)
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.

rtmistler 05-29-2014 12:35 PM

Quote:

Originally Posted by eklavya (Post 5178720)
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. :cool:


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