Re: How does "ls" command work in Linux in detail?

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On Thu, Dec 3, 2009 at 9:18 AM, Daniel (Youngwhan) Song
<breadncup@xxxxxxxxx> wrote:
> Thanks, krushnaal and Greg,
>
> By looking at the Greg's strace, it gives me great insight of the command.
>
> So, It looks like it calls a library which can be in glib(?), and the
> library actually calls kernel device drivers like console or something like
> that, and they were communicating each other back and forth, and finally, it
> outputs the result of "ls" onto the screen. Is my understanding correct?

Actually what you have told is true for any command and is not limited
to "ls". If you want to know how ls works you will need to understand
a little about how files are stored on disk and how they are indexed.
And then you can try to read about readdir(), opendir(),stat() system
calls and finally implement your own "ls" :-)


>
> Best Regards,
> Daniel (Youngwhan) Song
>
>
>
> On Wed, Dec 2, 2009 at 7:41 PM, Greg Freemyer <greg.freemyer@xxxxxxxxx>
> wrote:
>>
>> On Wed, Dec 2, 2009 at 9:12 PM, Daniel (Youngwhan) Song
>> <breadncup@xxxxxxxxx> wrote:
>> > Hi,
>> >
>> > Could somebody explain me how exactly "ls" command work in Unix/Linux?
>> >
>> > When we type "ls" command in linux shell, what does process/procedure
>> > work
>> > with linux library or linux kernel, and how exactly does it show
>> > directory
>> > information to the standard output (1)?
>> >
>> > Thanks in advance.
>> >
>> > Best Regards,
>> > Daniel Song
>> >
>>
>> read the source of course, but a very handy shortcut is:
>>
>> # strace ls
>>
>> The key part with a couple comments is:
>>
>> # Get the directory entries
>> open(".", O_RDONLY|O_NONBLOCK|O_LARGEFILE|O_DIRECTORY|O_CLOEXEC) = 3
>> fcntl64(3, F_GETFD)                     = 0x1 (flags FD_CLOEXEC)
>> getdents64(3, /* 53 entries */, 32768)  = 1744
>> getdents64(3, /* 0 entries */, 32768)   = 0
>> close(3)                                = 0
>>
>> # Verify stdout is a character device
>> fstat64(1, {st_mode=S_IFCHR|0600, st_rdev=makedev(136, 0), ...}) = 0
>>
>> # map stdin into memory.  (Not sure why, see the source)
>> mmap2(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1,
>> 0) = 0xb73ff000
>>
>> # write the directory entries to stdout and wrap-up
>> write(1, "bin  Desktop  Documents  Downloa"..., 91bin  Desktop
>> Documents  Download  Music  Pictures  Public  public_html  Templates
>> Videos
>> ) = 91
>> close(1)                                = 0
>> munmap(0xb73ff000, 4096)                = 0
>> close(2)                                = 0
>> exit_group(0)                           = ?
>>
>> Greg
>
>



-- 
Thanks -
Manish
==================================
[$\*.^ -- I miss being one of them
==================================

--
To unsubscribe from this list: send an email with
"unsubscribe kernelnewbies" to ecartis@xxxxxxxxxxxx
Please read the FAQ at http://kernelnewbies.org/FAQ



[Index of Archives]     [Newbies FAQ]     [Linux Kernel Mentors]     [Linux Kernel Development]     [IETF Annouce]     [Git]     [Networking]     [Security]     [Bugtraq]     [Yosemite]     [MIPS Linux]     [ARM Linux]     [Linux RAID]     [Linux SCSI]     [Linux ACPI]
  Powered by Linux