Find and Locate Command
You can search files using the find command. Also find command search files or directories through the entire file system.
I have created a file name as file1.txt at my current location
ubuntu@ip-172-31-25-118:~/A/B$ touch file1.txt
ubuntu@ip-172-31-25-118:~/A/B$ pwd
Output: /home/ubuntu/A/B
Now I am at home location. Run this command
ubuntu@ip-172-31-25-118:~$ find . -name file1.txt
Here . represent it will start finding from the current directory
Output: ./A/B/file1.txt
You can give the type of the file. Means whether you are trying to find a file or a directory.
Eg: find . -type f -name file1
-> It will search file on the basis of simple file.
find . -type d -name file1
-> -> It will search file on the basis of directory.
Locate command is similar to find command but the difference is that locate command search into it's database.
Eg: ubuntu@ip-172-31-25-118:~$ locate file1.txt
Output: Command 'locate' not found, but can be installed with:
sudo apt install plocate
Then login through root and run updatedb
ubuntu@ip-172-31-25-118:~$ sudo su -
root@ip-172-31-25-118:~# updatedb
Output: Command 'updatedb' not found, but can be installed with:
apt install locate # version 4.9.0-5, or
apt install plocate # version 1.1.19-2ubuntu1
Then check if mlocate is installed or not
To check: rpm -qa | grep mlocate
To install: apt install locate
Then run updatedb as a root and run locate command
ubuntu@ip-172-31-25-118:~$ locate file1.txt
Output: /home/ubuntu/A/B/file1.txt
Also it counts how many files are available with same name
Syntax: locate -c filename/directoryname
Subscribe to my newsletter
Read articles from Bilal directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by