Ubuntu: How to find a file/directory by name

Updated: January 28, 2024 By: Guest Contributor Post a comment

Introduction

The Ubuntu command-line interface (CLI) is a powerful ally in searching your system for files and directories. Three commonly used tools for file searching are find, locate, and grep.

Finding a file or directory within the vast file system of an Ubuntu operating system can sometimes be akin to locating the proverbial needle in a haystack. Thankfully, with a sound command of specific search commands, this task can become a matter of mere seconds. In this comprehensive guide, we will walk through the use of several command-line tools designed for the purpose of searching for files or directories by name.

Using the find Command

The find command is one of the most versatile and commonly used commands for searching files and directories. Its general syntax is: find [path...] [expression].

find /home/username -name "Your_File_Name.extension"

This command searches for “Your_File_Name.extension” in the “/home/username” directory and all subdirectories.

To search for only directories or only files, use the -type option with d for directories or f for files:

find / -type d -name 'Documents'
find / -type f -name 'config.txt'

Remember to run these search commands with appropriate user permissions to ensure the search is not restricted due to lack of access.

To avoid a deluge of permission-denied messages when executing the find command with sudo permissions, redirect the error output to /dev/null as shown:

sudo find / -name 'secretfile.txt' 2>/dev/null

Using the locate Command

Another effective command is locate, which uses a database updated by updatedb. Being that it searches through a database instead of the live file system, it’s usually faster than find.

The syntax for locate is straightforward:

locate Your_File_Name.extension

In case the database is not updated or you added a new file, update the database with:

sudo updatedb

You can also limit the number of search results presented by using the -n option:

locate -n 10 "*.pdf"

The above command will display up to ten PDF files from the search database.

Using the grep Command

If you’re looking not only for file and directory names but also for contents within files, grep becomes the command of choice:

grep -R "search term" /path/to/directory

By using the -R option, grep searches recursively through the specified directory and outputs any files containing the “search term”.

Graphical Alternatives

For those who prefer a GUI (Graphical User Interface), applications such as ‘GNOME Search Tool’ in Ubuntu come to the rescue.

To open GNOME Search Tool, click on ‘Activities’ and type ‘search’ or press Ctrl+F while in the file explorer.

Finally, remember that the search is only as good as the indexing of your system. Ensure your file system indexes are regularly updated, and utilize the correct user permissions for an optimal search experience.

Combining Tools and Final Tips

Advanced searches can combine the use of find, locate, and grep with general-purpose commands such as awk, sed, and xargs. For instance:

find / -type f -mtime -1 | xargs grep 'search term'

This complex command will search for files modified in the last 24 hours and have them examined for the ‘search term’.

Understanding these foundational search tools within Ubuntu will empower you to navigate the file system with ease and precision. Whether it’s through the CLI or GUI, effective search techniques can save invaluable time and elevate your system mastery to new levels.