Search directories for files with zero sizes to delete


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Search directories for files with zero sizes to delete
# 1  
Old 08-12-2008
Question Search directories for files with zero sizes to delete

Hi,
I am trying to write a script that will use ls on a directory and list the files one at a time and their size. If the size is 0 i want it to ask me if I want to delete it (yes or no). If I say yes, I want it to delete but it won't know what the file name is just from running from the script. First I just cannot get it to display the name before the size.
This is what I have:
Code:
dirname=$1

clear

if [ $# -ge 2 ]; then
  echo "Too many parameters. Usage: file-info [directory]"
  exit
elif [ $# -eq 0 ];then set `pwd`;
fi

if [ -d $1 ]; then
    cd $1
    echo "Processing `pwd`... "
  ls -1 -s
  if [ -filesize=0- ]; then
  echo "Would you like to delete this file? (y/n)"
  read ANSWER
      if [ $ANSWER = "y" ]; then
              rm -filename-
      elif [ $ANSWER = "n" ]; then
      echo "...proceed..."
       fi
    fi
cd   

else
    echo "$1 is not a valid directory... terminating..."
fi

Can someone please give me a hand with this? Thank you.
# 2  
Old 08-12-2008
Code:
find /path/to/files -size 0 -ok  rm {} \;

Pretty much does what you want.

Last edited by jim mcnamara; 08-12-2008 at 04:25 PM..
# 3  
Old 08-12-2008
rm -i `ls -lrt|awk '$5==0{print $9}'`
this will also do
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to delete directories and files with xargs?

Hello, i have an dynamical apache_cache that I need to clean all days (older tant one day) with an unix command : find /usr/IBM/HTTPServer/apache_cache/ -name '*' -mtime +1 -print0|xargs -0 rm -r -- but it didn't work. Could you explain me why. So I will put below all my script :... (13 Replies)
Discussion started by: steiner
13 Replies

2. Shell Programming and Scripting

Delete files in group of directories

OS: SUNOS 5.10 i386 Hello guys I wrote a shell script in bash shell to delete the files less than 30 days old. The following is the script. ======================================= #!/bin/bash for dirs in `/clu04/oracle/directory_list.lst` do find $dirs -type f -mtime -30 -exec rm {} \;... (3 Replies)
Discussion started by: zacsparrow
3 Replies

3. Shell Programming and Scripting

Script to go Into Directories and Find/Delete files

I have a task, I usually do manually, but with growing responsibilities I tend to forget to do this weekly, I want to write a script that automates this, but I cant seem to work it out in my head, I have the shell of it out, but need help, and you guys have helped me with EVERY problem I have... (5 Replies)
Discussion started by: gkelly1117
5 Replies

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

5. Shell Programming and Scripting

How to access files from different directories and to perform search action in those files?

Hi, I want to access files from different directories (for example: /home/dir1/file1 , /home/dir2/file2 ...) Like this i have to access these files(file1, file2...). (3 Replies)
Discussion started by: bangarukannan
3 Replies

6. UNIX for Dummies Questions & Answers

Directories sizes

Hello everyone, can anybody help me in finding a way to obtain a list of all the directories and their sizes. I would like to be able to run this and obtain an output like a tree structure with each branch saying how much space it is taking up . Hope you can point me in the right direction.... (1 Reply)
Discussion started by: gio001
1 Replies

7. Shell Programming and Scripting

Delete files from sub-directories over 7 days

Can any one please help me in deleting all the Files over 7 days from sub-directories A, B, C... Top-Directory Sub-Directory-A File-1 File-2 ..... File-n Sub-Directory-B File-1 File-2 ..... File-n Sub-Directory-C File-1 ... (1 Reply)
Discussion started by: sureshcisco
1 Replies

8. Shell Programming and Scripting

A Batch job to delete files from various directories

Hi, I have a shell script to find files older than 'X' days ($2) in directory path ($1) and delete them. Like this: my_file_remover.sh /usr/home/c 90 Now, I need to modify this script and add it in CRON, so that it checks other directories also. Like: my_file_remover.sh /usr/home/c... (3 Replies)
Discussion started by: guruparan18
3 Replies

9. UNIX for Dummies Questions & Answers

delete pattern files in sub directories

Hello friends, I am compiling some set of SQL scripts in a set of sub directories demoed as below. After compiling log files are being created. Each and every time after compiling time I had to go subdir by subdir to delete the log files. I am sure there should be simple way to look for all log... (4 Replies)
Discussion started by: adurga
4 Replies

10. UNIX for Dummies Questions & Answers

Search for files in multiple directories

I want to search for a file pattern in more than one directory. How I need to do that? Here is the scenario: I am having a directory structure like the following: /log ...../20051001 ..........ftp_server_20051001.log ..........ftp_down_server.log ..........ftp_up_server.log... (7 Replies)
Discussion started by: ravikirankethe
7 Replies
Login or Register to Ask a Question