Solved

Find string and remove files

Posted on 2002-06-26
13
221 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
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 
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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

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…
Why Shell Scripting? Shell scripting is a powerful method of accessing UNIX systems and it is very flexible. Shell scripts are required when we want to execute a sequence of commands in Unix flavored operating systems. “Shell” is the command line i…
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…
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.

705 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

18 Experts available now in Live!

Get 1:1 Help Now