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

modprobe

Usage :

Add and remove modules from the Linux kernel

Flags :

Flag Usage
-a pattern load all modules matching the pattern conditions
-c display current configuration not equivalent to lsmod
-r moduleName remove (=unload) the module moduleName
-t type affect modules of the corresponding type
Example, to list all i2c modules : modprobe -l -t i2c

md5sum

Usage :

MD5 (Message-Digest algorithm 5) was created for encryption purposes, but now, it's mostly used to check file integrity.
To compute the MD5 hash of myFile :
md5sum myFile

Example :

Check file integrity :

When downloading a large file (file.iso), you're also offered to retrieve a short plain text file usually named MD5SUM. This file contains the calculated md5 hashes of the file.iso in order to let you check its integrity after the download. To do so :
  1. download both MD5SUM and file.iso into the same directory
  2. check file integrity : md5sum -c MD5SUM
  3. if the downloaded file.iso is an exact copy of the remote file.iso, it will display : file.iso: OK

Get the MD5 hash of an arbitrary string from the command line :

Single line string :
echo -n hello | md5sum
will return :
5d41402abc4b2a76b9719d911017c592 -

-n is required because echo appends a newline character to the output. Without this flag, the encoded string wouldn't be hello but hello\n, resulting in a different hash (source).

Multiple line string :
  1. md5sum -
  2. blablabla
  3. bliblibli
  4. blobloblo
  5. CTRL-d
  6. Should return : c3567beab1ff181e1d7b07d189437361 -

mkdir

Usage :

Make a directory

Flags :

Flag Usage
-m octalMask --mode=octalMask Set file mode (as in chmod) using the mask octal notation format (i.e. : 644).
  • Do not use the mask symbolic notation format (i.e. : u=rw,g=r,o=r), this may have undesired effects.
  • There is no incompatibility between -m and -p, BUT if the target directory already exists :
    1. its mode will not be updated
    2. this will happen silently
-p --parents Make parent directories as needed, return no error if existing.

mv

Usage :

move and rename files

Flags :

Flag Usage Details
-i --interactive prompt before overwrite
-f --force do not prompt before overwriting
Looks like this is the default. See -i
echo LEAD > lead; echo GOLD > gold; mv lead gold; cat lead gold; rm gold
cat: lead: No such file or directory	missing because renamed
LEAD					this is gold, overwritten without prompting
-n --no-clobber do not overwrite an existing file
When this applies, no UNIX_FAILURE is generated
echo LEAD > lead; echo GOLD > gold; mv -n lead gold; echo $?; cat lead gold; rm lead gold
0	UNIX_SUCCESS
LEAD	no file was altered
GOLD

Example :

mv errors :

cannot move thisDirectory : Device or resource busy :
  1. With lsof and fuser, make sure no process is actually accessing thisDirectory :
    • It's likely there is an open shell which current directory is thisDirectory (in such situation, you'll find a Bash process using thisDirectory). Just cd somewhere else or terminate this process.
    • Kill blocking processes (fuser -k ) then retry.
  2. If no process is found but the error is still there, make sure the target directory is not a mount point.
mv: failed to preserve ownership for 'someFile': Permission denied (source) :
mv does its best to preserve metadata (permissions + owner/group information) when moving files around. But when the destination is not on the same filesystem than the source, this sometimes becomes impossible for reasons that are beyond mv (destination filesystem type, mount options, local vs remote user accounts, ...)
  • this is caused by the context, this is not a bug / limitation of mv
  • there is no command flag / hack / workaround
  • to move stuff to a different filesystem, use cp + rm instead

mktemp

Usage :

Create a safe temporary file or directory and display its name :

mktemp [options] template

template must contain at least 3 consecutive X in last component.

This is required to circumvent symlink exploits.

Flags :

Flag Usage
-d --directory create a directory, not a file
-u --dry-run do not create anything
--suff=suffix append suffix to template. The same happens if template does not end with an X
-p dir
--tmpdir=dir
  • create the temporary file into dir
  • If dir is omitted, use $TMPDIR instead
  • If $TMPDIR is not set, use /tmp

Depending on the context, it _may_ be more appropriate to create temporary files into a RAM disk.

This option doesn't exist in Bash 3.x. Use mktemp /path/to/dir/tmpFile.XXXX instead.

Example :

Create a temporary file in ... Command Creates Notes
the current directory mktemp tmpFile.XXXX ./tmpFile.4zxA
tmpFile=$(mktemp XXXXXXXX.tmp); do whatever you like with temp file; rm "$tmpFile"
any specified directory mktemp --tmpdir=./testDir/ test_File_XXXXXXXXXXX ./testDir/test_File_XTRr0VPklFg
the "temporary directory"
  • mktemp --tmpdir thisIsATempFile.XXXXXXXX
  • mktemp
  • /tmp/thisIsATempFile.9vEZbi0t
  • /tmp/tmp.fHBAF5mFbN
the "temporary directory", by a script
  • tmpFile=$(mktemp --tmpdir theNameOfMyTempFile.XXXXXXXX)
  • tmpFile=$(mktemp --tmpdir $(basename $0).XXXXXXXXXXXXXXXXX)
  • tmpFile=$(mktemp --tmpdir='/run/shm' $(basename $0).tmp.XXXXXXXXXXXX)
  • /tmp/theNameOfMyTempFile.Ts5yP2F6
  • /tmp/scriptName.sh.KXfKK7mYN0hFMSQwz
  • /run/shm/scriptName.tmp.eBpsQUZCrDO4

mtr

Usage :

mtr (My traceroute, originally called Matt's traceroute) is a computer program which combines the functionality of the traceroute and ping programs in a single network diagnostic tool.

mtr probes routers on the route path by limiting the number of hops individual packets may traverse, and listening to responses of their expiry. It will regularly repeat this process, usually once per second, and keep track of the response times of the hops along the path.

mount

Usage :

Mount a filesystem by "attaching" it to a location found on the local host (a.k.a a mount point).

Some of the mount options are filesystem-specific.

Flags :

Flag Usage
-a --all mount all filesystems mentioned in /etc/fstab
--bind oldDir newDir Allows to remount part of the file hierarchy somewhere else. After this call the same content is accessible in two places.
-o options options :
  • ro, --ro : mount as read only
  • rw, --rw : mount as read/write
  • remount : remount an already-mounted filesystem

Example :

Remount a volume as rw :

  • umount /boot && mount /boot --rw
  • mount -o remount --rw /boot

Get a nice display of mounted volumes (source) :

mount | column -t

mail

Usage :

Send or read emails from the shell.

Flags :

Flag Usage
-s specify mail subject

Example :

Send a simple mail (source) :

  1. mail -s "subject" recipient@domain.com

    When sending a mail to a localhost user, specifying recipient@domain.com, recipient@localhost or just recipient has the same result.

  2. If you didn't enter the mail subject in the command line, type it now after the prompt : Subject: Enlarge your tennis
  3. Type mail body :
    Roses are #FF0000
    Violets are #0000FF
    All of my base
    Are belong to you
  4. Terminate the mail body with : . then , or with CTRL-d.
  5. At the Cc: prompt, just hit CTRL-d again.

When mailing a localhost recipient, the /var/mail/recipient file must exist and be writable by the sender.

Send the content of a file as the mail body :

  • cat file | mail -s 'subject' recipient@domain.com
  • mail -s 'subject' recipient@domain.com < file

Be notified of a new mail in the terminal :

To receive "New mail" notifications in the terminal, you just have to set some variables in ~/.bashrc :
MAILPATH=/var/mail/bob
MAILCHECK=60
For details on MAILCHECK : man bash, then search : / MAILCHECK

mkfs

Usage :

make a filesystem

The Internetz are full of discussions like : What is the best filesystem ?

See Comparison of file systems.

Flags :

Flag Usage
-b block-size Specify the size of blocks in bytes. Valid block-size values are 1024, 2048 and 4096 bytes per block. If omitted, the block size will be determined from the filesystem size and usage (see -T)
-I set the Inode size
IFS Drive doesn't support inodes larger than 128 bytes (source)
-L set the volume Label
-m Specify the percentage of the filesystem blocks reserved for the super-user (default is 5%)
-T usageType Specify how the filesystem will be used so that optimal parameters can be applied. Supported usageType values are listed in /etc/mke2fs.conf

Example :

EXT4

  • mkfs.ext4 -I 128 -L ConPow -m 0 /dev/ConPow
  • mkfs.ext4 -L ConPow -m 0 -T ext4 /dev/sde1

FAT32

mkfs.vfat -L ConPow /dev/ConPow
This command is not verbose, but don't worry