Oct 12

Using the Find Command in Linux

ccasanova
Christopher Casanova

In a Graphical User Interface (GUI) operating system, searching for files and folders are simple as we often have a search bar, where we can type what we are looking for and have the results displayed for us. The approach is different in a Unix based operating system such as Linux, when you are using a command line terminal. Unix does provide a powerful command named “find” that will you to quickly look for files and folders. It also provides plenty of sorting options and easily allows you to narrow down what you looking for, even if it’s unique. This is very useful if you are using Linux hosting and would like to locate specific directory. Below is an example of how you would use the find command in Unix.

To search for a specific folder’s name, you would type the following in the command line terminal:

find . -type d -name "FOLDER"

Where you would replace “FOLDER” with the name of the folder you’re looking for. One example would be the following:

find . -type d -name "log"

Now the syntax for this command is as follows.

find        –    the name of the command.
.            –    the current directory that you are in and all its subdirectories.
-type d  –    an option that let’s you specify what kind of file you are looking for. In this case, we use ‘d’ to mean look for a directory.
-name   –    an option that specifies that you are looking for a file with a specific pattern (a file name)
“log”      –    indicates we a looking for a directory file named log. The section uses regular expressions to match a pattern.

Now a couple tips for more advanced users. Within your Linux hosting environment, often times you would need to change the permissions of files and folders to provide content to specific users. I will show you a couple ways you can use the find command along with other commands such as chmod to do so.  For example, a user may want to  look into his folder, which has more folders and files inside it, and change the permissions of all directories or all the files. You simple would not be able to run a command like chmod -R 775 * . As this command will make both files and folders have the permissions 775.  To change the only the directories to permissions 775, use the following:

find -type d -print0 |xargs -0 chmod 755

If you wish to change the only the files to let’s say 644, you would use the following command.

find -type f -print0 |xargs -0 chmod 644

I hope that these commands that I have showed you help make searching a bit easier for you in your Linux based environment.

Christopher Casanova is a systems engineer for websoul . To view further information and helpful tips on system administration, view his blog at Chris’ websoul blog.

Leave a Reply