Delete empty files from a directory entered in command prompt


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Delete empty files from a directory entered in command prompt
# 1  
Old 02-15-2012
Delete empty files from a directory entered in command prompt

My code to "Delete empty files from a directory entered in command promt"

Code:
 
#/bin/sh
echo "Enter directory"
read gh
for file in `ls $gh`
do
# to get the size of file
a=$( ls -l file | awk ' {print $7} ');
echo $a
if [ "$a" = 0 ]
then
echo "removing file "
rm file
 
fi
done

ERROR :

$ ./filsiz1
Enter directory
dir1
ls: cannot access file: No such file or directory

Thanks,
adirajup
# 2  
Old 02-15-2012
That's a useless use of ls.

You don't need ls -l to check if the filesize is zero, either.

You use $file to convert a variable name into the text inside of it, too.

Code:
echo "Enter directory"
read gh

if [ ! -d "$gh" ]
then
        echo "$gh doesn't exist or isn't a file"
        exit 1
fi

for file in ${gh}/*
do
        # If we somehow get an invalid filename, ignore it.
        # -e means 'file exists'.
        [ -e "$file" ] || continue
        # -s means "if the file exists and has nonzero size".
        [ -s "$file" ] && continue
        # remove the 'echo' once you've tested this and are sure it works
        echo rm -f "$file"
done

This User Gave Thanks to Corona688 For This Post:
# 3  
Old 02-16-2012
Code:
find yourdirectory -type f -ls 2>/dev/null | 
while read x x x x x x s x x x f
do
  if [ $s -eq 0 ]; then
    rm -f "$f"
  fi
done

This User Gave Thanks to chihung For This Post:
# 4  
Old 02-17-2012
It worked .thanks a lot Smilie
Code:
    [  -s "$file" ] && continue

what if the file has 0 bytes will "continue" work to remove the file in the next statement
# 5  
Old 02-17-2012
Quote:
Originally Posted by adirajup
It worked .thanks a lot Smilie
Code:
    [  -s "$file" ] && continue

what if the file has 0 bytes will "continue" work to remove the file in the next statement
That statement means, "if the file exists and is more than 0 bytes, skip it and go back to the top of the loop".

Empty files don't have more than one byte, and won't skip to the top of the loop.
# 6  
Old 02-17-2012
Code:
find "$dir" -mindepth 1 -prune -size 0c -exec rm {} \;

For greater efficiency, replace \; with +.

I don't think the task -- deleting empty files in a user-supplied directory without descending into any subdirectories -- can be reliably accomplished using just POSIX-standard find functionality (mindepth is an extension to POSIX).

In case someone cares to take a stab at it: http://pubs.opengroup.org/onlinepubs...ties/find.html.

Regards,
Alister

Last edited by alister; 02-17-2012 at 06:02 PM..
# 7  
Old 02-18-2012
Thanks for the suggestion..

Code:
 
used descending order in code
 
for file in `ls /home/bob/$gh`    do
 
  [ -e "$file" ]  || continue
   [  -s "$file" ] && continue
rm -f "$file"
done

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. 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

2. Shell Programming and Scripting

Command to delete half of files in directory.

Hello Friends, I have directory called /tmp. which stores the log files. Whenever it becomes full, i want to delete half of files from all log files. even after deleting the files, if space is more than 90% then it should delete rest of half files. While deleting files, older files... (7 Replies)
Discussion started by: Nakul_sh
7 Replies

3. Shell Programming and Scripting

To get the files in a directory for the given date (User entered date)

Need a ksh script to get the files that were created or modified in a directory on a particular date entered by the user. For example if a directory contains files as below : > ll total 41 -rw-rw-r-- 1 psn psn 199 Aug 23 07:06 psn_roll.sh -rw-rw-r-- 1 psn psn ... (10 Replies)
Discussion started by: ramprabhum
10 Replies

4. Shell Programming and Scripting

Script to delete folders and files from a prompt

Hi Everyone, I work for GE Money IVR as a DB analyst and the environment on which I work is Solaris 5.0 server and Oracle 11g. I got a project in which I have to clean up the folders and files which are not used in DB. I copied an existing script and edited it, dont know this is the... (5 Replies)
Discussion started by: habeeb506
5 Replies

5. Homework & Coursework Questions

Find and delete empty files and directories

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted! 1. The problem statement, all variables and given/known data: Need to make a script, to remove all empty files and folders from current category. It also should show the name... (2 Replies)
Discussion started by: Itixop
2 Replies

6. Shell Programming and Scripting

Script to delete empty files

I'm trying to write a shell script to files of zero length in a specified directory, but I keep getting errors. Would anybody be kind enough to look it over for issues? Thanks a bunch in advance. #!/bin/sh if then if then find $1 -type f -size 0 -print|xargs rm exit 0... (1 Reply)
Discussion started by: ScriptingIssues
1 Replies

7. Shell Programming and Scripting

Prompt for 2 variables if nothing is entered end script

What I'm trying to do is write a bash file that prompts for the user's name and what the want to name the output file. If nothing is entered in either prompt the script must end and not create the output file. This is what I have so far: #!/bin/sh echo "What is your name?" read NAME if ... (4 Replies)
Discussion started by: kriminul1982
4 Replies

8. Shell Programming and Scripting

How to empty all files in a directory

Hi all, Can you tell me how to empty all files in a directory with a "find" command? It does not seem to work the way I try it: # ls -l *.dat -rw-r--r-- 1 root root 7 Jul 20 20:51 la2.dat -rw-r--r-- 1 root root 4 Jul 20 20:51 la.dat # find... (9 Replies)
Discussion started by: majormark
9 Replies

9. Solaris

How to delete Directory and inside files using Find command

I am using the following Command to delete Directory with contents. But this command is deleting inside files only not directories. is there any change need in my command? find -type f -mtime +3 -exec rm -r {} \; Thanks (3 Replies)
Discussion started by: bmkreddy
3 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