Shell script to remove empty files


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Shell script to remove empty files
# 1  
Old 11-20-2011
Shell script to remove empty files

Hi All,

Can anyone please write a shell script to remove the empty files using an if condition.

please help me out , urgent

thanks
# 2  
Old 11-21-2011
Code:
find . -type f -size 0 -exec rm {} \;

This User Gave Thanks to rdcwayx For This Post:
# 3  
Old 11-21-2011
thank you so much !!!!!!
# 4  
Old 11-21-2011
Put the below script in the directory in which you want to remove empty files (i.e. files with size zero)

Code:
#! /bin/bash
for x in *
do
    if [ -s $x ]
    then
        continue
    else
        rm -rf $x
    fi
done

This User Gave Thanks to balajesuri For This Post:
# 5  
Old 11-21-2011
Thanks a lot !!!!!!
# 6  
Old 11-21-2011
Code:
 find ~ -empty

or
Code:
for i in *
do
if [ -s $i ]; then
echo "$i is a file and size is greater than zero."
else
echo "$i is an empty file and deleting now..."
rm -rf $i
fi
done

# 7  
Old 11-21-2011
Code:
 
for i in *; do [ ! -s $i ] && rm -rf $i; done

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Remove empty files in home directory

how to remove empty files tried below command its remove only zero bytes not empty file which is greater then zero byte. for x in * do if then rm $x fi done (8 Replies)
Discussion started by: Kalia
8 Replies

2. Shell Programming and Scripting

Shell script to delete empty files from specific locations

Hi, I need help in regard to developing a shell script to delete empty files from multiple specific locations. The directory paths will be stored in a text file. So the requirement is to read the text file for one specific path and then remove empty files from that particular path. Looping through... (4 Replies)
Discussion started by: Khan28
4 Replies

3. Shell Programming and Scripting

Shell script to compare ,diff and remove betwen 2 files

Hi Friends Need your expertise. Command to check the difference and compare 2 files and remove lines . example File1 is master copy and File2 is a slave copy . whenever i change, add or delete a record in File1 it should update the same in slave copy . Can you guide me how can i accomplish... (3 Replies)
Discussion started by: ajayram_arya
3 Replies

4. Shell Programming and Scripting

How to create a shell script to remove the files on solaris server at 00000hrs?

Hi folks, As per mentioned in the title, how to create a shell script to delete those files from the server at 00000hrs every day? Thanks in advance :) (2 Replies)
Discussion started by: kimurayuki
2 Replies

5. Shell Programming and Scripting

Shell script to remove files

I am working in directory week5 and I want to delete the files in directory week1, without leaving directory week5. I understand that rm is used to delete a file, but how do you remove them while working in a different directory. I also only want to delete a certain set of files *. (1 Reply)
Discussion started by: smiley76112
1 Replies

6. Shell Programming and Scripting

perl script to check if empty files are created and delete them and run a shell script

I have a local linux machine in which the files are dumped by a remote ubuntu server. If the process in remote server has any problem then empty files are created in local machine. Is there any way using perl script to check if the empty files are being created and delete them and then run a shell... (2 Replies)
Discussion started by: hussa1n
2 Replies

7. AIX

how to loop through non-empty files with shell script on AIX

I have av script that loops through some statistic files to create a report. We would like to only loop through non-empty files as these files create an empty report-line. I have figured out how to find the non-empty files, but not how to loop through only those files. Here is the code that finds... (4 Replies)
Discussion started by: Tessa
4 Replies

8. Shell Programming and Scripting

shell script to remove old files and write to a log file

Hi, I have a script that works on a unix box but am trying to get it working on a linux box that uses shell. I am not a programmer so this is proving harder than I imagined. I made some changes and ended up with the script below but when I run it I get the following messages. Any help would be... (4 Replies)
Discussion started by: yabai
4 Replies

9. Shell Programming and Scripting

Script to remove all empty files within the directory structure?

Hi I need to write a shell script which basically searches for all the empty files within the directory structure, lists them before asking the user to confirm if they would like to delete them. If the user deletes the file then a notice would appear confirming the file is deleted. I've be... (5 Replies)
Discussion started by: cat123
5 Replies

10. UNIX for Advanced & Expert Users

how to delete empty files in a shell script

I'm trying to figure out a way to delete empty files in a directory. I have a cron that runs and creates a flat file every 15 mins. However, most times at night the flat file will be empty. I'd like to run a script to delete empty files that end with *.dat Any suggestions? Rich (1 Reply)
Discussion started by: rpnuge
1 Replies
Login or Register to Ask a Question