Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

As I understand, the ls command calls getdents, which returns up to x number of directory entries. Are there any other system calls involved? If I run ls -l, are there any more system calls? I am trying to determine if ls -l is more expensive and hence slower than ls.

share|improve this question
3  
You can use strace ls and strace ls -l to see the system calls for each command. –  Timothy Martin Aug 6 at 19:59
    
I ran the strace on a Linux system. I assume the results are identical for a Unix system. –  user100503 Aug 6 at 20:07
1  
It doesn't matter which is more efficient, because efficiency only matters in automation and, since ls is not intended for automation, there are always more efficient and safer ways to illuminate the filesystem for programming purposes. –  kojiro Aug 7 at 12:46

2 Answers 2

up vote 15 down vote accepted

/bin/ls usually sorts the output. I'm not sure if your "efficient" question is just over system calls or the entire work that is done, but /bin/ls -f would probably do the least work. It only returns the filenames in directory order. No sorting, no additional inode lookups to get metadata (as ls -l would do).

Also, if your default ls is colorizing, it may be doing the equivalent of ls -l anyway so that it can tell how to color the output.

share|improve this answer
    
Thank you very much! –  user100503 Aug 6 at 22:29
5  
ls -l performs a stat for every single file. It need inodes for each file in order to get informations such as size, permission or number of hard links. –  pqnet Aug 6 at 23:34

ls -l is definitely more expensive, since it has to query the file system for metadata such as owner, group, permissions, access time, etc. Vanilla /bin/ls only has to look up the names of the entries in the directory being listed.

Note that ls may be aliased on your system to something less vanilla than /bin/ls. Run type ls to see if that's the case.

share|improve this answer
    
Vanilla ls gets the inode numbers and then has to look up the directory names and filenames from each inode. Is this correct? ls -l has to reach out to the same inodes as above but also has to read metadata from each inode in addition to the filename or directory name. Is this correct? –  user100503 Aug 6 at 20:10
    
Yes, but depending on the type of file system you have, how it was configured, and its usage patterns, it may be expensive to look up metadata. The metadata may not be stored with the inodes -- it may be stored on a different partition, or even on different hardware entirely. (If you're using a standard desktop, though, then that's probably not the case.) This is why the man page for find spends some time talking about efficient vs. inefficient searches; searches that just look at file names are far more efficient. –  dg99 Aug 6 at 20:14
1  
@user100503: I'm pretty sure that the directory contains the names with the inode numbers; you don't go to the inode to get the name (if you have several hardlinks to a file, which of the file names would be in the inode?) –  celtschk Aug 6 at 20:33
    
@celtschk Yes, I think my answer was overly wordy. I have edited it to remove some of the confusion. –  dg99 Aug 6 at 21:09
2  
@user100503, no it's not. The data in the directory can be thought of as a list of names and their associated inodes. That is the main purpose of the directory. The name does not exist inside the inode. As celtschk mentions, multiply linked inodes do not have a single name. –  BowlOfRed Aug 6 at 21:23

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.