LinuxQuestions.org
Did you know LQ has a Linux Hardware Compatibility List?
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
 
LinkBack Search this Thread
Old 10-09-2009, 08:54 PM   #1
pratap.iisc
LQ Newbie
 
Registered: Nov 2006
Posts: 19

Rep: Reputation: 1
Shell script to automatically delete files with the same name as the parent directory


Hi aall,
I am facing a problem in Windows due to a virus called Newfolder.exe which creats files with the same name as it's parent directory and an extension .exe and this happens for every directory in the entire hierarchy in the infected pen drive. The antivirus detects them, but is sucking slow. So I thought this is a good opportunity to use the concepts of the all mighty shell script to remove those as they follow the same pattern.
Can anybody help me in writing the script?
Let me just give an example.

Say my complete path is
Code:
/home/pkd/fol1/
The virus would have created an file with complete paths
Quote:
/home/pkd/fol1.exe
If fol1 has two more directories fol11 and fol12
Then there would be two more .exe(virus created) in the following path
Quote:
/home/pkd/fol11/fol11.exe
/home/pkd/fol12/fol12.exe

Waiting for some useful suggestions.
Thanks in advance.
-pratap
 
Old 10-09-2009, 09:07 PM   #2
nadroj
Senior Member
 
Registered: Jan 2005
Location: Canada
Distribution: ubuntu
Posts: 2,538

Rep: Reputation: 58
heres some pseudocode for this:

Code:
- set initial/starting path (i.e. "/home/pkd/" in your example)
- for each directory, "dir", in starting directory (and recursively): 
  - delete the file with name dir.exe (you already know the initial/starting directory, and the file to delete, "dir", so just append ".exe" to it)
you can use "ls" to list only directories, and you can also have it do list recursively. the "rm" command can be used to delete the file. you may want to include a simple check before the delete, to make sure youre deleting a file that actually exists (otherwise youll get error messages when you run the script, which is probably fine if you dont care about that).

i dont think you mentioned which scripting language, but just pick any one you are comfortable with (and that you have available, of course). the general idea is the same for any one you choose.

i imagine youre doing this on a "production" machine, i.e. one with important data. if this is the case then make sure to manually follow the flow of the code you write, in order to make sure it is "bug-free", otherwise very bad things will happen (you loose important files you didnt think it would delete).

Last edited by nadroj; 10-09-2009 at 09:10 PM.
 
Old 10-09-2009, 11:43 PM   #3
pratap.iisc
LQ Newbie
 
Registered: Nov 2006
Posts: 19

Original Poster
Rep: Reputation: 1
Found a working solution!

Hi,
after some trial I have come up with a working code.
But I would appreciate any other suggestion too and feedback on this code.

Quote:
mkdir -p /tmp/affected/; find -type d | cat | while read LINE; do k=`echo "$LINE"| sed 's/.*\///g'` ; j="$LINE""/""$k"".exe"; mv "$j" /tmp/affected/.; done;
Regards,
Pratap
 
Old 10-10-2009, 12:05 AM   #4
smeezekitty
Senior Member
 
Registered: Sep 2009
Location: washington U.S.
Distribution: Damn Small Linux, KateOs, M$ Ickdows Vista, My own OS
Posts: 2,094

Rep: Reputation: 104Reputation: 104
whats the | cat | for
 
Old 10-10-2009, 02:03 AM   #5
vonbiber
Member
 
Registered: Apr 2009
Distribution: slackware
Posts: 280

Rep: Reputation: 48
If I understand correctly you want to remove all the
foo.exe under a certain directory (/home/pkd/foll) if the
directory above has the same name (minus the extension).
In which case, the following script would do the job
(adjust the variable TOP to your actual top directory).
If some of your directories or files have spaces or
spurrious characters use double quotes ("$f" instead of $f).

Code:
#!/bin/sh

TOP=/home/pkd/foll

find $TOP/* | while read f
do
        if [ -d $f ]; then
                name=${$f##*/}
                if [ -e $f/$name.exe ]; then
                        rm -f $f/$name.exe
                fi
        fi
done
this script recurses thru the entire tree under the
top directory and every time it finds a directory it
checks the presence of a file that has the same name
as the base directory and an 'exe' extension. If it finds
any it removes it.
 
Old 10-11-2009, 01:47 AM   #6
pratap.iisc
LQ Newbie
 
Registered: Nov 2006
Posts: 19

Original Poster
Rep: Reputation: 1
Quote:
Originally Posted by smeezekitty View Post
whats the | cat | for
Oh yes...That's not required.
Thanks for pointing that out.
 
Old 10-11-2009, 01:55 AM   #7
pratap.iisc
LQ Newbie
 
Registered: Nov 2006
Posts: 19

Original Poster
Rep: Reputation: 1
Quote:
Originally Posted by vonbiber View Post
If I understand correctly you want to remove all the
foo.exe under a certain directory (/home/pkd/foll) if the
directory above has the same name (minus the extension).
In which case, the following script would do the job
(adjust the variable TOP to your actual top directory).
If some of your directories or files have spaces or
spurrious characters use double quotes ("$f" instead of $f).

Code:
#!/bin/sh

TOP=/home/pkd/foll

find $TOP/* | while read f
do
        if [ -d $f ]; then
                name=${$f##*/}
                if [ -e $f/$name.exe ]; then
                        rm -f $f/$name.exe
                fi
        fi
done
this script recurses thru the entire tree under the
top directory and every time it finds a directory it
checks the presence of a file that has the same name
as the base directory and an 'exe' extension. If it finds
any it removes it.


Hi vonbiber,
I am getting the following error if I run the code with the suggested modifications.
Quote:
line 8: ${$f##*/}: bad substitution
Do I need to make any further change?
 
Old 10-11-2009, 02:03 AM   #8
lutusp
Member
 
Registered: Sep 2009
Distribution: Fedora
Posts: 835

Rep: Reputation: 101Reputation: 101
Quote:
Originally Posted by pratap.iisc View Post

Hi vonbiber,
I am getting the following error if I run the code with the suggested modifications.

Do I need to make any further change?
The previous poster made a mistake. This line:

Code:
name=${$f##*/}
Should look like this:

Code:
name=${f##*/}
I won't vouch for the prior poster's script, which I haven't read. But this error is obvious. I wish people would test the code they post.
 
Old 10-11-2009, 02:53 AM   #9
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Slackware 13.37, Debian Squeeze
Posts: 7,987
Blog Entries: 25

Rep: Reputation: 1009Reputation: 1009Reputation: 1009Reputation: 1009Reputation: 1009Reputation: 1009Reputation: 1009Reputation: 1009
The looping code can be speeded up a little by using find's -d option (as already done in the one liner) and made robust in the case of directory names containing spaces by adding double quotes
Code:
#!/bin/sh

TOP=/home/pkd/foll

find $TOP/* -type d | while read dir
do
    name="${dir##*/}"
    if [ -e "$dir/$name.exe" ]; then
        rm -f "$dir/$name.exe"
    fi
done
 
Old 10-12-2009, 10:17 AM   #10
vonbiber
Member
 
Registered: Apr 2009
Distribution: slackware
Posts: 280

Rep: Reputation: 48
Quote:
Originally Posted by pratap.iisc View Post

Hi vonbiber,
I am getting the following error if I run the code with the suggested modifications.

Do I need to make any further change?
sorry about that
yeah the line
Code:
                name=${$f##*/}
should have read
Code:
                name=${f##*/}
 
  


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
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Script to delete files with 0k file size in a directory justgiver Linux - Newbie 4 01-28-2008 04:56 AM
How to create/delete temp/backup files through a shell script ? Sid2007 Programming 4 10-17-2007 01:55 PM
Script to delete week old files in a directory ilyavishnyakov Linux - General 11 02-13-2007 04:37 PM
Making shell script to 'locate' m4a files and delete them bd1308 Linux - Software 3 02-12-2006 10:44 AM
shell script: delete all directories named directory.# except directory.N brian0918 Programming 3 07-13-2005 06:54 PM


All times are GMT -5. The time now is 03:58 PM.

Main Menu
 
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
identi.ca: @linuxquestions
Facebook: @linuxquestions
Open Source Consulting | Domain Registration