Videos
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().
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.
BEA and Perlx.x in these folders are symbolic links. The symbolic link is another name that "points to" the real file.
The option -l tells the command to use a long list format. It gives back several columns wich correspond to:
- Permissions
- Number of hardlinks
- File owner
- File group
- File size
- Modification time
- Filename
The first letter in the permissions (lrwxrwxrwx) column show the file's type. l here means a link, A d means a directory and a - means a normal file (there are other characters, but those are the basic ones). The next nine characters are divided into 3 groups, each one a permission. Each letter in a group correspond to the read, write and execute permission, and each group correspond to the owner of the file, the group of the file and then for everyone else.
[ File type ][ Owner permissions ][ Group permissions ][ Everyone permissions ]
The characters can be one of four options:
r = read permission
w = write permission
x = execute permission
- = no permission
Finally, the + at the end means some extended permissions.
The file in question is a symbolic link. The symbolic link is another name that "points to" the real file.
When you do ls -l it also shows you which file is pointed to by the link. You can actually see:
lrwxrwxrwx
^
|________ `l` here means a link
In the order of output;
-rwxrw-r-- 1 root root 2048 Jan 13 07:11 afile.exe
- file permissions (
-rwxrw-r--), - number of (hard) links (
1), - owner name (
root), - owner group (
root), - file size in bytes (
2048), - time of last modification (
Jan 13 07:11), and - file/directory name (
afile.exe)
File permissions is displayed as following;
- first character is most often
-,lord. Adindicates a directory, a-represents a regular file,lis a symlink (or soft link) and other letters are used for other types of special files - three sets of characters, three times, indicating permissions for owner, group and other:
- r = readable
- w = writable
- x = executable (for files) or accessible (for directories)
- this may be followed by some other character of there are extended permissions, like e.g. Linux ACL that are marked with a
+.
In your example -rwxrw-r--, this means the line displayed is:
- a regular file (displayed as
-) - readable, writable and executable by owner (
rwx) - readable, writable, but not executable by group (
rw-) - readable but not writable or executable by other (
r--)
The number of hard links means the number of names the inode has, i.e. links created with ln without the -s option.
The output of the "ls" command depends on the version of "ls", the options used, the platform used, etc. It appears from your example that you're using it from a typical un*x (such as Linux), and probably using a typical modern "ls" version. In which case:
-rwxrw-r-- 10 root root 2048 Jan 13 07:11 afile.exe
?UUUGGGOOOS 00 UUUUUU GGGGGG #### MON DD XX:XX FILENAME
^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^- Filename.
| | | | | | | | | \-------------- Time of last modification.
| | | | | | | | \--------------------- File Size OR for directory size of the metadata. (Size is *usually* in bytes on modern systems; See below.)
| | | | | | | \-------------------------- Group Name (for example, Users, Administrators, etc)
| | | | | | \--------------------------------- Owner Acct
| | | | | \---------------------------------------- Link count (what constitutes a "link" here varies)
| | | | \--------------------------------------------- Alternative Access (blank means none defined, anything else varies)
| \--\--\----------------------------------------------- Read, Write and Special access modes for [U]ser, [G]roup, and [O]thers (everyone else)
\------------------------------------------------------- File type flag
I am not sure why your link count is so high for the example file you listed. Some platforms have an odd notion of what constitutes a "link". These usually include hard links and symbolic links, as well as directory entries (which is why directories often have high link counts – its parent has one link, the directory has a link to itself in the . entry, and each of its sub-directories has a link back via ..).
Some versions and/or command line flags will list the number of blocks used instead of the number of bytes; a filesystem with a block size of 1024 bytes will list all sizes up to 1024 bytes as "1", meaning 1 block is used, from 1025 to 2048 as "2", using 2 blocks, and so on. But listing block sizes by default (without explicitly using a command line option) is rare on most modern un*x machines.
The special/alternative access flag is usually a blank space, but on some platforms, it may be used to indicate there are special/alternative access modes (such as ACLs and security descriptors on WIN32, etc), and varies widely – consult your manual, man pages, info tool, or what-not.
The permissions (mode) flags (UUUGGGOOO) are three sets of three chars, where the first set is "User" (i.e., Owner), the second set is "Group" and the third set is "Others" (i.e., everyone else; anyone who is neither Owner nor Group). The three permissions flags in each set are typically r or - meaning the User/Group/Others can read the file (r) or not (-), followed by w or - indicating whether they can write to the file (you can have files which you can write to, but cannot read, as odd as that may sound!), and the third character is a 'catch-all' flag for other modes, typically something like x for execute (for directories, it means you can attempt to access the directory contents), or - for none. Sometimes you may encounter an s or S for setuid and/or setgid programs, or other less common characters; see your ls documentation for the mode characters it will show.
Finally, the very first character is the file type; typically one of: d for directory, l for a symbolic link (hard links show normally without a special character of their own), or - for a normal file. There are many other, but less commonly seen, file types for various filesystems. These first ten characters (file type and permissions) are discussed on Wikipedia. Again, your documentation will tell you exactly what kind of file types your command supports and displays.
BTW, if you cannot find a man/info page for ls itself (man ls/info ls), try looking in the "coreutils" package (info coreutils). Also note that among the more common platforms, Microsoft platforms tend not to translate very well to ls output, so you may see odd behavior, flags, or other unusual info in the output, depending on how your version of ls was compiled, what it was linked against, etc.
One more caveat: The file time stamp is usually the date/time the file was last modified, not the time the file was created. In fact, on a un*x-ish filesystem, there is no record of the file creation time; the ctime field does NOT mean "creation time" as it does on FAT/NTFS filesystems, but rather, it means the "inode [C]hange time" – the time the inode itself was last modified. The "mtime" (last [M]odified) and atime (last [A]ccesed/read) timestamps are the same on both systems – although the precision (FAT has a granularity of two seconds, for example) and time zone may vary.