[HowTo] Master the FIND command on Linux

I don’t know why, but most of people learn first how to use grep command before trying to use find. After seeing a very good topic about find command, I took the habit to use it to find file with specific filter and I just use grep sometime to help me to filter outputs. In this article, I will list out some of the most used Linux find commands in the form of practical examples.

Basic find command

The most basic example that I can give you is the following:

Used like this, find provides you a list of all the files and directories (including sub-directories and their content). Of course, this command is never used like this :-).

Specific a base directory

Of course, you can specify the base (root) directory where to begin the listing. For example, if my current working directory (pwd) is /home/fl0at0xff/Desktop and I want to list /var/log/apt, simply use the find command like this:

Find files using name (-name option)

In the most case, you want to search a specific file. If you know its name, this operation is ridiculously easy. For example,  imagine that I want to search a file named socket.h. If we really don’t have an idea where it could be located, we need to search from the root system directory (/). Please note that such as search must be executed with root access to have the possibility to read system directories.

Of course you can replace the slash (/) in the above command with a specific directory if you know approximately where the file is.

Find files using name and Ignoring Case (-iname option)

Because Linux system is case-sensitive, the previous command will return nothing if you executed the command like this

if you want to ignore the case, simply use the -iname option (-i for ignoring-case)

Personally, I use always this option when I want to search a specific file.

Find directories (-type d option)

Sometimes, you prefer to search a specific directory. Like for the files, you must specify the name of the directory, using -name or -iname option, but it is necessary to specify that we are looking for a directory and not files. This can be done using -type d option. For example, imagine we want to search all log directories in the file system.

Find specific files extension

It could be useful to be able to have a list of all files of a specific type. For example, imagine we want the list of all .pid files. It is not really another option of the find command but simply use the star (*) which means “all”.

For example, imagine you want to find all hidden files on the system. You can use this pattern : “.*” because each hidden files or directories hidden on Linux begin with a dot.

Find files with specific permissions

Sometimes, for security reasons for example, you might want to search all files in your system that have specific permissions. Look this example where we find all system files that have 777 as permissions.

The first number (set here to 0) is the sticky bit. For example, imagine that you don’t want to have files with 0777 permission on your system. Instead of changing one by one the permission, you could combine the find command with the exec one for example:

Find read only files

Read only files here, means that owner= read and write but group and other have only read access.

Find executable files

Another way to find executable files, and probably most usable is to use the -executable parameter.

The main advantage of this way is that -executable takes into account access control lists and other permissions artefacts which the -perm test ignores.

Find and remove files

Imagine you can find and remove all temporary files present on your system. This can be done like this:

I suggest you to always use -ok instead of -exec. The difference is that -ok will ask you for each file before delete it. Imagine for example that you have forget to write the “tilda” (~) in the -iname string and you use -exec. The result is that ALL your files on the system will be deleted. bad.

Find all files based on User

The option -user allows you to search files where the owner is defined by the value of -user option.

Find all files based on Group

Like for the previous one, the option -group is also available.

I hope this small post will help you to change your habits in order to use find instead of grep. Do not hesitate to send me your trick about the find command if you think that this post must include it 🙂

Share on FacebookShare on Google+Tweet about this on TwitterShare on LinkedInShare on RedditShare on StumbleUponShare on TumblrPin on PinterestFlattr the authorDigg thisBuffer this pagePrint this page

2 comments

  1. Very nice tutorial on find. I picked up a couple things to use.

    An alternative for deleting files but which may not be supported in all Linux distros is using -delete instead of -exec rm {} \;

    It is significantly faster, especially as the number of files to be deleted increases.

    1. Hello David. Thank you very much ! I’m happy to know that this [HowTo] was useful for you. I take into account your remark but I will not include it in this post for the compatibility reason and because the performance aspect is out of the scope of this post. But Thank you very much 🙂 Best Regards.

Leave a Reply

Your email address will not be published. Required fields are marked *