Find string and remove files

I'd like to know how to find a string "jpeg" from all the files in directory and remove the files that contains "jpeg" string using script.
Thanks
jasonkkAsked:
Who is Participating?
 
biraConnect With a Mentor Commented:
for i in `find /yourdir  -type f -exec fgrep -l jpeg {} \;`
  do
  mv $i /temp
  done
0
 
biraCommented:

find /yourdir -name "*jpeg" -exec rm {} ;
0
 
jasonkkAuthor Commented:
bira,

How about move to "temp" directory instead of delete.
It's kind of risky to delete the files.

Thanks
0
Cloud Class® Course: SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

 
jasonkkAuthor Commented:
bira,

I'm not trying to find the file name contains "jpeg".
I'm trying to find the string "jpeg" from inside the files.

Thanks
0
 
biraCommented:
for i in `find /yourdir -name "*.c"`
   do
   mv $i /temp
  done
0
 
haegerCommented:
find / -type f -exec grep jpeg {} \; > /tmp/the_filenames

Run that and you should have a file in your /tmp directory with the names of all normal files containing the string jpeg.  Read that file and dispose of the files as you see fit.

Best regards

.haeger
0
 
biraCommented:
jasonkk

   In my test i´ve used ".c".

     Below is the correct:

   for i in `find /yourdir -name "*.jpeg"`
   do
   mv $i /temp
  done
0
 
biraCommented:
The ultimate version:

for i in `find /yourdir  -type f -exec fgrep -l jpeg {} \;`
  do
  mv $i /temp
  done
0
 
haegerCommented:
Ya, I was thinking the same thing but I thought "Naah, he'll figure it out himself...".

Let's hope that it does the trick.

best regards

.haeger
0
 
ecwCommented:
rm `grep -l jpeg *`
0
 
yuzhCommented:
This command should work for you:

 rm `find . -type f -exec grep -l jpeg {} \;`

0
 
yuzhCommented:
Note: the above command will delete all the file contains a
      string "jpeg" from the current dir and all the subdir
      under the current dir.
      if you just want to delete the file from the current
      dir ONLY (no subdir) then ecw's last comment is the
      answer.
      rm `grep -l jpeg *`
0
 
ahoffmannCommented:
find . -type f -exec grep -l jpeg {} \; |awk '{print "mv "$1" /temp/"$1}'|sh
# works for any amount of found files
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.