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
# 8  
Old 06-14-2009
Quote:
Originally Posted by cfajohnson

When you post code, please wrap it in [code] tags.

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

(And you're missing a double quote.)
ahh yes, thank you...

When you are creaing textfiles, one by one, giving all files a number always +1 ... you maybe dont't wan't to save files matching a size criteria, so you don't get gaps in the numbering, then maybe that solution is better.

If you built in

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

into a script, allways all files that match this criteria, are gone, even your script, if it does aswell.

If you have a one command slim version for this, I can built it in my scripts: Smilie
Code:
size=0
size=`ls -l "file.txt | awk -F" " '{ print $5 }'`
if [ $size -gt 478 ];
then ((d++));
fi

Where do we miss double quotes, this works like described above.

btw, can it be that if I preview a message and save afterards, I loose the CODE-Tags?

Last edited by lowmaster; 06-15-2009 at 12:00 AM..
# 9  
Old 06-15-2009
Code:
for i in *;do
if [ ! -s $i ];then
rm $i
fi
done

# 10  
Old 06-15-2009
Quote:
Originally Posted by lowmaster
ahh yes, thank you...

When you are creaing textfiles, one by one, giving all files a number always +1 ... you maybe dont't wan't to save files matching a size criteria, so you don't get gaps in the numbering, then maybe that solution is better.

If you built in

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

into a script, allways all files that match this criteria, are gone, even your script, if it does aswell.

Of course. That was the question that was asked. And it is the same no matter what size you use or whether you use \; or +.

If you want a different size, that can easily be accommodated:

Code:
size=$1
find . -size "$size" -exec rm {} +

Quote:

If you have a one command slim version for this, I can built it in my scripts: Smilie
Code:
size=0


Why do you set size to 0 when you redefine it on the next line?
Quote:
Code:
size=`ls -l "file.txt | awk -F" " '{ print $5 }'`


Why are you using two external commands (ls and awk)?

Code:
size=$( wc -c < file.txt)

Quote:
Code:
if [ $size -gt 478 ];
then ((d++));
fi

Where do we miss double quotes, this works like described above.

If it worked for you, you didn't run what you posted.

You have an unmatched double quote: "file.txt
Quote:

btw, can it be that if I preview a message and save afterards, I loose the CODE-Tags?

"loose"??? Do you mean "lose"?

You lose all quotes but the last level. (Previewing has nothing to do with it.)
-----Post Update-----

Quote:
Originally Posted by summer_cherry
Code:
for i in *;do
if [ ! -s $i ];then
rm $i
fi
done


Your script will fail if any filenames contain spaces.

Code:
if [ ! -s "$i" ];then
  rm "$i"
fi


Why is the post being appended to this post when I was replying to a different post?

Last edited by cfajohnson; 06-15-2009 at 04:29 PM..
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