Linux · Lesson

Linux · Lesson

Locate and Find Command in Linux

Add the YouTube embed URL for this lesson in components/linux/linuxLessons.js.

Understanding locate and find commands

Linux provides multiple commands for searching files and directories across the filesystem.

The locate command performs very fast searches using a prebuilt database, while the find command performs real-time searches directly on the filesystem.

These commands are extremely useful for system administration, troubleshooting, file management and automation tasks.

Core search concepts

locate command

Search files quickly using a prebuilt database.

find command

Perform real-time searches with advanced filtering options.

Flexible filtering

Search using names, permissions, size, ownership and timestamps.

Locate command overview

Features of locate

  • Very fast file searching.
  • Uses a prebuilt database.
  • Supports case-insensitive searches using -i.
  • Searches the entire filesystem.

Limitations of locate

  • Database may not include newly created files.
  • Requires updatedb to refresh the database.
  • Cannot target a specific search directory.
  • Some directories like /tmp may be excluded.

Find command capabilities

Search options

  • Search by name or inode number.
  • Filter by file type or permissions.
  • Search by size, user or group.
  • Find empty files and directories.

Advanced usage

  • Execute commands using -exec.
  • Search using access or modification times.
  • Supports real-time filesystem searches.
  • Allows searching inside specific directories.

Key differences between locate and find

locate

Faster searches using a database, but may show outdated results if the database is not updated.

find

Slower but more powerful because it performs live filesystem searches with advanced filters.

Practical Linux examples

The following examples demonstrate different ways to search files and directories using locate and find commands.

1. Locate command basic usage

The locate command quickly searches files and directories using a prebuilt database.

terminal — bash
linux-search-commands
[root@localhost ~]# locate chrony.conf
/etc/chrony.conf
/usr/share/man/man5/chrony.conf.5.gz
[root@localhost ~]#
 
[root@localhost ~]# locate useradd
/etc/default/useradd
/usr/sbin/luseradd
/usr/sbin/useradd
/usr/share/bash-completion/completions/luseradd
/usr/share/bash-completion/completions/useradd

2. Case-insensitive locate search

Use the -i option to perform a case-insensitive search.

terminal — bash
linux-search-commands
[root@localhost ~]# locate -i UserAdd
/etc/default/useradd
/usr/sbin/luseradd
/usr/sbin/useradd
/usr/share/bash-completion/completions/luseradd
/usr/share/bash-completion/completions/useradd
[root@localhost ~]#

3. Update locate database

Use updatedb when newly created files are not found by locate.

terminal — bash
linux-search-commands
[root@localhost ~]# locate newfile.html
[root@localhost ~]#
 
[root@localhost ~]# updatedb && locate newfile.html
/root/newfile.html
[root@localhost ~]#

4. Find command using file name

Search files and directories by name using find.

terminal — bash
linux-search-commands
[root@localhost ~]# find /etc -name chrony
/etc/logrotate.d/chrony
[root@localhost ~]#
 
[root@localhost ~]# find /etc -iname cHroNy
/etc/logrotate.d/chrony
[root@localhost ~]#

5. Find using inode number

Search files using inode numbers.

terminal — bash
linux-search-commands
[root@localhost ~]# find / -inum 17232604
/etc/passwd
[root@localhost ~]#

6. Find by file type

Search files based on type such as regular files, directories or symbolic links.

terminal — bash
linux-search-commands
[root@localhost ~]# find /usr/libexec -type l
/usr/libexec/gawk
/usr/libexec/arptables-helper
/usr/libexec/sudo/libsudo_util.so
/usr/libexec/platform-python
/usr/libexec/platform-python3.9
[root@localhost ~]#

7. Find by file size and empty files

Search files using file size or empty status.

terminal — bash
linux-search-commands
[root@localhost ~]# find /etc -size +2M
/etc/selinux/targeted/policy/policy.33
/etc/udev/hwdb.bin
[root@localhost ~]#
 
[root@localhost ~]# find /etc/firewalld -empty
/etc/firewalld/helpers
/etc/firewalld/icmptypes
/etc/firewalld/ipsets
/etc/firewalld/services
[root@localhost ~]#

8. Find by permissions and ownership

Search files based on permissions, user ownership or group ownership.

terminal — bash
linux-search-commands
[root@localhost ~]# find /home -perm 644
/home/john/.bash_logout
/home/john/.bash_profile
/home/john/.bashrc
[root@localhost ~]#
 
[root@localhost ~]# find / -user john
/var/spool/mail/john
/home/john
/home/john/.bash_logout
/home/john/.bash_profile
/home/john/.bashrc
[root@localhost ~]#

9. Execute commands using find

Run commands on files found using the -exec option.

terminal — bash
linux-search-commands
[root@localhost ~]# find / -group john -exec cp -rf {} /tmp '\;'
find: ‘/proc/1921/task/1921/fd/6’: No such file or directory
find: ‘/proc/1921/task/1921/fdinfo/6’: No such file or directory
[root@localhost ~]#

10. Find using time attributes

Search files based on access, modification or change time.

terminal — bash
linux-search-commands
[root@localhost ~]# find /usr/sbin -atime 1
/usr/sbin/alternatives
/usr/sbin/groupadd
/usr/sbin/ping
[root@localhost ~]#
 
[root@localhost ~]# find /usr/include -mtime 1
/usr/include
/usr/include/openssl
/usr/include/mysql
[root@localhost ~]#
 
[root@localhost ~]# find /etc/cockpit -ctime 1
/etc/cockpit/ws-certs.d
/etc/cockpit/ws-certs.d/0-self-signed-ca.pem
[root@localhost ~]#

Best practices for file searching

Efficient searching

  • Use locate for quick searches.
  • Run updatedb regularly for accurate results.
  • Use find when advanced filtering is required.

System administration

  • Use find carefully on large filesystems.
  • Combine find with -exec for automation tasks.
  • Redirect errors when searching under /proc or /sys.

Practice tasks for your Linux lab

  • Search for configuration files using locate.
  • Practice case-insensitive searches using locate -i.
  • Create a file and test it using updatedb.
  • Search files by type, size and permissions using find.
  • Use find -exec to copy or manage files.
  • Practice time-based searches using -atime, -mtime and -ctime.

In upcoming lessons, you will continue learning Linux filesystem management, permissions and shell automation concepts.