Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

delete files excluding zipfiles

 
saikrishna cinux
Ranch Hand
Posts: 689
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,
Anybody have the solution for deleting the all files in a directory excluding zip files
Thanks
Sai
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24211
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Andrew Monkhouse
author and jackaroo
Marshal Commander
Pie
Posts: 11888
203
C++ Firefox Browser IntelliJ IDE Java Mac Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
saikrishna cinux
Ranch Hand
Posts: 689
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 689
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 11888
203
C++ Firefox Browser IntelliJ IDE Java Mac Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
    Posts: 689
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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
    Pie
    Posts: 11888
    203
    C++ Firefox Browser IntelliJ IDE Java Mac Oracle
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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
     
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic