find is a very useful command that can help you keep your file system tidy

For example, if you write out a series of automated process log files on a regular basis, it can keep the system clean by tidying up those of a certain age that are no longer required.

Let's go through it's usage stage by stage. Firstly, the basic command:

find <path> 

This will give all files in the specified path given by <path>, including all subdirectories. For example, to find all files in the current user's home directory, you could use:

find $HOME

It's possible to adjust this to give fIles of a particular name pattern, by using the case-sensitive -name or case-insensitive -iname as shown in this example:

find <path> -name <filespec>

Say you want to find all the text files in the current user's home directory, all those that fit the pattern *.log, the following command could be used:

find $HOME -iname *.log

And it should return a list of all .log files. You will also notice it gives all of them, regardless of which subdirectory they're in as well.

But what if you only want those in the current directory and not those further down? This is where the -maxdepth option comes into play.

-maxdepth instructs find to only work within a certain path limit. Not using it will give you everything as far down as possible. Giving it a value of 1 will restrict it to just the current path. A value of 2 will permit the current path, plus one additional level of subdirectories, and so on.

Now we're only interested in the current directory, thus the command will be:

find $HOME -iname *.log -maxdepth 1

This will now list all objects ending .log in the current user's home directory. But you can also notice that if you had a sub-directory with a name of files.log, it too will be listed in the results. In our case we're only interested in files, and find has another option here that can help with that, the -type option.

The -type option takes a value, and while according to the manpages there are many you can give, it's only the f value, that for /files/, that we're interested in.

find $HOME -iname *.log -maxdepth 1 -type f

And now you will only find files are listed.

Alternatively, if you wanted only subdirectories to be listed, changing the value of f for d will accomplish this.

You can see the command is building up in stages quite well. We're nearly there, with only 2 parameters left to go.

Next, we'd like to restrict which files are listed again, this time based on their age. find contains a lot of methods for controlling this, we'll make use of the -mtime option which takes a single value, which is a number of days. For our example a value of 10, that is 10 days old, is being used. We also need to use the plus (+) symbol to tell find to return only those files at least 10 days old.

find $HOME -iname *.log -maxdepth 1 -type f -mtime +10

Running this version will return only those files that end with .log that are in the current user's home directory only and that are at least 10 days old. Excellent, we're almost there.

Finally, one last parameter, this is the action of what we want to happen to these files.

Normally with Linux / BSD systems, you would pipe these results to another command, one that would specifically act on each item in the list. There is no reason why this could not still be done, or if you needed to perform a more complex series of commands on each. For our purposes we no longer need these files and are happy for them to be cleaned up, for them to be deleted.

Fortunately find offers us a very useful parameter, that of -delete, which will delete the files for us. Our final command looks thusly:

find $HOME -iname *.log -maxdepth 1 -type f -mtime +10 -delete 

find is quite a useful command and contains a subtantial number of parameters you can make use of.

Running the command man find will give you access to the manpages for it, or alternatively make use of one of the online manpages like those listed below

Previous Post Next Post