Share your knowledge at the LQ Wiki.
Go Back > Forums > Linux Forums > Linux - Newbie
User Name
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!


  Search this Thread
Old 04-24-2002, 12:33 PM   #1
Registered: Apr 2002
Location: Moon Kingdom
Distribution: Debian 4.0
Posts: 94

Rep: Reputation: 15
Question delete files by size

Is there any way to delete files by file size? I have an application that makes daily logs, and for some reason empty logs can have different sizes (normally 0-3 bytes each). So I'd like to delete everything under perhaps 5 bytes but retain the rest for examination.
Old 04-24-2002, 12:55 PM   #2
Registered: Apr 2002
Location: Denver
Distribution: SuSe,RedHat,Mandrake,
Posts: 109

Rep: Reputation: 16

You can either enter directly in the shell an if statement,
This is pseudo;

if [ filename ,Size > value ]
rm [filename]

or create a scrip for it and either execute it every so often or put it in cron to have it run automatically.

Here I'm assuming that you are knowledable with creating scripts.

Old 04-25-2002, 07:38 PM   #3
Registered: Jun 2001
Location: Fairfax, California
Distribution: RH 9.0, RH 7.3, Mandrake 8.0
Posts: 986

Rep: Reputation: 30
Use a combination of "find" and "exec". For example, to find and delete (without confirmation!) all files in the current directory smaller than 300 Bytes:
[root@Boris testbed]# ls -l
total 20
-rw-r--r--    1 root     root          468 Apr 25 16:59 bigfile
-rw-r--r--    1 root     root          234 Apr 25 17:27 sizetest.1
-rw-r--r--    1 root     root          234 Apr 25 17:27 sizetest.2
-rw-r--r--    1 root     root          234 Apr 25 17:27 sizetest.3
-rw-r--r--    1 root     root          234 Apr 25 17:27 sizetest.4
[root@Boris testbed]# find -size -300c -exec rm '{}' ';'
[root@Boris testbed]# ls -l
total 4
-rw-r--r--    1 root     root          468 Apr 25 16:59 bigfile
[root@Boris testbed]#
The hyphen before 300 indicates "less than"; a plus would indicate "greater than". The "c" prefix indicates Bytes; b indicates blocks, k and m are self explanatory.

To confirm each delete:
find -size -300c -ok rm '{}' ';'
(note the lack of the -exec)

To specify a directory other than the current, do it as you normally would with find.

Last edited by DMR; 04-25-2002 at 07:45 PM.


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
Script to delete files with 0k file size in a directory justgiver Linux - Newbie 4 01-28-2008 04:56 AM
Delete Files by size LoafOfBread34 Linux - General 3 11-28-2005 11:16 AM
How to delete files that won't delete? di11rod Linux - Security 7 10-19-2005 09:14 PM
increase point size size when printing PDF files newlin Red Hat 0 09-27-2003 07:13 AM
How to delete the destination files while the source files deleted in cp -u ? myunicom Linux - General 4 09-26-2003 01:13 PM > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 05:28 AM.

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