To remove files with 0 File Size


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting To remove files with 0 File Size
# 1  
Old 06-14-2009
To remove files with 0 File Size

Dear All,

I want to remove files having size of (0) that are generated once script is completed.

Here is the code

but i m not sure about /usr/bin

do i need to write /usr/bin, or the path of my script

/tmp/test

please find below the script logic

Code:
 
#!/usr/bin/ksh
for i in `ls -a`
do
  FILESIZE=`ls -l $i|awk '{print $5}'`
  if [ $FILESIZE -eq 0 ]
  then
    /usr/bin/rm $i
  fi
done

as /usr/bin contains all executable files.
# 2  
Old 06-14-2009
use find command its easy
Code:
find . -size 0 -exec rm {} \;

or if you want to use loop
use test command inside your for loop gothrough the man page of test
test ! -s filename

Last edited by vidyadhar85; 06-14-2009 at 03:44 PM..
# 3  
Old 06-14-2009
Thnkz vidyadhar85

I recieve followng output

Code:
find: 0652-018 An expression term lacks a required parameter

Can u please write the complete line for this case.
# 4  
Old 06-14-2009
one extra sapce was introuduced between -exec rm {} \ ;
try like this
Code:
 find . -size 0 -exec rm {} \;

# 5  
Old 06-14-2009
Quote:
Originally Posted by vidyadhar85
use find command its easy
Code:
find . -size 0 -exec rm {} \ ;


There should be no space between the backslash and the semi-colon.

More efficiently:

Code:
find . -size 0 -exec rm {} +

# 6  
Old 06-14-2009
with this you cam enter a size of your desire.
here all below 478 bytes get deleted:

size=0
size=`ls -l "file.txt | awk -F" " '{ print $5 }'`
if [ $size -gt 478 ];
then ((d++));
fi
# 7  
Old 06-14-2009
Quote:
Originally Posted by lowmaster
with this you cam enter a size of your desire.
here all below 478 bytes get deleted:

When you post code, please wrap it in [code] tags.
Quote:
Code:
size=0
size=`ls -l "file.txt | awk -F" " '{ print $5 }'`
if [ $size -gt 478 ];
then ((d++));
fi


Do you really propose calling two external programs (ls and awk) for every file?

(And you're missing a double quote.)
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Looping the files for getting the file size information

Am using Linux 3.10 years.txt is input source file which has the list of year like below 2013 2014 2015 2016 Other Input files are XX_TEST1_YR2016_01012018.csv XX_TEST1_YR2015_01012018.csv XX_TEST1_YR2014_01012018.csv XX_TEST1_YR2013_01012018.csv XX_TEST2_YR2016_01012018.csv... (2 Replies)
Discussion started by: weknowd
2 Replies

2. Shell Programming and Scripting

How to remove a file in shell script if its size exceeds limit?

How can i remove a file using shell script when its size exceeds 10MB. Given that file is located in different location to the shell script where it is running? (4 Replies)
Discussion started by: vel4ever
4 Replies

3. Shell Programming and Scripting

Remove duplicate lines from a 50 MB file size

hi, Please help me to write a command to delete duplicate lines from a file. And the size of file is 50 MB. How to remove duplicate lins from such a big file. (6 Replies)
Discussion started by: vsachan
6 Replies

4. Shell Programming and Scripting

Find duplicate files by file size

Hi! I want to find duplicate files (criteria: file size) in my download folder. I try it like this: find /Users/frodo/Downloads \! -type d -exec du {} \; | sort > /Users/frodo/Desktop/duplicates_1.txt; cut -f 1 /Users/frodo/Desktop/duplicates_1.txt | uniq -d | grep -hif -... (9 Replies)
Discussion started by: Dirk Einecke
9 Replies

5. Shell Programming and Scripting

Getting the total file size for certain files per directory

Hi, I am trying to get the total file size for certain files per directory. I am using find /DirectoryPath -name '*.dta' -exec ls -l {} \; | awk '{ print $NF ": " $5 }' > /users/cergun/My\ Documents/dtafiles.txt but this lists all the files in the directories. I need the total... (9 Replies)
Discussion started by: cergun
9 Replies

6. Shell Programming and Scripting

To remove the files in MB size

Hi, Using the shell script, I need to remove the files that are larger than 50 MB in size. I am not good in shell scripting. (1 Reply)
Discussion started by: gsiva
1 Replies

7. Shell Programming and Scripting

Check file size and remove files

Hi, Here we have a situation where we have to check the file size and if the file size is greater than 0 bytes then remove the files from the directory. 1)EdwTrxn 2)EdwPost 3)EdwTndr 4)EdwSls 5)EdwSlsRej 6)EdwTndrRej Files will be created in the directory in the following manner. ... (5 Replies)
Discussion started by: srivsn
5 Replies

8. Shell Programming and Scripting

Files with 0 file size and created today

hi, I am building a script to identify those files created/modified today and with file size 0. I am able to find the files with 0 file size and created/modified in last 24 hrs as shown below but not today (current date), I tried using (touch -t time filenm) but in my version of unix at work it... (7 Replies)
Discussion started by: rushs682
7 Replies

9. Shell Programming and Scripting

bash script working for small size files but not for big size files.

Hi, I have one file stat. Stat file contents are as follows: for example. H50768020040913,00260100,507680,13,0000000643,0000000643,00000,0000 H50769520040808,00260100,507695,13,0000000000,0000000000,00000,0000 H50770620040611,00260100,507706,13,0000000000,0000000000,00000,0000 Now i... (1 Reply)
Discussion started by: davidpreml
1 Replies

10. Shell Programming and Scripting

remove files with 0 size, space and double qoute

os=hpux11 shell=ksh some jokers had written thousands of empty files into my $HOME. and the files are named inconsistently that some of them include space and double qoutes. all those files are of 0 size (when i did ls -al it told me so). the naming paterns are like e.g of ls -al output: ... (3 Replies)
Discussion started by: nongrad
3 Replies
Login or Register to Ask a Question