shell script probelm!!

cat123

Inactive User
Joined
Dec 3, 2006
Messages
134
Reaction score
1
Hi

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'm just strating out doing Linux and I've hit a brick wall with this so any help would be appreciated, cheers.
 
Hi mate. The best way to o it is with the 'find' command. Have a look at the documentation for it (i.e. man find).
Anyway, the following should do the job:
Code:
You don't have permission to view the code content. Log in or register now.

You can replace 'path_to_top_directory' with either a full path (e.g. /home/geoff), or just a '.' if you want to search with the current directory.
I really would recommend you learn all the ins and outs of the find command. It's very powerful.

And welcome to Linux.:Clap::Clap::Clap:
 
Cheers for the reply m8 I'll give it a go.

Hope to get it together.
 
To get it to ask you if it is ok to delete the files first, you could try this:


find 'path_to_top_directory' -size 0 -ok command rm -i {} \;

This should give you a y/n prompt when trying to delete.
 
I think you mean
Code:
You don't have permission to view the code content. Log in or register now.
but it achieves the same thing, it's just that the -i (interactive yes/no) switch for 'rm' is replaced by the -ok switch in 'find'. Six and two threes really. That's the great thing about unix. There's always more than one way to do it.
 
Hi

I tried that script and it worked excellently but I would like to add a few more lines, for example echo file has been deleted or if I decide not to delete, echo file has not been deleted.

Any help would be fantastic guys.

Cheers
 
I would like to add a few more lines, for example echo file has been deleted or if I decide not to delete, echo file has not been deleted.
Well in that case, you'll probably have to run a multi-line script within the '-exec' construct. You can do it by actually 'execing' the shell. So something like this:
Code:
You don't have permission to view the code content. Log in or register now.
If you want to re-ask if you don't get a 'n' or 'N', then you'd be best doing that in a 'case' statement. I'll leave that to you.
 
Cheers for that Beady I'll give it a go m8.

Thanks
 
Back
Top