The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
Google UNIX.COM


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Transfer files wih directory structure. uxlunatick SCO 11 04-28-2008 12:25 PM
remove empty directory sirrtuan Shell Programming and Scripting 6 03-04-2008 06:50 AM
copy files with directory structure adddy UNIX for Dummies Questions & Answers 3 12-11-2006 05:50 AM
MV files from one directory structure(multiple level) to other directory structure srmadab UNIX for Advanced & Expert Users 4 09-13-2006 01:01 PM
disk space used for files with in a directory structure. kasala Shell Programming and Scripting 8 01-14-2005 01:26 PM

Reply
 
Submit Tools LinkBack Thread Tools Display Modes
  #1  
Old 06-04-2008
Registered User
 

Join Date: Jun 2008
Posts: 3
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 trying to do this for weeks now and its driving me mad, I'm quite new to this, although I am getting to grips with it slowly.

Cheers
Reply With Quote
Forum Sponsor
  #2  
Old 06-04-2008
Registered User
 

Join Date: Apr 2008
Posts: 46
use the following command to identify the empty files in ur directory

find . -name "*.*" -size 0c
Reply With Quote
  #3  
Old 06-05-2008
Registered User
 

Join Date: Jun 2008
Posts: 3
Cool

Thanks for the reply m8.

I don't think I explained what I was trying to do correctly, I'm trying to create a shell script that will allow me to find all empty files within my directory tree, list them and then give me the option of deleting them. I would like to be able to get the option of deleting them in the format of :

delete word.doc y/n? :> y
word.doc deleted.

I know some files that are empty are not to be deleted this is why I need to see what I am about to get rid of.

I'm just strating out doing Linux and I've hit a brick wall with this so any help would be appreciated, cheers.

Last edited by cat123; 06-05-2008 at 03:56 AM.
Reply With Quote
  #4  
Old 06-05-2008
Registered User
 

Join Date: Mar 2008
Location: /bin/sh
Posts: 352
This pseudo code may put you on right lines...

Code:
file_names=`find . -name "*.*" -size 0c`
for i in $file_names
do
print "Do you want to delete the file?"
read input
if ans='y' or 'Y'
rm -f $i
else 
#do nothing
echo "File not deleted"
fi
done
Reply With Quote
  #5  
Old 06-05-2008
Moderator
 

Join Date: Sep 2007
Location: Germany
Posts: 958
You can also use
Code:
find . -name "*.*" -size 0c -exec rm -i {} \;
"rm -i" asks you if you want to delete it or not. Works on Debian Linux and AIX at least.

Btw. that find did not work for me on Debian and AIX. If you encounter the same problem, you might try:
Code:
find . -type f -size -1 -exec rm -i {} \;
Reply With Quote
  #6  
Old 06-05-2008
Registered User
 

Join Date: Jun 2008
Posts: 3
cheers guys for the help, im gonna give it a go and I'll let you's know how I do.

Cheers
Reply With Quote
Google The UNIX and Linux Forums
Reply

Tags
linux

Thread Tools
Display Modes




All times are GMT -7. The time now is 04:07 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008. All Rights Reserved.Ad Management by RedTyger Visit The Complex Event Processing Blog

Content Relevant URLs by vBSEO 3.2.0