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
142 changes: 109 additions & 33 deletions docs/books/admin_guide/04-advanced-commands.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
---
title: Advanced Linux Commands
title: Advanced Commands for Linux users
---
<!-- markdownlint-disable MD033 -->

# Advanced Commands for Linux users

Expand All @@ -10,12 +11,12 @@ Advanced commands provide greater customization and controls in more specialized

**Objectives**: In this chapter, future Linux administrators will learn:

:heavy_check_mark: some useful commands not covered in the previous chapter.
:heavy_check_mark: some useful commands not covered in the previous chapter.
:heavy_check_mark: some advanced commands.

:checkered_flag: **user commands**, **Linux**

**Knowledge**: :star:
**Knowledge**: :star:
**Complexity**: :star: :star: :star:

**Reading time**: 20 minutes
Expand All @@ -28,7 +29,7 @@ The `uniq` command is a very powerful command, used with the `sort` command, esp

To illustrate how the `uniq` command works, let's use a `firstnames.txt` file containing a list of first names:

```
```text
antoine
xavier
steven
Expand All @@ -45,7 +46,7 @@ steven

With no argument, the `uniq` command will not display identical lines that follow each other in the `firstnames.txt` file:

```
```bash
$ sort firstnames.txt | uniq
antoine
patrick
Expand All @@ -55,14 +56,14 @@ xavier

To display only the rows that appear only once, use the `-u` option:

```
```bash
$ sort firstnames.txt | uniq -u
patrick
```

Conversely, to display only the lines that appear at least twice in the file, use the `-d` option:

```
```bash
$ sort firstnames.txt | uniq -d
antoine
steven
Expand All @@ -71,7 +72,7 @@ xavier

To simply delete lines that appear only once, use the `-D` option:

```
```bash
$ sort firstnames.txt | uniq -D
antoine
antoine
Expand All @@ -84,15 +85,15 @@ xavier

Finally, to count the number of occurrences of each line, use the `-c` option:

```
```bash
$ sort firstnames.txt | uniq -c
3 antoine
1 patrick
2 steven
2 xavier
```

```
```bash
$ sort firstnames.txt | uniq -cd
3 antoine
2 steven
Expand All @@ -107,7 +108,7 @@ The `xargs` command reads whitespace or linefeed delimited arguments from standa

A first and simplest example would be the following:

```
```bash
$ xargs
use
of
Expand All @@ -118,7 +119,7 @@ use of xargs

The `xargs` command waits for an input from the standard **stdin** input. Three lines are entered. The end of the user input is specified to `xargs` by the keystroke sequence <kbd>CTRL</kbd>+<kbd>D</kbd>. `xargs` then executes the default command `echo` followed by the three arguments corresponding to the user input, namely:

```
```bash
$ echo "use" "of" "xargs"
use of xargs
```
Expand All @@ -127,7 +128,7 @@ It is possible to specify a command to be run by `xargs`.

In the following example, `xargs` will run the command `ls -ld` on the set of folders specified in the standard input:

```
```bash
$ xargs ls -ld
/home
/tmp
Expand All @@ -142,7 +143,7 @@ In practice, the `xargs` command executed the `ls -ld /home /tmp /root` command.

What happens if the command to be executed does not accept multiple arguments, such as with the `find` command?

```
```bash
$ xargs find /var/log -name
*.old
*.log
Expand All @@ -151,14 +152,14 @@ find: paths must precede expression: *.log

The `xargs` command attempted to execute the `find` command with multiple arguments behind the `-name` option, which caused `find` to generate an error:

```
```bash
$ find /var/log -name "*.old" "*.log"
find: paths must precede expression: *.log
```

In this case, the `xargs` command must be forced to execute the `find` command several times (once per line entered as standard input). The `-L` option followed by an **integer** allows you to specify the maximum number of entries to be processed with the command at one time:

```
```bash
$ xargs -L 1 find /var/log -name
*.old
/var/log/dmesg.old
Expand All @@ -177,7 +178,7 @@ $ xargs -L 1 find /var/log -name

To specify both arguments on the same line, use the `-n 1` option:

```
```bash
$ xargs -n 1 find /var/log -name
*.old *.log
/var/log/dmesg.old
Expand All @@ -195,7 +196,7 @@ $ xargs -n 1 find /var/log -name

Case study of a backup with a `tar` based on a search:

```
```bash
$ find /var/log/ -name "*.log" -mtime -1 | xargs tar cvfP /root/log.tar
$ tar tvfP /root/log.tar
-rw-r--r-- root/root 1720 2017-04-05 15:43 /var/log/boot.log
Expand All @@ -206,8 +207,8 @@ The special feature of the `xargs` command is that it places the input argument

Using the example of the `cp` command, to copy a list of files in a directory, this list of files will be added at the end of the command... but what the `cp` command expects at the end of the command is the destination. To do this, use the `-I` option to put the input arguments somewhere else than at the end of the line.

```
$ find /var/log -type f -name "*.log" | xargs -I % cp % /root/backup
```bash
find /var/log -type f -name "*.log" | xargs -I % cp % /root/backup
```

The `-I` option allows you to specify a character (the `%` character in the above example) where the input files to `xargs` will be placed.
Expand All @@ -229,13 +230,14 @@ The `repoquery` command is used to query the packages in the repository.
Examples of use:

* Display the dependencies of a package (it can be a software package that has been installed or not installed), equivalent to `dnf deplist <package-name>`
```

```bash
repoquery --requires <package-name>
```

* Display the files provided by an installed package (does not work for packages that are not installed), equivalent to `rpm -ql <package-name>`

```
```bash
$ repoquery -l yum-utils
/etc/bash_completion.d
/etc/bash_completion.d/yum-utils.bash
Expand Down Expand Up @@ -273,7 +275,7 @@ The `yumdownloader` command downloads RPM packages from the repositories. Equiv

Example: `yumdownloader` will download the _samba_ rpm package and all its dependencies:

```
```bash
$ yumdownloader --destdir /var/tmp --resolve samba
or
$ dnf download --downloadonly --downloaddir /var/tmp --resolve samba
Expand All @@ -294,7 +296,7 @@ The `psmisc` package contains utilities for managing system processes:

Examples:

```
```bash
$ pstree
systemd─┬─NetworkManager───2*[{NetworkManager}]
├─agetty
Expand All @@ -314,13 +316,13 @@ systemd─┬─NetworkManager───2*[{NetworkManager}]
└─tuned───4*[{tuned}]
```

```
```bash
# killall httpd
```

Kill processes (option `-k`) that access the `/etc/httpd/conf/httpd.conf` file:

```
```bash
# fuser -k /etc/httpd/conf/httpd.conf
```

Expand All @@ -338,13 +340,13 @@ Examples:

* Display the end of the `/etc/passwd` file every 5 seconds:

```
$ watch -n 5 tail -n 3 /etc/passwd
```bash
watch -n 5 tail -n 3 /etc/passwd
```

Result:

```
```bash
Every 5.0s: tail -n 3 /etc/passwd rockstar.rockylinux.lan: Thu Jul 1 15:43:59 2021

sssd:x:996:993:User for sssd:/:/sbin/nologin
Expand All @@ -354,12 +356,86 @@ sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin

* Monitoring the number of files in a folder:

```
$ watch -n 1 'ls -l | wc -l'
```bash
watch -n 1 'ls -l | wc -l'
```

* Display a clock:

```bash
watch -t -n 1 date
```

## `install` command

Contrary to what its name might suggest, the `install` command is not used to install new packages.

This command combines file copying (`cp`) and directory creation (`mkdir`), with rights management (`chmod`, `chown`) and other useful functionalities (like backups).

```bash
install source dest
install -t directory source [...]
install -d directory
```

Options:

| Options | Remarks                           |
| ------- | ------------------------------------------------------------ |
| `-b` or `--backup[=suffix]` | create a backup of destination file. |
| `-d`    | treat arguments as directory names.               |
| `-D`    | create all leading components before copying SOURCE to DEST. |
| `-g` and `-o`    | set ownership.               |
| `-m`    | set permissions.               |
| `-p`    | preserve the timestamps of sources files.               |
| `-t` | copy all source arguments to directory. |

!!! NOTE
There are options for managing the SELinux context (see the manual page).

Examples:

Create a directory with the `-d` option:

```bash
install -d ~/samples
```

Copy a file from a source location to a directory:

```bash
install src/sample.txt ~/samples/
```

These two orders could have been carried out with a single command:

```bash
$ install -v -D -t ~/samples/ src/sample.txt
install: creating directory '~/samples'
'src/sample.txt' -> '~/samples/sample.txt'
```

This command already saves time, now let's combine it with owner, owner group and rights management:

```bash
sudo install -v -o rocky -g users -m 644 -D -t ~/samples/ src/sample.txt
```
$ watch -t -n 1 date

!!! NOTE
`sudo` is required in this case to make property changes.

You can also create a backup of existing files thanks to the `-b` option:

```bash
$ install -v -b -D -t ~/samples/ src/sample.txt
'src/sample.txt' -> '~/samples/sample.txt' (archive: '~/samples/sample.txt~')
```

As you can see, the `install` command creates a backup file with a `~` tilde appended to the original file name.

The suffix can be specified thanks to the `-S` option:

```bash
$ install -v -b -S ".bak" -D -t ~/samples/ src/sample.txt
'src/sample.txt' -> '~/samples/sample.txt' (archive: '~/samples/sample.txt.bak')
```