Link to home
Start Free TrialLog in
Avatar of jasonkk
jasonkk

asked on

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
Avatar of bira
bira


find /yourdir -name "*jpeg" -exec rm {} ;
Avatar of jasonkk

ASKER

bira,

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

Thanks
Avatar of jasonkk

ASKER

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
for i in `find /yourdir -name "*.c"`
   do
   mv $i /temp
  done
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
jasonkk

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

     Below is the correct:

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

for i in `find /yourdir  -type f -exec fgrep -l jpeg {} \;`
  do
  mv $i /temp
  done
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
ASKER CERTIFIED SOLUTION
Avatar of bira
bira

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
rm `grep -l jpeg *`
Avatar of yuzh
This command should work for you:

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

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 *`
find . -type f -exec grep -l jpeg {} \; |awk '{print "mv "$1" /temp/"$1}'|sh
# works for any amount of found files