Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 358
iankits Avatar asked Oct 15 '08 09:10

iankits


People also ask

How does ls work in Unix?

ls–Lists the names of files in a particular Unix directory. If you type the ls command with no parameters or qualifiers, the command displays the files listed in your current working directory. When you give the ls command, you can add one or more modifiers to get additional information.

What is ls command in Unix with examples?

ls is a Linux shell command that lists directory contents of files and directories. Some practical examples of ls command are shown below. 1. Open Last Edited File Using ls -t. ls -t : It sorts the file by modification time, showing the last edited file first.

What is ls command in Linux terminal?

The ls command is one of the most commonly used commands in daily Linux/UNIX operations. The command is used in listing contents inside a directory and is one of the few commands beginners learn from the onset.


1 Answers

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().

like image 69
Mark Baker Avatar answered Oct 05 '22 03:10

Mark Baker