So ls can only search in current directory and locate can find anything anywhere.
Almost, but not quite....
The
ls
command will only list things on a specified path.
So for example:
ls *.doc
searches the current directory for all .doc files.
Whereas:
ls /path/to/somedirectory/*.doc
will list all .doc files in a directory called
/path/to/somedirectory
So
ls
requires you to specify a path. If no path is specified, then it will act on the current working directory.
There are a few other options that control the behaviour of the
ls
command and control its output. To see all of the options for
ls
- refer to it's man page via:
man ls
And yes,
locate
can find anything anywhere on the PC, but it relies on a database, which must regularly be updated via the
updatedb
command (via sudo). So it's usually a good idea to run
updatedb
to update the database before running
locate
. But, the
updatedb
command can take a while to rebuild the database - depending on the size of the HD and the amount of files/data on there.
Also, as per my first post in this thread, there is the
find
command, which has an extensive set of options that will allow you to create very complex and powerful searches and is reasonably fast.
Generally, as a rule - you'll use the
ls
command to list the contents of the current working directory, or some other specific directory.
If you don't know where something is - then use
locate
, or
find
.
If you're searching for something that you
know has been on your machine for some-time, then
locate
is a very quick and convienient way of finding things - because for an older file, it might not matter if the database is not completely up to date. But if your database is out of date - it's often quicker to use the
find
command than it is to use
updatedb
and then
locate
.
I hope this helps!