Solved

Find string and remove files

Posted on 2002-06-26
13
223 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
  • 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
Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

 
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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

Suggested Solutions

Hello fellow BSD lovers, I've created a patch process for patching openjdk6 for BSD (FreeBSD specifically), although I tried to keep all BSD versions in mind when creating my patch. Welcome to OpenJDK6 on BSD First let me start with a little …
FreeBSD on EC2 FreeBSD (https://www.freebsd.org) is a robust Unix-like operating system that has been around for many years. FreeBSD is available on Amazon EC2 through Amazon Machine Images (AMIs) provided by FreeBSD developer and security office…
Learn how to get help with Linux/Unix bash shell commands. Use help to read help documents for built in bash shell commands.: Use man to interface with the online reference manuals for shell commands.: Use man to search man pages for unknown command…
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.:

776 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