aspose file tools*
The moose likes Linux / UNIX and the fly likes delete files excluding zipfiles Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » Linux / UNIX
Bookmark "delete files excluding zipfiles" Watch "delete files excluding zipfiles" New topic
Author

delete files excluding zipfiles

saikrishna cinux
Ranch Hand

Joined: Apr 16, 2005
Posts: 689
Hi All,
Anybody have the solution for deleting the all files in a directory excluding zip files
Thanks
Sai


A = HARDWORK B = LUCK/FATE If C=(A+B) then C=SUCCESSFUL IN LIFE else C=FAILURE IN LIFE
SCJP 1.4
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34

Like everything else, I'm sure there's more than one way to do it, but the first thing that occurs to me is

find . -type f -not -name '*.zip' -exec rm \{\} \;

Which deletes (-exec rm \{\} \ all the plain files (-type f) whose name doesn't end in zip (-not -name '*.zip') .

You have to type every single character of this exactly as given, including the backslashes and braces and even that semicolon.


[Jess in Action][AskingGoodQuestions]
Andrew Monkhouse
author and jackaroo
Marshal Commander

Joined: Mar 28, 2003
Posts: 11424
    
  85

Note that Ernest's solution will not only delete files in your current directory, but in all directories included within it.

So if you are currently in /home/saikrishna and you have a directory /home/saikrishna/dont_delete then everything in that second directory will get deleted as well.

The -maxdepth option will allow you to restrict how deep to go:

find . -type f -maxdepth 1 -not -name '*.zip' -exec rm \{\} \;

For example:

Regards, Andrew


The Sun Certified Java Developer Exam with J2SE 5: paper version from Amazon, PDF from Apress, Online reference: Books 24x7 Personal blog
saikrishna cinux
Ranch Hand

Joined: Apr 16, 2005
Posts: 689
Originally posted by Andrew Monkhouse:
Note that Ernest's solution will not only delete files in your current directory, but in all directories included within it.

So if you are currently in /home/saikrishna and you have a directory /home/saikrishna/dont_delete then everything in that second directory will get deleted as well.

The -maxdepth option will allow you to restrict how deep to go:

find . -type f -maxdepth 1 -not -name '*.zip' -exec rm \{\} \;

For example:

Regards, Andrew


Ernest Friedman,Andrew
I got a very good solution from you guys!
Thanks so much!
saikrishna cinux
Ranch Hand

Joined: Apr 16, 2005
Posts: 689
Originally posted by Andrew Monkhouse:
Note that Ernest's solution will not only delete files in your current directory, but in all directories included within it.

So if you are currently in /home/saikrishna and you have a directory /home/saikrishna/dont_delete then everything in that second directory will get deleted as well.

The -maxdepth option will allow you to restrict how deep to go:

find . -type f -maxdepth 1 -not -name '*.zip' -exec rm \{\} \;

For example:

Regards, Andrew


Andrew,
May i know why we are using this -exec rm at the end of the line ?
what for we will use exec?
i tried to read in man pages but not clear with the information they have provided
can you please explain me when and where we have to use - exec ?
Thanks,
Andrew Monkhouse
author and jackaroo
Marshal Commander

Joined: Mar 28, 2003
Posts: 11424
    
  85

This is a longish, roundabout answer ...

The find command is designed to find files (maybe that is why it is called "find"). Its first parameter is the directory where you want to start searching, then everything after that is some check (sometimes combined checks) that must be true before "find" will print the name of the matched file. For example, the first parameter in both Ernest and my examples is the "-type f" option - it will only be true if the thing found is a regular file (so it wont try to match a directory with later arguments).

What we have then is:
  • -type (if the type matches)
  • -not (and if the next argument doesn't match)
  • -name (if the name matches)
  • -exec (and if the command executed returns zero)



  • Then do <something> (where something is often "print").

    One thing to remember is that find is meant to find files that meet certain criteria. The -exec parameter is designed to help you with that: find will execute any command and use it's output to decide whether to continue on down the list of requirements.

    To make that a little clearer, imagine using the "grep" command, which returns zero for successfully finding a match, and one if it didn't find a match. Such that:
    The first 2 lines were just setting up a test file.

    The 3rd line is searching for text that I know exists.

    The 4th line uses the "$?" to print out the exit status of the previous command. So I can use this to show that grep does indeed return zero if it matches.

    The 5th line is searching for text that I know does not exist.

    The 6th line just shows that if grep does not find the text then it returns one.

    So we can use this in our find command:

    Now this example was a little useless, since grep can give me the same output for much less work, but how about a far more complex example:

    Start off by creating 2 files (the Java source and the compiled class):
    Now lets make a far more complex problem to solve:
  • Find source files only
  • That I own
  • That contain the string "Hello"
  • That have been modified today

  • :
    A sample to do this could be:


    So - that explains how find works, in a roundabout way.

    Back to your question. You originally wanted to delete files. But find is all about finding files - not deleting them. However since any command can be used with the "-exec" parameter, then we can put the rm command in there to remove the files that match all our other requirements.

    There are, of course, many ways to do what you originally requested. Even just starting with the find command we could do alternatives. For example:

    Once again - man is your friend. Try doing a man on xargs and a man on rm.

    Regards, Andrew
    saikrishna cinux
    Ranch Hand

    Joined: Apr 16, 2005
    Posts: 689
    hi Andrew,
    Your solutions are very nice!
    but there is still some problem with this command
    The shell i am using is ksh , here it is not acception "-not" option for find command.
    is ther any other way to do so?
    Thanks
    Andrew Monkhouse
    author and jackaroo
    Marshal Commander

    Joined: Mar 28, 2003
    Posts: 11424
        
      85

    The 'find' command is not a shell built-in command - it is a standalone executable. So the shell you are using is irrelevant in this case.

    What is relevant is either what version of Linux or Unix you are using (I suspect you are not using Linux), and what version of the 'find' command you are using.

    If you type `uname -a`, we should be able to see what OS and version you are using. Examples from 2 different machines:


    To find out which version of find you are using, you can try the 'which' command:


    A more accurate way of getting this would be to follow any symbolic links:


    Another thing you could try is using the '!' symbol instead of the 'NOT' string, e.g.:


    You could also look in the man pages to see what it suggests.

    Regards, Andrew
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: delete files excluding zipfiles