When displaying directories in Linux using ls -ld, I get something like this:
user@shell:~/somedirectory$ ls -ld
drwxr-xr-x 2014 K-rock users 65536 20011-11-05 11:34
user@shell:~/somedirectory$
How would I find the number of subdirectories and files in the somedirectory
using the result above? From what I understand, the number of links corresponds to the number of subdirectories, but what about the number of files? How would I read the result of the ls -ld
to find those numbers?
Also, this is an assignment and I have to say the number of files and subdir that are in somedirectory using the result shown above. So I can't really use any other code unfortunately.
Best Answer
Since, you want to decipher from the output that you have got, we will try and simplify things.
Now,
ls -ld
on a directory gives me the output as above. Now, the number 4 is something that you need to concentrate on. The 4 corresponds to:.
;..
entries in the 2 sub-directories inside the directory.To verify this, if I just issue
ls
I could see that I have couple of more directories. So, this gives an idea of what we could decipher from the output of your case.There are 2012 sub-directories inside which is why you get 2014 in the output.
As to the number of files, it is not possible to find it out from the output that you have.
To test if my theory is correct, I did the below testing.
Now, I issue
ls -ld
command and the output I get is,It did not take into consideration the files or subdirectories nested inside the subdirectories of the folder. Basically, the above command says I have 10 directories inside my folder.
P.S.: It is often a bad idea to parse something from
ls
output as it is not reliable. Usefind
with-maxdepth
instead if you have a chance to use it.