Finding Files

Another useful task you should be able to perform on your server is finding files. Of course, you can use the facility that is available to do just that from the graphical interface, but when you are working on the command line, you probably don't just want to start a graphical environment to find some files. In that scenario, use the find command instead. This is a powerful command that helps you find files based upon any property the file can have. For example, you can find files by their names; the access, creation, or modification date; the user who created them; the permissions set on the file; and much more. If, for example, you want to find all the files with names that start with hosts, use find / -name "hosts*". I recommend always putting the string of the thing you are looking for between quotes; this makes sure that find knows where the argument starts and where it stops.

You can also find files based on the user who created them. For example, find / -user "britney" will locate all files created by user britney.

You can also execute a command on the result of the find command by using the -exec option. If, for example, you want to copy all files of user britney to the directory /files, use find / -user "britney" -exec cp {} files \;.In such a construction, you should pay attention to two specific elements used in the command. First there is the {} construction, which is used to refer to the result of the foregoing find command. Next, there is the \; element, which is used to tell find that this is the end of the part of the command that began with - exec.

Was this article helpful?

0 0

Post a comment