LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (http://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Write a shell scriprt to delete files size of 56KB (http://www.linuxquestions.org/questions/linux-newbie-8/write-a-shell-scriprt-to-delete-files-size-of-56kb-691366/)

umwai 12-17-2008 10:14 PM

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

shanecraddock@gmail.com 12-17-2008 10:41 PM

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

Junior Hacker 12-17-2008 11:53 PM

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.

Junior Hacker 12-18-2008 12:00 AM

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.

billymayday 12-18-2008 12:18 AM

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 {} \;


All times are GMT -5. The time now is 04:42 PM.