Professional Documents
Culture Documents
Locate and Find
Locate and Find
What if we only want to return files with names that don’t contain a certain string? Then we will use:
If you want to search for files by type, you can do so with the following command:
f: Regular file
d: Directory
l: Symbolic link
c: Character devices
b: Block devices
find -type f
You can find files based on access time (-atime), modified time (-mtime), and change time (-ctime)
flags.
Let’s find a file modified more than 5 days ago:
find . -ctime +5
find . -ctime -1
The -user and -group flags can be used to find a file located by a specific user or group
find -user mc
Find can filter files based on their size. SImply use the -size flag with the following size conventions:
c: Bytes
k: Kilobytes
M: Megabytes
G: Gigabytes
b: 512-byte blocks
In order to find a file that is exactly 1GB in size, simply type in the phrase:
find . -size 1G
Locate
In some Linux distribution locate command may not work directly then we have to install it using
below command
Sudo apt install mlocate
Locate apache
locate filename
NOTE: If you have just created a new file, you need to update the locate database as shown before
searching for the file.NOTE: If you have just created a new file, you need to update the locate
database as shown before searching for the file.
sudo updatedb
If the output is a little overwhelming and perhaps you want to view the first N search queries, then
execute the command below. Here, we have limited the output to only 20 search results.
$ locate apache -n 20
To gather more insights on the locate database – mlocate.db – run the following command:
$ locate -S
This prints out the number of files and directories indexed by the database, among other details.
$ man locate