Find and Locate Command with Example

by Jay

Table of Contents

What are the commands to search files and directories in linux?

To search files and directories there are two commands.

(i) # locate
(ii) # find

Explain the locate command and how to use it?

Locate always looks at the locate database and not in a specific location. The data of the locate is stored in /var/lib/mlocate/mlocate.db file. If the data is not updated in locate database or the locate database is available or locate database is deleted, we cannot locate the files and directories. # updatedb is the command to update the locate database. locate database cannot find the newly created files and directories. It is not recommended to use on production servers because it impacts the performance of the servers. So, to overcome this problem we normally use the # find command on production servers.

To update the locate database

# updatedb 

To search the specified file or directory using locate command

# locate <file name/directory name> ()

Explain the find command and how to use it?

The find command is a powerful utility in Linux/Unix systems that allows you to search for files and directories in a directory hierarchy based on various criteria. It can search by file name, file type, size, permissions, and much more.

The basic syntax of the find command is as follows:

find [path] [expression]

Here, path is the directory in which you want to start your search, and expression is the search criteria you want to use.

Syntax:

# find <location><options><file or directory>

The options are,

-type: Search for files of a specific type, such as f for regular files or d for directories.
-name: Search for files or directories with a specific name.
-prem: search for permissions
-size: Search for files of a specific size, such as -size +10M for files larger than 10 megabytes.
-user: Search for files owned by a specific user.
-uid:  search for files/directories of uid)
-gid:  search for files/directories of gid)
-group: search for the group owner
-empty: search for empty files
-amin: searches for files that were last accessed n minutes ago.
-mmin: searches for files that were last modified n minutes ago.
-cmin: searches for files that were last changed n minutes ago.
-atime: search for access day (access day, minutes, hrs, ...etc)
-mtime: Search for files modified within a certain time period, such as -mtime -1 for files modified within the last day.
-ctime: search for change day (permissions, .....etc)

Examples :

To search for file names in the root directory:
find / -name <file name>
To find file names only in the root directory:
find / -name <file name> -type f
To find directories with lowercase names only in the root directory:
find / -name <directory name> -type d
To search for files/directories with a specified name (case-insensitive) in the root directory:
find / -iname <file/directory name> -type d
To search for empty files or directories in the root directory:
find / -empty
To search for empty files only in the root directory:
find / -empty -type f
To search for empty directories only in the root directory:
find / -empty -type d
To search for .mp3 files only in the root directory:
find / -name "*.mp3"
To search for files/directories with exact 10MB size in the root directory:
find / -size 10M
To search for files/directories smaller than 10MB in the root directory:
find / -size -10M
To search for files/directories larger than 10MB in the root directory:
find / -size +10M
To search for files/directories owned by the ‘student’ user in the root directory:
find / -user student
To search for files/directories owned by the ‘student’ group in the root directory:
find / -group student
To search for files/directories owned by the ‘student’ user and not owned by the ‘student’ group in the root directory:
find / -user student -not -group student
To search for files/directories owned by the ‘student’ user or owned by the ‘student’ group in the root directory:
find / -user student -o -group student
To search for files/directories which belong to the user having the specified user id:
find / -uid <uid no.>
To search for files/directories which belong to the group having the specified group id:
find / -gid <gid no.>
To search files/directories which are having the permissions 755:
find / -perm 755
To search files/directories which are having permissions below 755 and also at least one match also:
find / -perm -755
To search for files/directories which are modified within 20 minutes (above 20 minutes is represented by +20 and below 20 minutes is represented by -20):
find / -mmin -20
To search files/directories which are modified within 2 days:
find / -mtime 2
To search all .mp3 files and delete them:
find / -name "*.mp3" -exec rm -rf {} ;
To search all mp3 files and copy them into /ram directory:
find / -name "*.mp3" -exec cp -a {} /ram ;
To search student user’s files and directories and copy them into /ram directory:
find / -user student -exec cp -a {} /ram ;
To search files/directories which do not belong to any user and move them into /home/ram directory:
find / -nouser -exec mv -a {} /home/ram ;
To search for the 10 biggest files in the root directory:
du -h / | sort -rh | head -n 10

Related Posts

Leave a Comment

* By using this form you agree with the storage and handling of your data by this website.