As much as I like Oli (which is a lot!) I disagree with him on the
find command. I don't like it.
find command takes over three minutes
Take for example this simple command:
$ time find / -type f -name "mail-transport-agent.target"
find: ‘/lost+found’: Permission denied
find: ‘/etc/ssmtp’: Permission denied
find: ‘/etc/ssl/private’: Permission denied
(... SNIP ...)
find: ‘/run/user/997’: Permission denied
find: ‘/run/sudo’: Permission denied
find: ‘/run/systemd/inaccessible’: Permission denied
It takes over three minutes for
find to search everything starting from
/. By default reams of error messages appear and you must search through them to find what you are looking for. Still it is better than
grep to search the whole drive for a string which takes 53 hours: `grep`ing all files for a string takes a long time
I know I can fiddle with the find command's parameters to make it work better but the point here is the amount of time it takes to run.
locate command takes less than a second
Now let's use
$ time locate mail-transport-agent.target
The locate command takes less than a second!
updatedb only run once a day by default
It is true the
updatedb command which updates the locate database is only run once a day by default. You can run it manually before searching for files just added by using:
$ time sudo updatedb
Although this will take 3 seconds, it's small in comparison to
find command's 3+ minutes.
I've updated my
sudo crontab -e to include the line at the bottom:
# m h dom mon dow command
0 0 1 * * /bin/journalctl --vacuum-size=200M
*/5 * * * * /usr/bin/updatedb
Now every five minutes
updatedb is run and
locate commands database is almost always up-to-date.
But there are no attributes?
You can pipe
locate output to other commands. If for example you want the file attributes you can use:
$ locate mail-transport-agent.target | xargs stat
Size: 473 Blocks: 8 IO Block: 4096 regular file
Device: 10305h/66309d Inode: 667460 Links: 1
Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root)
Access: 2018-03-31 18:11:55.091173104 -0600
Modify: 2017-10-27 04:11:45.000000000 -0600
Change: 2017-10-28 07:18:24.860065653 -0600
I posted this answer to show the speed and ease of use of
locate. I tried to address some of the command short-comings pointed out by others.
find command needs to traverse the entire directory structure to find files. The
locate command has it's own database which gives it lightning speed in comparison.
anacronis for. I don't know if it's installed by default on desktop systems/servers, but it is on notebooks. It runs upon boot and sees if any cron jobs should have run while the system was off and runs them. It's really helpful, but can cause some issues if you have jobs scheduled far away from midnight. That can cause the job to be run upon boot and then again when the time comes up - possibly a lot less than 24 hours later (for a daily job.)