1

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

So that's some reference.

But I was wondering how a command such as

ls -1 | grep 'myfile'

would be executed by the shell, i.e. when is exec called, when is fork called, when id dup called(if at all).

Also, how is this entire command parsed?

McFiddlyWiddly
  • 547
  • 10
  • 29
  • Just run [strace](https://linux.die.net/man/1/strace) if you want to see what syscalls are made. – kaylum Nov 19 '16 at 04:41
  • strace + everything in the command? – McFiddlyWiddly Nov 19 '16 at 04:43
  • Read the manual. Search the web. Try it out. Then come back if you still have a question. – kaylum Nov 19 '16 at 04:45
  • 1
    Stack Overflow is a site for programming and development questions. This question appears to be off-topic because it is not about programming or development. See [What topics can I ask about here](http://stackoverflow.com/help/on-topic) in the Help Center. Perhaps [Super User](http://superuser.com/) or [Unix & Linux Stack Exchange](http://unix.stackexchange.com/) would be a better place to ask. Also see [Where do I post questions about Dev Ops?](http://meta.stackexchange.com/q/134306) – jww Nov 19 '16 at 12:38
  • The `ls` command's handling of multiple arguments is nearly trivial (it loops over `argv`), and a very tiny part of what you're asking about. For that matter, the command you use as an example doesn't even pass multiple arguments to `ls`. If you post the question to a more appropriate site, you'll need to clean it up substantially. – Keith Thompson Nov 21 '16 at 19:43

2 Answers2

1

What does fork do

Fork is the primary (and historically, only) method of process creation on Unix-like operating systems.

What does exec do

In computing, exec is a functionality of an operating system that runs an executable file in the context of an already existing process

What does this mean
When you run a command (that is not built in like exit, cd), shell creates a child process using fork. This child process then uses exec executes the binary file for that command (e.g: /bin/ls)

What happens when during input/output redirecction
Every process is supplied with three streams standard input (STDIN), standard output (STDOUT) and standard error (STDERR). By default these streams are mapped to parent process's respective streams. Thus commands like wc or nano which reads from STDIN can be supplied with data from parent shell process's STDIN, and their output is captured by parent shell process and displayed.

However, when using redirection like

ls /tmp /abcd 1>out.log and 2>err.log

stdout is now mapped to a file output stream of out.log, similarly stderr is mapped to err.log. And the output is written to corresponding files.

PIPE chaining

ls -1 | grep 'myfile'

In shell PIPE | is used to chain the STDOUT of first command to STDIN of second command.

This means output of ls -1 (list of files and directories) is given as input to grep myfile which searches for lines containing "myfile" and prints to its STDOUT. The combined effect is to search filename containing char sequence "myfile"

shanmuga
  • 4,329
  • 2
  • 21
  • 35
0

I'm answering here specifically the textual question of the title,

How does 'ls' command work when it has multiple arguments?

...not addressing the question which came underneath. Was needing to check if a list of files was present in a directory and this question's phrasing was the closest to what I needed.

If you need to do this, either separate them with a space or wrap them in curly brackets with commas and no space as follows:

ls something.txt something_else.txt

or

ls {something.txt,something_else.txt}
cardamom
  • 6,873
  • 11
  • 48
  • 102