Back to menu

4 - Advanced Linux Commands

Advanced Commands for Linux users

Advanced commands provide greater customization and controls in more specialized situations once you become familiar with basic commands.

Rocky Linux Academy > Admin Guide > Advanced Commands
Back to menu

Objectives

In this chapter, future Linux administrators will learn:

✔️ some useful commands not covered in the previous chapter.
✔️ some advanced commands.

🏁 user commands, Linux

Knowledge: ⭐
Complexity: ⭐ ⭐ ⭐

Reading time: 20 minutes

Rocky Linux Academy > Admin Guide > Advanced Commands
Back to menu

uniq command

The uniq command is a very powerful command, used with the sort command, especially for log file analysis. It allows you to sort and display entries by removing duplicates.

Rocky Linux Academy > Admin Guide > Advanced Commands
Back to menu

uniq command

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

antoine
xavier
steven
patrick
xavier
antoine
antoine
steven

Note

uniq requires the input file to be sorted before running because it only compares consecutive lines.

Rocky Linux Academy > Admin Guide > Advanced Commands
Back to menu

uniq command

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

$ sort firstnames.txt | uniq
antoine
patrick
steven
xavier
Rocky Linux Academy > Admin Guide > Advanced Commands
Back to menu

uniq command

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

$ sort firstnames.txt | uniq -u
patrick
Rocky Linux Academy > Admin Guide > Advanced Commands
Back to menu

uniq command

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

$ sort firstnames.txt | uniq -d
antoine
steven
xavier
Rocky Linux Academy > Admin Guide > Advanced Commands
Back to menu

uniq command

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

$ sort firstnames.txt | uniq -D
antoine
antoine
antoine
steven
steven
xavier
xavier
Rocky Linux Academy > Admin Guide > Advanced Commands
Back to menu

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

$ sort firstnames.txt | uniq -c
      3 antoine
      1 patrick
      2 steven
      2 xavier
$ sort firstnames.txt | uniq -cd
      3 antoine
      2 steven
      2 xavier
Rocky Linux Academy > Admin Guide > Advanced Commands
Back to menu

xargs commands

The xargs command allows the construction and execution of command lines from standard input.

The xargs command reads whitespace or linefeed delimited arguments from standard input, and executes the command (/bin/echo by default) one or more times using the initial arguments followed by the arguments read from standard input.

Rocky Linux Academy > Admin Guide > Advanced Commands
Back to menu

xargs commands

A first and simplest example would be the following:

$ xargs
use
of
xargs
<CTRL+D>
use of xargs
Rocky Linux Academy > Admin Guide > Advanced Commands
Back to menu

xargs commands

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 CTRL+D. xargs then executes the default command echo followed by the three arguments corresponding to the user input, namely:

$ echo "use" "of" "xargs"
use of xargs
Rocky Linux Academy > Admin Guide > Advanced Commands
Back to menu

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:

$ xargs ls -ld
/home
/tmp
/root
<CTRL+D>
drwxr-xr-x. 9 root root 4096  5 avril 11:10 /home
dr-xr-x---. 2 root root 4096  5 avril 15:52 /root
drwxrwxrwt. 3 root root 4096  6 avril 10:25 /tmp
Rocky Linux Academy > Admin Guide > Advanced Commands
Back to menu

xargs commands

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?

$ xargs find /var/log -name
*.old
*.log
find: paths must precede expression: *.log
Rocky Linux Academy > Admin Guide > Advanced Commands
Back to menu

xargs commands

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

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

Rocky Linux Academy > Admin Guide > Advanced Commands
Back to menu

xargs commands

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:

Rocky Linux Academy > Admin Guide > Advanced Commands
Back to menu
$ xargs -L 1 find /var/log -name
*.old
/var/log/dmesg.old
*.log
/var/log/boot.log
/var/log/anaconda.yum.log
/var/log/anaconda.storage.log
/var/log/anaconda.log
/var/log/yum.log
/var/log/audit/audit.log
/var/log/anaconda.ifcfg.log
/var/log/dracut.log
/var/log/anaconda.program.log
<CTRL+D>
Rocky Linux Academy > Admin Guide > Advanced Commands
Back to menu

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

$ xargs -n 1 find /var/log -name
*.old *.log
/var/log/dmesg.old
/var/log/boot.log
/var/log/anaconda.yum.log
/var/log/anaconda.storage.log
/var/log/anaconda.log
/var/log/yum.log
/var/log/audit/audit.log
/var/log/anaconda.ifcfg.log
/var/log/dracut.log
/var/log/anaconda.program.log
<CTRL+D>
Rocky Linux Academy > Admin Guide > Advanced Commands
Back to menu

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

$ 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
-rw-r--r-- root/root    499270 2017-04-06 11:01 /var/log/audit/audit.log

The special feature of the xargs command is that it places the input argument at the end of the called command. This works very well with the above example since the files passed in will form the list of files to be added to the archive.

Rocky Linux Academy > Admin Guide > Advanced Commands
Back to menu

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

The -I option allows you to specify a character (the % character in the above example) where the input files to xargs will be placed.

Rocky Linux Academy > Admin Guide > Advanced Commands
Back to menu

yum-utils package

The yum-utils package is a collection of utilities, built for yum by various authors, which make it easier and more powerful to use.

Rocky Linux Academy > Admin Guide > Advanced Commands
Back to menu

Note

While yum has been replaced by dnf in Rocky Linux 8, the package name has remained yum-utils, although it can be installed as dnf-utils as well. These are classic YUM utilities implemented as CLI shims on top of DNF to maintain backwards compatibility with yum-3.

Rocky Linux Academy > Admin Guide > Advanced Commands
Back to menu

Here are some examples of these utilities:

repoquery command

The repoquery command is used to query the packages in the repository.

Rocky Linux Academy > Admin Guide > Advanced Commands
Back to menu

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>

 repoquery --requires <package-name>
Rocky Linux Academy > Admin Guide > Advanced Commands
Back to menu

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

    $ repoquery -l yum-utils
    /etc/bash_completion.d
    /etc/bash_completion.d/yum-utils.bash
    /usr/bin/debuginfo-install
    /usr/bin/find-repos-of-install
    /usr/bin/needs-restarting
    /usr/bin/package-cleanup
    /usr/bin/repo-graph
    /usr/bin/repo-rss
    …
Rocky Linux Academy > Admin Guide > Advanced Commands
Back to menu

yumdownloader command

The yumdownloader command downloads RPM packages from the repositories. Equivalent to dnf download --downloadonly --downloaddir ./ package-name

Note

This command is very useful to quickly build a local repository of a few rpms!

Rocky Linux Academy > Admin Guide > Advanced Commands
Back to menu

yumdownloader command

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

$ yumdownloader --destdir /var/tmp --resolve samba
or
$ dnf download --downloadonly --downloaddir /var/tmp  --resolve  samba
Rocky Linux Academy > Admin Guide > Advanced Commands
Back to menu

yumdownloader command

Options Comments
--destdir The downloaded packages will be stored in the specified folder.
--resolve Also downloads the package dependencies.
Rocky Linux Academy > Admin Guide > Advanced Commands
Back to menu

psmisc packages

The psmisc package contains utilities for managing system processes:

  • pstree: the pstree command displays the current processes on the system in a tree-like structure.
  • killall: the killall command sends a kill signal to all processes identified by name.
  • fuser: the fuser command identifies the PID of processes that use the specified files or file systems.
Rocky Linux Academy > Admin Guide > Advanced Commands
Back to menu

Examples:

$ pstree
systemd─┬─NetworkManager───2*[{NetworkManager}]
        ├─agetty
        ├─auditd───{auditd}
        ├─crond
        ├─dbus-daemon───{dbus-daemon}
        ├─firewalld───{firewalld}
        ...
        ...
        ├─systemd-journal
        ├─systemd-logind
        ├─systemd-udevd
        └─tuned───4*[{tuned}]
Rocky Linux Academy > Admin Guide > Advanced Commands
Back to menu
# killall httpd

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

# fuser -k /etc/httpd/conf/httpd.conf
Rocky Linux Academy > Admin Guide > Advanced Commands
Back to menu

watch command

The watch command regularly executes a command and displays the result in the terminal in full screen.

The -n option allows you to specify the number of seconds between each execution of the command.

Note

To exit the watch command, you must type the keys: CTRL+C to kill the process.

Rocky Linux Academy > Admin Guide > Advanced Commands
Back to menu

Examples:

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

watch -n 5 tail -n 3 /etc/passwd

Result:

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
chrony:x:995:992::/var/lib/chrony:/sbin/nologin
sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
Rocky Linux Academy > Admin Guide > Advanced Commands
Back to menu

Monitoring the number of files in a folder:

watch -n 1 'ls -l | wc -l'

Display a clock:

watch -t -n 1 date
Rocky Linux Academy > Admin Guide > Advanced Commands
Back to menu

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

install source dest  
install -t directory source [...]
install -d directory
Rocky Linux Academy > Admin Guide > Advanced Commands
Back to menu
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.
Rocky Linux Academy > Admin Guide > Advanced Commands
Back to menu
Options Remarks
-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).

Rocky Linux Academy > Admin Guide > Advanced Commands
Back to menu

install command

Examples:

Create a directory with the -d option:

install -d ~/samples

Copy a file from a source location to a directory:

install src/sample.txt ~/samples/
Rocky Linux Academy > Admin Guide > Advanced Commands
Back to menu

install command

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

$ install -v -D -t ~/samples/ src/sample.txt
install: creating directory '~/samples'
'src/sample.txt' -> '~/samples/sample.txt'
Rocky Linux Academy > Admin Guide > Advanced Commands
Back to menu

install command

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

sudo install -v -o rocky -g users -m 644 -D -t ~/samples/ src/sample.txt

Note

sudo is required in this case to make property changes.

Rocky Linux Academy > Admin Guide > Advanced Commands
Back to menu

install command

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

$ 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.

Rocky Linux Academy > Admin Guide > Advanced Commands
Back to menu

install command

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

$ install -v -b -S ".bak" -D -t ~/samples/ src/sample.txt
'src/sample.txt' -> '~/samples/sample.txt' (archive: '~/samples/sample.txt.bak')
Rocky Linux Academy > Admin Guide > Advanced Commands
Back to menu



Questions ?




Next Chapter

Rocky Linux Academy > Admin Guide > Advanced Commands