OnWorks Linux and Windows Online WorkStations

Logo

Free Hosting Online for WorkStations

< Previous | Contents | Next >

Predefined Actions

Let’s get some work done! Having a list of results from our find command is useful, but what we really want to do is act on the items on the list. Fortunately, find allows actions to be performed based on the search results. There are a set of predefined actions and sev- eral ways to apply user-defined actions. First let’s look at a few of the predefined actions:


Table 17-6: Predefined find Actions


Action Description

Action Description

-delete Delete the currently matching file.


image

-ls Perform the equivalent of ls -dils on the matching file.

Output is sent to standard output.


image

-print Output the full pathname of the matching file to standard output. This is the default action if no other action is specified.


image

-quit Quit once a match has been made.


image


As with the tests, there are many more actions. See the find man page for full details. In our very first example, we did this:


find ~

find ~


which produced a list of every file and subdirectory contained within our home directory. It produced a list because the -print action is implied if no other action is specified. Thus our command could also be expressed as:



find ~ -print

find ~ -print


We can use find to delete files that meet certain criteria. For example, to delete files that


have the file extension “.BAK” (which is often used to designate backup files), we could use this command:



find ~ -type f -name '*.BAK' -delete

find ~ -type f -name '*.BAK' -delete


In this example, every file in the user’s home directory (and its subdirectories) is searched for filenames ending in .BAK. When they are found, they are deleted.


image

Warning: It should go without saying that you should use extreme caution when using the -delete action. Always test the command first by substituting the

-print action for -delete to confirm the search results.


image

Before we go on, let’s take another look at how the logical operators affect actions. Con- sider the following command:



find ~ -type f -name '*.BAK' -print

find ~ -type f -name '*.BAK' -print


As we have seen, this command will look for every regular file (-type f) whose name ends with .BAK (-name '*.BAK') and will output the relative pathname of each matching file to standard output (-print). However, the reason the command performs the way it does is determined by the logical relationships between each of the tests and actions. Remember, there is, by default, an implied -and relationship between each test and action. We could also express the command this way to make the logical relation- ships easier to see:



find ~ -type f -and -name '*.BAK' -and -print

find ~ -type f -and -name '*.BAK' -and -print


With our command fully expressed, let’s look at how the logical operators affect its exe- cution:


Test/Action Is Performed Only If...

Test/Action Is Performed Only If...

-print -type f and -name '*.BAK' are true


image

-name ‘*.BAK’ -type f is true


image

-type f Is always performed, since it is the first test/action in an

-and relationship.


image


Since the logical relationship between the tests and actions determines which of them are performed, we can see that the order of the tests and actions is important. For instance, if we were to reorder the tests and actions so that the -print action was the first one, the command would behave much differently:



find ~ -print -and -type f -and -name '*.BAK'

find ~ -print -and -type f -and -name '*.BAK'


This version of the command will print each file (the -print action always evaluates to true) and then test for file type and the specified file extension.


Top OS Cloud Computing at OnWorks: