Friday, November 4, 2016

How to find files on command line of Ubuntu

Finding files on graphical mode is easy, but what about finding files on command line? how to do that? in this article i will show you how to find files on command line of ubuntu.
There are two commands for finding files on the command line, the 'locate' and 'find' command, here's how to use it:

Searching files using find command
The 'find' command is a powerful tool for searching files and folders on the command line, here's the syntax and the example:

find [the-directory-path] -name [the-filename-or-pattern]

Example:

find . -name "*png"

find . -name "eva.jpg"

find /var/www/html/ -name "index.html"

find /tmp/ -name "anything"

Searching files using locate command
The 'locate' command is the faster way of searching files on command line, 'locate' is fast because it's using some kind of database index to find the files, the database is generated daily. You can use 'locate' command like this:

locate [pattern_of_the_file]

Example:

locate myfile.txt

locate *.txt

locate *.mp4

locate my-private-video.avi

The 'find' command maybe slower than 'locate' command, but it is more reliable, because unlike 'locate' it can track recent files activity. The problem with 'locate' is that it depends on the database which updated daily, so 'locate' won't be able to track new files or recently moved files, you have to wait until the next day if you want to use 'locate'.

Although there is a way to force 'locate' to update the database, so it can immediately track any new files changes, but this is not practical. For the most part i suggest using the 'find' command, it maybe slower but it is definitely more reliable.