Bash Index : I - The 'I' Bash commands : description, flags and examples

mail

install

Usage :

mkdir -p + chown + chmod in a single command

Copy files while setting their file mode (aka permission) bits and, if possible, their owner and group.
Even though you have no freshly-compiled software to install, install is very convenient to do things like :

mkdir -p /path/to/my/dir && chown bob:developers /path/to/my/dir && chmod 700 /path/to/my/dir

with a single command :

install -d /path/to/my/dir -o bob -g developers -m 700

Flags :

Flag Usage
-d some/directory
--directory some/directory
  1. create missing parent directories (if any), giving them default attributes (755 permissions)
  2. create each given directory
  3. set owner + group + mode as specified (or use defaults : current user (the man says "root") + current user's group + u=rwx,go=rx,a-s)
-g group --group group set the group ownership of installed files or directories to group
-m mode --mode mode set the file mode bits for the installed file or directory to mode, which can be either an octal number (700) or a symbolic mode (u=rwx,go=) as in chmod
-o user --owner user set the ownership of installed files or directories to user
mail

inotifywait

Installed with the Debian package :

inotify-tools

Usage :

Wait for changes to files using inotify

Flags :

Flag Usage
-m --monitor Instead of exiting after receiving a single event, execute indefinitely
--format formatSpec Output in a user-specified format, using printf-like syntax :
  • %e : comma-separated list of the event(s) that occurred
  • %Xe : like %e with X instead of a comma
  • %f : name of the file affected by the event

Example :

Experimenting with inotifywait :

inotifywait -m --format 'EVENT: "%:e", FILE: "%f"' . &
[1] 15730
Setting up watches.
Watches established.
touch newFile
EVENT: "CREATE", FILE: "newFile"
EVENT: "OPEN", FILE: "newFile"
EVENT: "ATTRIB", FILE: "newFile"
EVENT: "CLOSE_WRITE:CLOSE", FILE: "newFile"
echo hello > newFile
EVENT: "MODIFY", FILE: "newFile"
EVENT: "OPEN", FILE: "newFile"
EVENT: "MODIFY", FILE: "newFile"
EVENT: "CLOSE_WRITE:CLOSE", FILE: "newFile"
echo world >> newFile
EVENT: "OPEN", FILE: "newFile"
EVENT: "MODIFY", FILE: "newFile"
EVENT: "CLOSE_WRITE:CLOSE", FILE: "newFile"
rm newFile
EVENT: "DELETE", FILE: "newFile"
CTRL-c

Alternate solution :

Alternate solution to detect the creation of a file :

#!/usr/bin/env bash

fileToWatch='path/to/some/file'
[ -f "$fileToWatch" ] && rm "$fileToWatch"
while true; do
	[ -f "$fileToWatch" ] && { echo 'file created'; exit; }
done
mail

ionice

Usage :

Set / display a process I/O nice value.

Flags :

Flag Usage
-c classValue Apply the specified class value to the process. Class values :
  • 0 : none
  • 1 : real time
  • 2 : best-effort. This is the effective scheduling class for any process that has not asked for a specific I/O priority. This class takes a priority argument
  • 3 : idle. Get disk time when no other program has asked for disk I/O for a defined grace period. Impact on normal system activity should be zero.
-n priority Priority of job within its class. Ranges from 0 (highest) to 7 (lowest).
-p PID Apply to the specified process ID

root permissions are required to :

Example :

Set the ionice value of the process having the PID 9799

ionice -c 3 -p 9799

Display the ionice value of the process having the PID 9799

ionice -p 9799

Set the actual shell to idle priority :

This is useful when you don't want local shell actions to interfere with existing processes : ionice -c3 -p$$
mail

iftop

Usage :

iftop is not really a Bash command, it's an additional utility designed to monitor network usage.
It can easily be installed on "Debianoids" with apt-get install iftop.
Running iftop requires root privileges.

Flags :

Flag Usage
-i interface Listen on interface

Example :

Monitor usage of interface eth0 :

iftop -i eth0
mail

innotop

Flags :

interactive-mode keys :

Flag Usage
? show help

Example :

Start an innotop session :

innotop -h host -u login -ppassword
mail

iostat

Usage :

Report CPU and I/O stats :
iostat options interval/count
This comes from the sysstat Debian package.

Flags :

Flag Usage
-c display the CPU usage report
-d display the device usage report
-h make the NFS report human-readable
-m display statistics in megabytes per second instead of blocks or kilobytes per second
-n display the network filesystem (NFS) report
-x display extended statistics

Example :

NFS report updated every 2 seconds :

iostat -nhm 2
mail

id

Usage :

print real and effective user and group IDs
If no username is provided, id returns information about the user invoking it.

Special uid's

  • root has the uid 0
  • nobody has the largest possible uid (as the opposite of the superuser) : 32767

Flags :

Flag Usage
-u --user print only the effective user ID
-n --name print a name instead of a number
If you need to get the current user name :
  • id -un will do the trick
  • $USER is even simpler
mail

ip

Usage :

Show / manipulate routing, devices, policy routing and tunnels.

Synopsis :

ip options object command

Objects :

Example :

Get interface status (name + up/down status + MTU + MAC address) :

  • ip link show
  • ip link show eth0

Turn interface up/down :

  • Was : ifconfig lo up|down
  • Now : ip link set lo up|down

ip addr :

Get interface status (as above + IP address) :

  • ip addr or ip a
  • ip addr show eth0 or ip a s eth0

Create a virtual interface :

  • Was : deviceName='eth0:test'; ipAddress='192.168.1.42'; ifconfig $deviceName $ipAddress up; ifconfig $deviceName; ping -c 3 $ipAddress; ifconfig $deviceName down; ifconfig
  • Now : deviceName='eth0:test'; ipAddress='192.168.1.42'; maskLength=24; ip addr add $ipAddress/$maskLength dev $deviceName; ip a s $deviceName; ping -c 3 $ipAddress; ip addr del $ipAddress/$maskLength dev $deviceName; ip a

ip route :

add a route (source) :
  • ip route add network/mask via gatewayIp
  • ip route add network/mask dev device
delete a route :
ip route del routeSpecification
The man page says routeSpecification has the same format than for ip route add (see example). So replaying the ip route add ... command replacing add with del should do the job .
find the network gateway (i.e. router)'s IP address :
ip r | grep default
default via 192.168.105.254 dev eth0 proto static metric 100
find the route that will be used to reach the specified IP address :
  • ip route get 8.8.8.8
  • short version : ip r g 8.8.8.8
8.8.8.8 via 10.0.2.2 dev eth0 src 10.0.2.15
	cache

ip rule (source) :

This is the command to manipulate the RPDB.
mail

iotop

Usage :

top-like I/O monitor, coming from iotop.

Flags :

Flag Usage
-a --accumulated Show accumulated I/O instead of bandwidth. In this mode, iotop shows the amount of I/O processes have done since iotop started.
-d seconds --delay=seconds Set the delay between iterations in seconds (1 second by default). Accepts non-integer values such as 1.1 seconds.
-o --only Show only processes doing I/O instead of showing them all.
Can be toggled dynamically by pressing o.
-P --processes Show only processes (processes + threads by default)
-p PIDs --pid=PIDs Monitor PIDs (all by default)
-u users --user=users Monitor processes from the users list (all by default)