Every Linux command has one or more options and flags to perform different operations. Today, let us learn about one of the useful and important option that allow us to simulate Linux commands but do not actually change the system.
For instance, we can simulate (or dry run) the process of installation or removal of a package or program, but without actually installing or removing the intended package from a Linux system.
As you already know, Simulation is just an imitation of the operation of an actual process or system to verify how the process will work in real time.
Simulation is being used in many fields for teaching, testing, training purposes and solving problems. Now let us get back and learn how to simulate Linux commands.
Why dry run or simulate Linux commands?
Simple. We can exactly find what a Linux command will do even before running it. For example, you might want to install Vim editor on your Linux box. You don't know what additional dependencies this package brought with it. By simulating the installation process, you will know how many dependencies will be installed along with the package. It is also same for package removal. You will know what dependencies are no longer required after uninstalling a package. It also helps you to make sure if a command works properly even before running it. Still don't understand? No problem. Allow me to show you an example.
Simulate Linux commands
Let us say, you want to install Vim editor on your Ubuntu system.
To simulate the installation of Vim on Ubuntu, simply run:
$ sudo apt install vim --dry-run
$ sudo apt install vim --simulate
$ sudo apt install vim -s
Sample output of the above commands:
Reading package lists... Done Building dependency tree Reading state information... Done Suggested packages: ctags vim-doc vim-scripts The following NEW packages will be installed: vim 0 upgraded, 1 newly installed, 0 to remove and 82 not upgraded. Inst vim (2:8.0.1453-1ubuntu1.1 Ubuntu:18.04/bionic-updates, Ubuntu:18.04/bionic-security [amd64]) Conf vim (2:8.0.1453-1ubuntu1.1 Ubuntu:18.04/bionic-updates, Ubuntu:18.04/bionic-security [amd64])
As you can see in the above output, we only simulated the Vim installation process, but didn't actually install it. Nothing is changed in the system, just a simulation. By looking at the simulation, we can find what additional packages (dependencies) are going to be installed with actual package.
Similarly, we can simulate the removal of an installed program like below.
$ sudo apt remove vim --dry-run
$ sudo apt remove vim --simulate
$ sudo apt remove vim -s
Reading package lists... Done Building dependency tree Reading state information... Done The following packages will be REMOVED: vim 0 upgraded, 0 newly installed, 1 to remove and 82 not upgraded. Remv vim [2:8.0.1453-1ubuntu1.1]
Again, we only simulated the removal process, but didn't remove anything from the system. You can make sure what programs will be deleted if you run the above commands in real time.
We can even simulate the entire upgrade process on Ubuntu like below.
$ sudo apt dist-upgrade --dry-run Reading package lists... Done Building dependency tree Reading state information... Done Calculating upgrade... Done The following packages will be upgraded: apt apt-utils base-files bash bsdutils cloud-init console-setup console-setup-linux debconf debconf-i18n dmeventd dmsetup dpkg fdisk friendly-recovery grep grub-common grub-pc grub-pc-bin grub2-common initramfs-tools initramfs-tools-bin initramfs-tools-core iputils-ping iputils-tracepath keyboard-configuration landscape-common language-selector-common libapt-inst2.0 libapt-pkg5.0 libblkid1 libdevmapper-event1.02.1 libdevmapper1.02.1 libdrm-common libdrm2 libfdisk1 libldap-2.4-2 libldap-common liblvm2app2.2 liblvm2cmd2.02 libmount1 libnss-systemd libpam-systemd libprocps6 libpython3.6 libpython3.6-minimal libpython3.6-stdlib libsmartcols1 libsystemd0 libudev1 libuuid1 linux-firmware lvm2 mdadm mount netplan.io nplan open-vm-tools procps python-apt-common python3-apt python3-debconf python3-distupgrade python3-gdbm python3-software-properties python3.6 python3.6-minimal snapd software-properties-common sosreport systemd systemd-sysv thermald ubuntu-minimal ubuntu-release-upgrader-core ubuntu-standard udev unattended-upgrades update-notifier-common util-linux uuid-runtime xkb-data 82 upgraded, 0 newly installed, 0 to remove and 0 not upgraded. Inst base-files [10.1ubuntu2.4] (10.1ubuntu2.7 Ubuntu:18.04/bionic-updates [amd64]) Conf base-files (10.1ubuntu2.7 Ubuntu:18.04/bionic-updates [amd64]) Inst bash [4.4.18-2ubuntu1] (4.4.18-2ubuntu1.2 Ubuntu:18.04/bionic-updates [amd64]) Conf bash (4.4.18-2ubuntu1.2 Ubuntu:18.04/bionic-updates [amd64]) Inst bsdutils [1:2.31.1-0.4ubuntu3.3] (1:2.31.1-0.4ubuntu3.4 Ubuntu:18.04/bionic-updates [amd64]) [..] Conf thermald (1.7.0-5ubuntu5 Ubuntu:18.04/bionic-updates [amd64]) Conf unattended-upgrades (1.1ubuntu1.18.04.13 Ubuntu:18.04/bionic-updates [all]) Conf cloud-init (19.3-41-gc4735dd3-0ubuntu1~18.04.1 Ubuntu:18.04/bionic-updates [all]) Conf open-vm-tools (2:11.0.1-2ubuntu0.18.04.2 Ubuntu:18.04/bionic-updates [amd64])
By simulating the upgrade process, you will get a clear idea about which packages will be upgraded.
Please note that the non-root users can also perform the simulation. If you run simulation without sudo, you will see a warning message like below.
$ apt remove vim --dry-run NOTE: This is only a simulation! apt needs root privileges for real execution. Keep also in mind that locking is deactivated, so don't depend on the relevance to the real current situation! Reading package lists... Done Building dependency tree Reading state information... Done The following packages will be REMOVED: vim 0 upgraded, 0 newly installed, 1 to remove and 82 not upgraded. Remv vim [2:8.0.1453-1ubuntu1.1]
What if the"Dry Run" option is not available?
As far as I know, most Linux and unix commands have the dry run option, but some commands doesn't. For example, the
yum command doesn't has dry run option. If you want to simulate Yum install or remove operations, use
"setopt" option like below.
$ yum install --setopt tsflags=test vim
If you want to simulate installation of an
.rpm package, do:
$ sudo rpm -ivh --test vim-enhanced-7.4.629-6.el7.x86_64.rpm
The above commands will not install vim, but displays how
"yum install vim" and
"rpm -ivh" commands will perform in real time.
May be you should try "maybe"
Alternatively, you can use a command line utility named “maybe” to simulate Linux commands. It allows you to run a command and see what it does to your files without actually doing it! After reviewing the output listed, you can then decide whether you really want to run it or not. For more details, please visit the following link.
Hope this helps.