Linux find command β part 1
In a previous post I talked about the locate command. Now it’s time to write about the find command and how it can help us find files and directories.
I will cover this topic in a couple of blog posts.
In this one I will present some basic usage of the find command and move to more complex searches in the next posts.
find
The find command is one of the most used commands in Linux operating systems.
It can be used to search for files and directories based on different criteria such as size, type, permission, date etc.
find command has the following syntax:
find [path] [options] [file name]
[path] it defines the starting directory/directories where the command will do the search
find / will start the search from root directory
find ~ starts the search from the /home/user/ directory
find . will search in the current working directory
[options] it is used to search for a specific file based on name, size, type etc
All these attributes are optional, you can use them according to your needs.
Running find –help in the command line will list very basic usage information about this command.
Also a good starting point is the manual ( man find in terminal) or the manual page.
Ok, now it’s time to see the find command at work
Find files by name
Searching for files by name is one of the most common use of find. In order to run this command we will use the -name option followed by the name of the file.



You can also search in other directories while still in the current working directory by providing the path for that directory.
Find files by extension
The -name option is used as well for searching files by extension.


Find files by type
Keep in mind that everything is a file in Linux
We can search for files based on their type by using the -type option and a descriptor to specify the file type.
The role of the descriptor is to specify the type of file we are searching (a regular file, a directory etc).
-type f is used to search for a regular file

-type d when searching for a directory

Other descriptors are:
-type l for symbolic link ( is a special type of file that points to another file or directory ).
-type c for character devices
-type b for block devices
-type p for named pipe (FIFO)
-type s for socket
Find files by size
One other method of searching files is by size. We can use the -size option along with the size criteria.
-size b 512-byte blocks (this is the default)
-size c for bytes
-size w for two-byte words
-size k for Kilobytes
-size M for Megabytes
-size G for Gigabytes
Let’s see how we can use these options.

In the above example, I have search for a file with the exact size of 165 bytes. But if we don’t know the size of the file we can approximate.
We can search for a file that has a size under or over a certain value. We do this by using the – sing for less than and + for more than.


Also we can search for files that are in a size range

This is it for now. In the next post about the find command I will show you how to search for files based on the creation date, owner and permission.
