LINUX CONCEPTS

How Linux operates

Useful commands that facilitate using the Linux OS.

Standard Input, Standard Output, Standard Error

How to have a dialog with the shell.

Description

Standard Input (0, stdin), Standard Output (1, stdout), and Standard Error (2, stderr) are the words, we and the shell, use to communicate. Typically, we use stdin as a way to ask the shell to do specific things, and it replies with stdout or sterr.

Useful flags

> Redirect stdout to a file, overwriting it.
>> Redirect stdout to a file, appending to the end.
< Redirect stdin to read from a file instead of the keyboard.
2> Redirect stderr to a file, overwriting it.
2>> Redirect stderr to a file, appending to the end.
2>&1 Duplicate stderr onto wherever stdout currently points.
2>&> / &>> Bash shortcut for sending both stdout and stderr to a file (overwrite / append).

Example

$ ls nonexistent_folder > stdout_file.txt 2> sterr_file.txt
$
$
$ sort < names.txt > sorted_names.txt
$ sort names.txt

Note

Both commands return the same result, but the mechanism is different. While using sort < names.txt, the shell handles the file reading instead of the command itself.

Piping

Add concept function.

Description

A pipe connects the standard output of one command to the standard input of another using the | symbol.

Useful flags

| Connect one command’s stdout directly to the next command’s stdin.

Example

$ cat names.txt | sort
$
$ cat data.txt | sort | head -5
$
$ cat names | sort | uniq | wc -l
$
$ ls -l | grep "report"
$
$ ps aux | grep "python"
                    
Command substitution

You can embed a result of a command inside another command.

Description

Add convepts's description.

Useful flags

$(...) You can use one command directly within another one while using the first one. This can be useful in a dynamic environment. In the example section, you can see that the echo command will always provide the number of files within the folder and it doesn't matter what is your pwd.

Example

$ echo "There are $(ls | wc -l) files here."