Solved

Find string and remove files

Posted on 2002-06-26
13
222 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
 
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Help stop mail server from sending spam. 27 122
Bad Block Relocation for Synchronous GLVM AIX 7.1 2 65
SQUD PROXY SERVER, UNIX, SLL/HTTPS 5 72
Linux hostname change 2 51
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…
Installing FreeBSD… FreeBSD is a darling of an operating system. The stability and usability make it a clear choice for servers and desktops (for the cunning). Savvy?  The Ports collection makes available every popular FOSS application and packag…
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 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…

932 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now