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 |
You are searching for files that contain "searchword" in the filename or in the file itself?
|
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:
|
Quote:
Thanks all. |
IFS=$'\n'
lst=$(grep -ilr "searchedword" /absolute/path/of/the/directory/* ) for i in $lst; do rm $i; done What does IFS=$'\n' do? |
Here's how I do that; however FIRST I ensure that the find works the way intended:
Code:
find . -type f -name "*pattern*" Code:
find . -type f -name "*pattern*" -exec rm -f {} \; |
Quote:
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'. |
Quote:
|
Quote:
|
All times are GMT -5. The time now is 10:09 PM. |