Searching and Removing Files using 1 command

Hello Friends,

Today I used my USB stick in friend system, he having windows OS in his system. After used in that system my USB stick contains of lot of .exe files (May be VIRUS). When i used it my Ubuntu PC i founded that it is unwanted to me so i decided to delete it. I removed it in 2 folders manually then only i decided to remove it automatically using Linux commands.

I tried it with list, search and remove commands,  ls, grep, rm respectively. But i failed to do that i can search and list all the files that have extension called .exe using the command

bala@bala-desktop:/media/KRISH$ ls -R | grep “.exe”
bala .exe
Aptitude .exe
Project .exe
connection .exe
waste .exe
tabtoenter .exe
wholemodule-change .exe
Resume updt 05-03-11 .exe
Resume .exe
Scanned Documents .exe
Screen Cast .exe
Vid Tut .exe

If i add the remove command means its not worked

bala@bala-desktop:/media/KRISH$ ls -R | grep “.exe” | rm -rf

Again I tried using

bala@bala-desktop:/media/KRISH$ ls -R | grep “.exe” | rm -r *.exe

This time i can delete files in the current directory only. My aim is to delete all folders and sub folders in the USB. Finally i decided to google for this and get the solution as i expected. I would like share here…

bala@bala-desktop:/media/KRISH$ find . -name “*.exe” -exec rm -i {} \;
rm: remove regular file `./bala/connection/connection .exe’? y
rm: remove regular file `./bala/waste/waste .exe’? y
rm: remove regular file `./bala/Project/Project .exe’? y
rm: remove regular file `./bala/tabtoenter/tabtoenter .exe’? y
rm: remove regular file `./bala/wholemodule-change/wholemodule-change .exe’? y
rm: remove regular file `./bala/Aptitude/Aptitude .exe’? y
rm: remove regular file `./bala/bala .exe’? y
rm: remove regular file `./Resume updt 05-03-11/Resume updt 05-03-11 .exe’? y
rm: remove regular file `./Resume updt 05-03-11/Resume/Resume .exe’? y
rm: remove regular file `./Scanned Documents/Scanned Documents .exe’? y
rm: remove regular file `./Screen Cast/Screen Cast .exe’? y
rm: remove regular file `./Vid Tut/Vid Tut .exe’? y

By using the above command we can remove the file with user confirmation (y or n). Otherwise we can also use

bala@bala-desktop:/media/KRISH$ find . -name “*.exe” -exec rm -i {} \;

With this command we can remove tons of files in any format using single command in fraction of seconds.
Caution: Before removing file makes sure, you have backup of all-important files. Do not use rm command as root user it can do critical damage to Linux system.

Enjoy on removing files…. Ha ha 😉

Thank You.

With Regards,
V. BalaKrishnan.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s