LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 12-17-2008, 10:14 PM   #1
umwai
Member
 
Registered: May 2008
Location: Sri Lanka
Distribution: Ubuntu
Posts: 57
Blog Entries: 1

Rep: Reputation: 16
Angry Write a shell scriprt to delete files size of 56KB


I wanna know how to write a shell script that deletes specific kind of a file type with a 56KB
example : exe files with 56KB
 
Old 12-17-2008, 10:41 PM   #2
shanecraddock@gmail.com
LQ Newbie
 
Registered: Dec 2008
Posts: 10

Rep: Reputation: 0
find . -type f -name "*.exe" -size 56k -exec rm -f {} \;
 
Old 12-17-2008, 11:53 PM   #3
Junior Hacker
Senior Member
 
Registered: Jan 2005
Location: North America
Distribution: Debian testing Mandriva Ubuntu
Posts: 2,687

Rep: Reputation: 61
Code:
#!/bin/bash
find . -name "*.exe" -size 56k -delete
Code:
#!/bin/bash
find / -type f -name "*.exe" -size -56k -exec rm -f {} \;
Code:
#!/bin/bash
find / -name "*.exe" -size +56k -delete
These are three different scripts, you would save it with a name, something like "56k" in your /home directory or wherever (somewhere in your path). Then you would just type: 56k in a terminal to execute.

The first example deletes all .exe files which are precisely 56KB from the directory you are in (PWD) and all subdirectories.
The second example deletes all .exe files 56KB and under starting from root directory and all subdirectories, meaning it'll delete 56KB and under .exe files throughout the entire file system.
The third deletes all files larger than 56KB from root and all subdirectories.
There are many ways to use the find command, this shows two ways, one may be safer than the other.
 
Old 12-18-2008, 12:00 AM   #4
Junior Hacker
Senior Member
 
Registered: Jan 2005
Location: North America
Distribution: Debian testing Mandriva Ubuntu
Posts: 2,687

Rep: Reputation: 61
Test any scripts you get from forums/online on expendable files in a testing directory first before executing in valuable areas of the file system.
Just a safety precaution.
 
Old 12-18-2008, 12:18 AM   #5
billymayday
LQ Guru
 
Registered: Mar 2006
Location: Sydney, Australia
Distribution: Fedora, CentOS, OpenSuse, Slack, Gentoo, Debian, Arch, PCBSD
Posts: 6,678

Rep: Reputation: 122Reputation: 122
With find, it's usually safest to run it without the -delete or -exec part first. It will just display a list of files that meet the given criteria. So, for example

find . -name "*.exe" -size 56k

and see if the list makes sense before running

find . -name "*.exe" -size 56k -exec rm -f {} \;
 
  


Reply

Tags
shell script



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
How to write a script to delete files? AGazzaz Linux - General 11 12-05-2007 06:43 AM
Delete Files by size LoafOfBread34 Linux - General 3 11-28-2005 11:16 AM
vsftpd: allow users to write and delete files me3 Linux - Software 4 03-04-2005 07:22 PM
clients can write to files but they cannot delete them sparsh Linux - Networking 1 05-10-2003 09:54 PM
delete files by size Serena Linux - Newbie 2 04-25-2002 07:38 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 01:05 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