LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 07-24-2003, 02:02 PM   #1
flippy
LQ Newbie
 
Registered: Jul 2003
Distribution: Slackware 9.0, Gentoo 1.4
Posts: 7

Rep: Reputation: 0
Help with file search and destroy script


I've been working on a script for my boss so that he can automate going through ftp accounts and getting rid of "unnecessary" files like .exes and .mp3s. Some people are actually smart enough to rename files so i am using the file command to find the renamed files. I have not been able to get this up and running yet so I brought it to this GREAT forum and hopefully someone will be able to pick it apart and help give me some idea of what i need to change. Any thoughts, help or suggestions would be appreciated.

#!/bin/bash

ls /home/*/ > temp.txt <--Not sure how to make this recursive

while [ -n $1 ]
do
loc = locate $1
f=file loc | tr -s " " " " | awk '/.*xec.*/ {print}' | cut --delimiter= " "
--fields=1 | tr -d ":"
if [ -n $f ]
then
echo Removing Illegal File $f
rm -rf $f <-- I know the the rf is a bit excessive
fi
done < temp.txt

rm -rf temp.txt <--Same here but i wanna make sure the file is gone
 
Old 07-24-2003, 05:20 PM   #2
Dark_Helmet
Senior Member
 
Registered: Jan 2003
Posts: 2,786

Rep: Reputation: 374Reputation: 374Reputation: 374Reputation: 374
Code:
#!/bin/bash

find /home -type f > temp.txt

while [ -n $1 ]
do
  f=`file $1 | tr -s " " " " | awk '/.*xec.*/ {print}' | cut --delimiter " " --fields=1 | tr -d ":"`

  if [ -n $f ]
  then
    echo Removing Illegal File $f
    rm -f $f

    if [ -e "$f" ]
      echo "You weren't just being paranoid, $f is still here"
    fi

  fi

done < temp.txt

rm -f temp.txt

if [ -e temp.txt ]
  echo "You weren't just being paranoid, temp.txt is still here"
fi
Couldn't resist the paranoid bit... I'm guilty of it sometimes myself...

EDIT:
Stupid copy-and-paste errors! The first "paranoid" test was not programmed to look at the right file... DOH!

EDIT #2:
Stupid coder errors! That same test condition wasn't nested in the if block above like it was supposed to be... DOH!

Last edited by Dark_Helmet; 07-24-2003 at 06:04 PM.
 
Old 07-24-2003, 05:51 PM   #3
flippy
LQ Newbie
 
Registered: Jul 2003
Distribution: Slackware 9.0, Gentoo 1.4
Posts: 7

Original Poster
Rep: Reputation: 0
Talking Thanks!

thanks a lot for taking the time to look at it. yeah when it comes to making sure everything is cleared out I'm obviously a bit paranoid with that
 
Old 07-24-2003, 10:55 PM   #4
Strike
Member
 
Registered: Jun 2001
Location: Houston, TX, USA
Distribution: Debian
Posts: 569

Rep: Reputation: 31
find has an exec bit that you can pass the filenames to ...

Code:
#!/bin/sh

find /home -type f -name *.mp3 -print -exec rm -f {};
find /home -type f -name *.exe -print -exec rm -f {};
 
Old 07-25-2003, 02:16 PM   #5
flippy
LQ Newbie
 
Registered: Jul 2003
Distribution: Slackware 9.0, Gentoo 1.4
Posts: 7

Original Poster
Rep: Reputation: 0
Thanks

Yeah that'll be nice for the non-renamed ones but will it work for the files that have been renamed? like the file blob.exe changed to kittens.ppt?
 
Old 07-25-2003, 04:29 PM   #6
TheLinuxDuck
Member
 
Registered: Sep 2002
Location: Tulsa, OK
Distribution: Slack, baby!
Posts: 349

Rep: Reputation: 33
flippy:

(I like your alias, btw!)

AFA mp3's are concerned, if you've got a relatively speedy system, you could parse the files contents and search for mp3 specific information.. that way, you're not relying on the filename.. of course, this is much much slower than just simply searching for filenames... I assume that the same could be done for binary's as well..

that's about the only real way of which I can think to really get rid of all the things you don't want on the system.

deleting by filename is faster, for sure, but not effective for getting renamed files, especially if you're not sure to what they have been renamed.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Does anyone know of a bash script can search & replace txt in a file. jimwelc Linux - Newbie 6 09-15-2008 12:13 AM
Script to search and replace in text file - kinda... jeffreybluml Programming 45 11-07-2004 05:37 PM
Search and replace text in file using shell script? matthurne Linux - Software 2 11-02-2004 10:11 AM
How to 'apt-cache search' & 'apt-file search' by distribution? davidas Debian 3 04-19-2004 01:56 PM
Search for a file using shell script sharathkv Linux - Newbie 2 08-06-2003 05:05 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 12:18 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration