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,
author and jackaroo
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:
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.
Joined: Apr 16, 2005
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
author and jackaroo