Question about ls command and locate

ex4722

Member
Joined
Oct 14, 2020
Messages
42
Reaction score
16
Credits
344
I type in ls *.doc to find all docs in my directory but somehow it says cannot access *.doc: no such file or directory. Im trying to use the locate command and somehow its not working for me. Does anyone know why its not working for me, im following a gudie and somehow its working for them.
Thank you in advanced
 


So what it's telling you is that it is not finding any file or directory with the extension .doc in your CURRENT DIRECTORY.
Do 'pwd' to find out what directory you're currently in. My guess is that it's not the directory where you've put your .doc files....

keith
 
@ex4722 remind us what Linux distro you are using, so that we can tailor your answer to suit. You should always mention that either in your subject title or in your first post.

On locate, whatever the distro, you first need to establish it is installed, it relies on a package named

mlocate

I a Debian-based distro, I would type in

Code:
apt-cache policy mlocate

and subject to the output, install it if necessary.

Once installed, the first time you use it, you need to update its database

Code:
sudo updatedb

before using it.

If files have been added during your current session and then you need to find where they were put, first repeat the above command.

Cheers

Wizard
 
Also there is the find command.
Which can be used like this:
find {search path} {options} {search pattern}
So for finding all .doc files:
Bash:
find ./ -type f -iname "*.doc"
In the above, the search path is the current working directory, or ./
The options we have used are: -type f, which means that we’re interested only in files. And -iname "*.doc", which means do a case insensitive search for objects where the name ends with .doc. And because it’s case insensitive it will also find files which end with .DOC, .Doc, .dOc or any other insane combination of upper/lowercase letters making the word doc.

And we haven’t specified a general search pattern, we’ve just used -iname’s pattern.

So overall, the above find command says:
Perform a case insensitive search of the current directory for all files with names ending in .doc.

And the find command will recursively search the current directory for all files ending with .doc.

And by recursively, I mean it will search the current directory and ALL sub-directories.

Find searches recursively by default. To limit the depth of the search, you can use its -depth option. The depth option must be specified before the search path.

There are a number of other options available in find, which can allow you to perform extremely complex searches in the terminal. You can even specify actions to take when files are found.
 
Thank you guys for your answers but i think i didn't explain my problem correctly in my first post. Im suing a Ubuntu btw. Here the thing im following the edx course and it says to type ls *.doc to find all docs in this current directory but for some reason i get the result "
ls: cannot access '*.doc': No such file or directory". This confused me cause it seems that im getting the result for the ls command when you try to list the contents of a directory that does not exist.
Sorry for not explaining myself. But the funny thing is i did run into the problem in which the find locate command was not working so i did have to install it in the end.
 
If you do 'ls *.doc' and you get that error, it means that there are NO FILES with the .doc extension in your current directory. So, either you've not created any .doc files, or you've created them, but they are not in the directory in which you are currently located.
For instance, if I do 'pwd' and it shows /home/eggbert and I do 'ls *.doc' and get the error, then there are no files with the extension .doc in /home/eggbert. So perhaps the files were not created, or they were created in another directory, or they are created in /home/eggbert, but that is not your current directory?
One thing to do is just a straight 'ls' command. This should list out everything in your current directory, oncluding any .doc files that are there...I suspect that none will show up.

keith
 
Here is an example from my environment, of what @khedger is referring to.

I am writing this from Debian 10.7 'Buster' Xfce

Code:
chris@Buster-Xfce-HDD:~$ ls *.doc
ls: cannot access '*.doc': No such file or directory
chris@Buster-Xfce-HDD:~$ locate *.doc
/usr/lib/python2.7/pdb.doc
chris@Buster-Xfce-HDD:~$ cd /usr/lib/python
python2.7/ python3/   python3.7/
chris@Buster-Xfce-HDD:~$ cd /usr/lib/python2.7/
chris@Buster-Xfce-HDD:/usr/lib/python2.7$ ls *.doc
pdb.doc

In that 3rd command, I have part-typed and then used Tab Complete to get the options to complete the path.

Once I am in a directory/folder which has .doc files, the command works.

Cheers

Wizard
 
NO FILES with the .doc extension in your current director
So this confused me a lot cause if i use the locate *.doc instead of ls it list all the .docs i have in the directory. I added a screenshot to show whats happening
1607353752685.png
 
Okay, so you are currently located in /home/ex4722 which contains no .doc files. Hence, 'ls *.doc' gets nothing. Locate shows the .doc files in /snap/gnome ... <rest of path>.
No mystery here. If you want the 'ls *.doc' to return the .doc files, you would need to 'cd ...' into the /snap/gnome...' directory then issue the 'ls *.doc' command. The 'ls' command is only going to return files found in the CURRENT WORKING DIRECTORY.
Does that answer the question?
 
My apologies....upon reading my last response I can see how it might seem to be kind of nasty......it was not meant to have that tone at all. Truly.
Thanks a bunch that made lots of sense. So ls can only search in current directory and locate can find anything anywhere. BTW the is definitely the most wholesome and beginner friendly community i have seen, other places love making fun of noobs . Ehem looking at you discord
 
We are an eclectic bunch @ex4722 but I think its fair to say we all mean well. I couldn't even read any -ve tone in @khedger post. Sometimes it is tricky to gauge that on here at least it is for me.
 
Sometimes it is tricky to gauge that on here at least it is for me.

That's because you are orbiting Planet Andy, where things move in mysterious ways. ;)

Seriously Eddie, Brian (@Condobloke ) and I came from a place that was like a concentration camp or prison. This place is like a breath of fresh air in comparison, hence we have "hung our hats here" for over 3 and a half years. No cyber-bullying, and if there is, I will be the big foot that stomps on it. :D

Why does your cd command list 3 directories when cd only changes your current one?

Tab Completion, have a read here, it is handy

https://www.howtogeek.com/195207/use-tab-completion-to-type-commands-faster-on-any-operating-system/

But it helps to know a bit of the path you are headed for. In /usr/share/ , or similar, you might find

Code:
chris@TaraXFCE-WD:~$ cd /usr/share/
Display all 308 possibilities? (y or n)

Cheers

Wiz
 
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!
 
Last edited:
Good catch JasKinesis. I was so focused on the poster's particular situation that I mispoke....thanks for clarifying.....

keith
I don't think there was anything wrong with what you said.
The reason the user was getting the error message was because there were no .doc files in the current working directory. That was 100% correct.

I didn't even mention, or address that point, because that was already dealt with.

I was just offering some clarification to the OP on the differences between ls, locate and find. And some context on when you might choose to use one over the other.
 


Top