How To Find A File Or Directory Quickly In Linux/Unix?

Searching a file manually in Linux or Unix can be a little bit challenging task especially for new users. Linux/Unix operating systems provide various command-line tools that can be used to accomplish this easily.

In this article, we are going to discuss some of these tools also we will see their usage with example.

Find command-

Find is a powerful utility that searches a file based on some user-specified criteria it supports multiple options for searching for a file or directory. It starts searching from a mentioned starting point that can be the current working directory or some other location in the filesystem. And then it recursively traverses the directories under that location. Look at some examples that are given below-

1. Find a file under the current working directory-

find . -name sample.txt

2. Find a file under the home directory –

find /home -name sample.txt

3. Find a directory by its name under the home directory –

find /home -type d -name sample

4. Find all the files with .sh extension under the home directory-

find /home -name "*.sh"

5. Find all files having permission 777 under the current working directory-

find . -type f -perm 0777 -print

6. Find and remove a file-
By using the ‘-exec’ other UNIX command can be executed on files and directories.

find -type f -name "sample.txt" -exec rm -f {} \;

7. Find and remove all files with .mp3 extension under home directory-

find /home -type f -name "*.mp3" -exec rm -f {} \;

8. Find all empty files under a certain path-

find /home -type f -empty

9. Find all the empty directories under the home directory-

find /home -type d -empty

10. Find files based on modification date (files modified in last 25 days)-

find / -mtime 25

11. Find files based on their size(finding files between 100 and 500 MB)-

find / -size +100M -size -500M

You can see this man page to find more options that can be used and the actions that can be performed on a file or directory by using it with the find command.

Locate command –

locate is also a Unix utility that is used for searching a file by its name. locate operates significantly faster and better than the find command because instead of traversing every directory under certain path it searches the given file through the prebuilt database of files that are generated by updatedb command. This database contains the parts of the files and their corresponding path in the system. locate requires regular updating of the database.
Look at some example of using locate command –

1. Find a file –

locate hello.txt

2. Refresh the mlocate database-
The database in which locate searches for a file is called mlocate. It needs regular updates so that it can perform accurate searches. To update this database use the following command-

sudo updatedb

3. To find the files with .txt extension (Limit it to specified number)

locate "*.txt" -n 20

4. Searching file name ignoring its case-
Use the -i option with locate command to ignore the case sensitivity

locate -i sample.txt

5. Display only those files that are present in the system-
When you delete or create a file in the system locate database is needed to be updated by running updatedb command. If you don’t update the database after deleting a file it will still display when you search that file using locate command.

locate -i -e *sample.txt*

Apart from these two most used utilities for finding a file or directory, there are some other tools also available like grep that can be used indirectly for locating a file. We are not going to discuss it here because I already discussed it briefly in the Concept of I/O, pipes and filter.

That’s all for now. If you have a query or you would like to say something on the given topic, feel free to write us in the comments below.

1 thought on “How To Find A File Or Directory Quickly In Linux/Unix?”

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Exit mobile version