frame

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Sign In Register

[Linux] How to find a File/Files Using Linux Terminal

Finding files on a Linux system can be a daunting task, especially when dealing with vast directories and complex file structures.
The traditional find command, while powerful, can be slow when searching through large filesystems.

This is where the locate command comes in, providing a much faster and more efficient way to locate files almost instantly. By relying on a pre-built database, locate eliminates the need for real-time searching, making it a great tool for users who frequently need to find files quickly. Whether you're a beginner or an experienced Linux user, mastering the locate command can significantly improve your productivity and efficiency.

What is the locate Command?

The locate command is a utility in Linux that helps users find files by searching a database instead of scanning the entire filesystem. This makes it significantly faster than the find command, which searches directories in real-time.
The locate database is updated periodically using the updatedb command, ensuring that searches remain efficient and up to date.

By default, Linux does not come with the locate command pre-installed. To get the package, run the following commands one after another:

sudo apt-get update
sudo apt-get install mlocate

To use locate, simply type locate filename, and it will return a list of all matching file paths. While locate is fast, it may not always reflect the latest file changes, so other alternatives such as find, fd, and ripgrep may be useful depending on the scenario.

  • find command: The find command searches directories in real-time, providing the most accurate results but at the cost of slower performance.
  • fd command: A user-friendly alternative to find, fd offers color-coded output, smart search defaults, and better performance.
  • Ripgrep (rg): Although mainly a text-search tool, ripgrep can also be used to find file paths efficiently, making it a powerful alternative in certain cases.

1. find command syntax

find [directory] -name "filename"

Here's the example on how to find simple document file:

find /home -name "document.txt"

This searches for document.txt within the /home directory.

Using Find to Search by Type

  • d – directory or folder
  • f – normal file
  • l – symbolic link
  • c – character devices
  • b – block devices

Using Find to Search by Time

  • Access Time (-atime) – when the file was either read or written into.
  • Modification Time (-mtime) – when the file was modified.
  • Change Time (-ctime) – when the file’s meta-data was updated.

Using Find to Search by Size

  • c – bytes
  • k – kilobytes
  • M – megabytes
  • G – gigabytes
  • b – 512-byte chunks

Basic example of using find with size:

find . -size 5M

2. fd command syntax

fd "filename" [directory]

Same as before, example on how to find a singular file:

fd "document.txt" /home

This searches for document.txt in the /home directory using fd.

Using -e to filter by file extension

fd -e txt -e md "notes"

This finds files with .txt or .md extensions that match "notes".

Excluding unwanted directories with --exclude

fd "cache" --exclude node_modules

This ignores node_modules to improve performance.

Using --hidden to include hidden files

fd --hidden "config"

3. ripgrep (rg) command syntax

rg --files | grep "filename"

rg --files | grep "document.txt"

This finds document.txt efficiently within the indexed files using ripgrep.

Using --files for file searching instead of text content

rg --files | grep "report.txt"

4. locate command syntax and examples

locate [filename]

Let's try finding files with few examples:

locate document.txt - Finds all instances of document.txt
locate -i picture.jpg - Case-insensitive search for picture.jpg
locate '*.mp3' - Finds all MP3 files. A "*" before an extension looks for every file with a name which has .mp3 extension.

IMPORTANT: Update the database before searching for the latest results

sudo updatedb

Combining grep for more refined filtering

locate "config" | grep "apache"

Pro-tips

  • / (slash) — search the whole system.
  • . (dot) — search from the folder you’re currently working on (current directory).
  • ~ (tilde) — to search from your home folder.

Conclusion

The locate command is a powerful and efficient tool for quickly finding files on a Linux system. While it offers speed, it is best used alongside other tools like find, fd, and ripgrep for more comprehensive searches. Each tool has its strengths, and knowing when to use which one can greatly improve your efficiency in file management. By combining these commands with best practices, you can optimize your search process and save valuable time.

Sign In or Register to comment.

Time4VPS

Learn how to install a web and database server, email, FTP client or other applications. Discover and share information on server security or optimization recommendations.
Feel free to join our constantly expanding community, participate in discussions, strengthen your knowledge on Linux and Windows server management!
© 2013 - 2025 Time4VPS. All rights reserved.

Get In Touch