Solved

Find string and remove files

Posted on 2002-06-26
13
225 Views
Last Modified: 2010-04-21
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
0
Comment
Question by:jasonkk
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 2
  • 2
  • +3
13 Comments
 
LVL 6

Expert Comment

by:bira
ID: 7111681

find /yourdir -name "*jpeg" -exec rm {} ;
0
 

Author Comment

by:jasonkk
ID: 7111700
bira,

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

Thanks
0
 

Author Comment

by:jasonkk
ID: 7111721
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 6

Expert Comment

by:bira
ID: 7111730
for i in `find /yourdir -name "*.c"`
   do
   mv $i /temp
  done
0
 
LVL 1

Expert Comment

by:haeger
ID: 7111748
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
 
LVL 6

Expert Comment

by:bira
ID: 7111771
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
 
LVL 6

Expert Comment

by:bira
ID: 7111836
The ultimate version:

for i in `find /yourdir  -type f -exec fgrep -l jpeg {} \;`
  do
  mv $i /temp
  done
0
 
LVL 1

Expert Comment

by:haeger
ID: 7111859
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
 
LVL 6

Accepted Solution

by:
bira earned 100 total points
ID: 7111898
for i in `find /yourdir  -type f -exec fgrep -l jpeg {} \;`
  do
  mv $i /temp
  done
0
 
LVL 5

Expert Comment

by:ecw
ID: 7112325
rm `grep -l jpeg *`
0
 
LVL 38

Expert Comment

by:yuzh
ID: 7112531
This command should work for you:

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

0
 
LVL 38

Expert Comment

by:yuzh
ID: 7112542
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
 
LVL 51

Expert Comment

by:ahoffmann
ID: 7112827
find . -type f -exec grep -l jpeg {} \; |awk '{print "mv "$1" /temp/"$1}'|sh
# works for any amount of found files
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Unix / Linux grid computing 5 169
AIX    Volume group Auto ON/OFF question 2 127
unix in java example 9 79
Solaris 4.1.3 cloning and booting 13 88
This tech tip describes how to install the Solaris Operating System from a tape backup that was created using the Solaris flash archive utility. I have used this procedure on the Solaris 8 and 9 OS, and it shoudl also work well on the Solaris 10 rel…
Every server (virtual or physical) needs a console: and the console can be provided through hardware directly connected, software for remote connections, local connections, through a KVM, etc. This document explains the different types of consol…
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
Learn how to navigate the file tree with the shell. Use pwd to print the current working directory: Use ls to list a directory's contents: Use cd to change to a new directory: Use wildcards instead of typing out long directory names: Use ../ to move…

696 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question