Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 48 additions & 21 deletions docs/books/admin_guide/03-commands.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
---
title: Linux Commands
author: Antoine Le Morvan
contributors: Steven Spencer, Aditya Putta
update: 11-08-2021
---

# Commands for Linux Users
Expand Down Expand Up @@ -178,13 +181,13 @@ This set of manuals is divided into 8 sections, grouping information by topic, t
8. System administration tools and daemons.
Information about each section can be accessed by typing `man x intro`, where `x` is the section number.

The command :
The command:

```bash
man passwd
```

will tell the administrator about the passwd command, its options, etc. While a :
will tell the administrator about the passwd command, its options, etc. While a:

```bash
$ man 5 passwd
Expand Down Expand Up @@ -215,7 +218,7 @@ Examples:
[root]# shutdown -r +5
```

Options :
Options:

| Options | Remarks |
| ------- | -------------------------------- |
Expand Down Expand Up @@ -270,7 +273,7 @@ The `clear` command clears the contents of the terminal screen. In fact, to be m

In a terminal, the display will be permanently hidden, whereas in a graphical interface, a scrollbar will allow you to go back in the history of the virtual terminal.

!!! tip
!!! Tip
<kbd>CTRL</kbd> + <kbd>L</kbd> will have the same effect as the `clear` command

### `echo` command
Expand All @@ -283,7 +286,7 @@ The `-n` option will not return to the line after displaying the text (which is

For various reasons, the script developer may need to use special sequences (starting with a `\` character). In this case, the `-e` option will be stipulated, allowing interpretation of the sequences.

Among the frequently used sequences, we can mention :
Among the frequently used sequences, we can mention:

| Sequence | Result |
| -------- | --------------------- |
Expand Down Expand Up @@ -396,7 +399,7 @@ The **relative path** references that same file by traversing the entire tree fr

In the above example, the "`..`" refers to the parent directory of the current directory.

A directory, even if it is empty, will necessarily contain at least **two references** :
A directory, even if it is empty, will necessarily contain at least **two references**:

* `.`: reference to itself.
* `..`: reference to the parent directory of the current directory.
Expand Down Expand Up @@ -458,7 +461,7 @@ $ ls /home
. .. rockstar
```

The main options of the `ls` command are :
The main options of the `ls` command are:

| Option | Information |
| ------------------------------------------------------------ | ------------------------------------------------------------------------------------------------------------------------------------ |
Expand Down Expand Up @@ -615,7 +618,7 @@ The `touch` command changes the timestamp of a file or creates an empty file if
touch [-t date] file
```

Example :
Example:

```bash
$ touch /home/rockstar/myfile
Expand Down Expand Up @@ -760,6 +763,7 @@ $ cp -r /home/rockstar /tmp
| `-f` | Do not ask for confirmation if overwriting the destination file. |
| `-p` | Keeps the owner, permissions and timestamp of the copied file. |
| `-r` | Copies a directory with its files and subdirectories. |
| `-s` | Creates a symbolik links rather than copying |

```bash
cp file1 /repexist/file2
Expand Down Expand Up @@ -850,7 +854,7 @@ The `cat` command concatenates the contents of multiple files and displays the r
cat file1 [files]
```

Example 1 - Displaying the contents of a file to the standard output :
Example 1 - Displaying the contents of a file to the standard output:

```bash
$ cat /etc/passwd
Expand All @@ -862,13 +866,13 @@ Example 2 - Displaying the contents of multiple files to standard output:
$ cat /etc/passwd /etc/group
```

Example 3 - Displaying the contents of several files in the file `usersAndGroups.txt` :
Example 3 - Displaying the contents of several files in the file `usersAndGroups.txt`:

```bash
$ cat /etc/passwd /etc/group > usersAndGroups.txt
```

Example 4 - Displaying the line numbering :
Example 4 - Displaying the line numbering:

```bash
$ cat -n /etc/profile
Expand Down Expand Up @@ -951,7 +955,7 @@ The `sort` command sorts the lines of a file.
It allows you to order the result of a command or the content of a file in a given order, numerically, alphabetically, by size (KB, MB, GB) or in reverse order.

```bash
sort [-kx] [-n] [-o file] [-ty] file
sort [-kx] [-n] [-u] [-o file] [-ty] file
```

Example:
Expand All @@ -969,6 +973,7 @@ adm:x:3:4:adm:/var/adm/:/sbin/nologin
| `-o file ` | Saves the sort to the specified file |
| `-ty` | Specifies the field separator character `y`. |
| `-r` | Reverse the order of the result |
| `- u` | unique |

The `sort` command sorts the file only on the screen. The file is not modified by the sorting. To save the sort, use the `-o` option or an output redirection `>`.

Expand Down Expand Up @@ -998,7 +1003,7 @@ $ sort -R /etc/passwd

A system administrator is quickly confronted with the processing of IP addresses from the logs of his services such as SMTP, VSFTP or Apache. These addresses are typically extracted with the `cut` command.

Here is an example with the file `dns-client.txt` :
Here is an example with the file `dns-client.txt`:

```
192.168.1.10
Expand All @@ -1017,11 +1022,32 @@ $ sort -nr dns-client.txt
5.1.150.146
```

* Sorting file by removing duplicates

The `sort` command knows how to remove the duplicates from the file output using `-u` as option.

Here is an example with the file `colours.txt`:

```
Red
Green
Blue
Red
Pink
```
```
$ sort -u colours.txt
Blue
Green
Pink
Red
```

* Sorting file by sizes

The `sort` command knows how to recognize file sizes, from commands like `ls` with the `-h` option.

Here is an example with the file `size.txt` :
Here is an example with the file `size.txt`:

```
1,7G
Expand Down Expand Up @@ -1094,12 +1120,13 @@ $ find /tmp -name *.txt -exec rm -f {} \;

The previous command searches for all files in the `/tmp` directory named `*.txt` and deletes them.


!!! Tip "Understand the `-exec` option"
In the example above, the `find` command will construct a string representing the command to be executed.

If the `find` command finds three files named `log1.txt`, `log2.txt`, and `log3.txt`, then the `find` command will construct the string by replacing in the string `rm -f {} \;` the braces with one of the results of the search, and do this as many times as there are results.

This will give us :
This will give us:

```
rm -f /tmp/log1.txt ; rm -f /tmp/log2.txt ; rm -f /tmp/log3.txt ;
Expand Down Expand Up @@ -1301,7 +1328,7 @@ $ ls -R / 2>> errors_file

### Examples of redirection

Redirection of 2 outputs to 2 files :
Redirection of 2 outputs to 2 files:

```bash
$ ls -R / >> ok_file 2>> nok_file
Expand All @@ -1313,7 +1340,7 @@ Redirection of the 2 outputs to a single file:
$ ls -R / >> log_file 2>&1
```

Redirection of *stderr* to a "bottomless pit" (`/dev/null`) :
Redirection of *stderr* to a "bottomless pit" (`/dev/null`):

```bash
$ ls -R / 2>> /dev/null
Expand Down Expand Up @@ -1390,13 +1417,13 @@ For example:
$ ll
```

will replace the command :
will replace the command:

```bash
$ ls -l
```

The `alias` command lists the aliases for the current session. Aliases are set by default on Linux distributions. Here, the aliases for a Rocky server :
The `alias` command lists the aliases for the current session. Aliases are set by default on Linux distributions. Here, the aliases for a Rocky server:

```bash
$ alias
Expand All @@ -1409,13 +1436,13 @@ alias which='alias | /usr/bin/which --tty-only --read-alias --show-dot --show-ti

The aliases are only defined temporarily, for the time of the user session.

For permanent use, they must be created in the :
For permanent use, they must be created in the:

* `.bashrc` file in the user's login directory;
* `/etc/profile.d/alias.sh` file for all users.

!!! Warning
Special care must be taken when using aliases which can be potentially dangerous! For example, an alias set up without the administrator's knowledge :
Special care must be taken when using aliases which can be potentially dangerous! For example, an alias set up without the administrator's knowledge:

```bash
alias cd='rm -Rf'
Expand Down