?
Solved

Find string and remove files

Posted on 2002-06-26
13
Medium Priority
?
227 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
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 400 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

Industry Leaders: 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

When you do backups in the Solaris Operating System, the file system must be inactive. Otherwise, the output may be inconsistent. A file system is inactive when it's unmounted or it's write-locked by the operating system. Although the fssnap utility…
A metadevice consists of one or more devices (slices). It can be expanded by adding slices. Then, it can be grown to fill a larger space while the file system is in use. However, not all UNIX file systems (UFS) can be expanded this way. The conca…
Learn how to find files with the shell using the find and locate commands. Use locate to find a needle in a haystack.: With locate, check if the file still exists.: Use find to get the actual location of the file.:
In a previous video, we went over how to export a DynamoDB table into Amazon S3.  In this video, we show how to load the export from S3 into a DynamoDB table.
Suggested Courses

777 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