How does the 'ls' command work in Linux/Unix?

35,137

Solution 1

ls doesn't fork. The shell forks and execs in order to run any command that isn't built in, and one of the commands it can run is ls.

ls uses opendir() and readdir() to step through all the files in the directory. If it needs more information about one of them it calls stat().

Solution 2

To add to the answer, in The C Programming Language book (K&RC) they have given a small example on how to go about implementing ls. They have explained the datastructures and functions used very well.

Solution 3

To understand what ls does, you could take a gander at the OpenSolaris source: https://hg.java.net/hg/solaris~on-src/file/tip/usr/src/cmd/ls/ls.c.

If that´s overwhelming, on Solaris you start by using truss to look at the system calls that ls makes to understand what it does. Using truss, try:

truss -afl -o ls.out /bin/ls

then look at the output in ls.out

I believe that trace is the equivalent to truss in Linux.

Solution 4

If you really want to understand the detailed innards of ls, look at the source code. You can follow tpgould's link to the Solaris source, or it's easy to find the source online from any Linux or BSD distribution.

I'll particularly recommend the 4.4BSD source.

As I recall, ls starts by parsing its many options, then starts with the files or directories listed on the command line (default is "."). Subdirectories are handled by recursion into the directory list routine. There's no fork() or exec() that I recall.

Share:
35,137
iankits
Author by

iankits

Digi-prenuer! :-)

Updated on September 21, 2021

Comments

  • iankits
    iankits over 2 years

    I would like to know exactly how the "Is" command works in Linux and Unix.

    As far as I know, ls forks & exec to the Linux/Unix shell and then gets the output (of the current file tree. eg./home/ankit/). I need a more detailed explanation, as I am not sure about what happens after calling fork.

    Could anyone please explain the functionality of the 'ls' command in detail?

  • Matthew Scharley
    Matthew Scharley over 15 years
    As a point of interest, I think you'll find it stat()'s just about every file in the directory, especially if colors and/or file type pre/suffixs are turned on.
  • Matthew Scharley
    Matthew Scharley over 15 years
    On a different note, why does the shell need to fork for foreground processes?
  • Greg Hewgill
    Greg Hewgill over 15 years
    monoxide: an exec() without a fork() will replace the currently running process, which means your shell would vanish the first time you ran a command.
  • iankits
    iankits over 15 years
    Thanks Mark, here is the decelaration part from K&RC to help others. DIR *opendir(char *dirname); Dirent *readdir(DIR *dfd); void closedir(DIR *dfd);
  • warren
    warren over 15 years
    if ls is a built-in, like in some standalone shells, does it still fork and exec?
  • ephemient
    ephemient over 15 years
    If it is built-in, it does not need to start another program, so an exec() is not required. Whether it fork()s or not depends on the shell and the situation -- for example, if cat was built-in, it would still be easier to fork to handle something like cat | cat, but forked read is hard.
  • Jonathan Leffler
    Jonathan Leffler over 15 years
    @Mark: it also uses lstat() since otherwise it only sees the permissions of the file at the far end of a symlink and not the symlink itself.
  • Jonathan Leffler
    Jonathan Leffler over 15 years
    @monoxide: the shell can run other programs in exactly one of two ways - by using exec() to replace itself with the other program, or by using fork() and then exec(). Those are the only mechanisms available in Unix (unless you count pthread_spawn()).
  • Thomas
    Thomas almost 15 years
    The Linux version of "truss" is called "strace".
  • alanc
    alanc over 12 years
    I think you're misunderstanding the original answer and in heated agreement with it. The question stated "ls forks & exec to the linux/unix shell" to which the answer correctly replied "ls doesn't fork. The shell forks and execs" and went on to say that ls is one of the commands the shell forks/execs.
  • Raghupathy
    Raghupathy over 12 years
    @alanc Thanks for clarification. Up voting the accepted answer :)
  • avi
    avi over 8 years
    If anyone here comes looking, the section is 8.6 in book.
  • henry
    henry almost 7 years
    Complete C newbie who woke up one morning wondering how C works. Is this the 4.4BSD file in question? (Obviously it'll be a while before I'm at the point of being able to make sense of it but gotta start somewhere…) github.com/sergev/4.4BSD-Lite2/blob/master/usr/src/bin/ls/ls‌​.c
  • mpez0
    mpez0 almost 7 years
    @henry That's the file, though it (or any other ls.c) is probably too complex for a "complete C newbie" using it for study.
  • henry
    henry almost 7 years
    Yes definitely won't be understanding this any time soon, but at least I know the goal. Thanks!
  • tripleee
    tripleee over 2 years
    Perhaps notice that the data structures are somewhat different with modern filesystems.