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

mail

dmsetup

low level logical volume management : dmsetup manages logical devices that use the device-mapper driver.
Usage :
dmsetup command [flags] device
mail

dirname

dirname [options] name
mail

disown

Usage

To do so :

source : help disown

mail

deborphan

Usage

deborphan is not a Bash command, but a Debian-specific utility. It can be installed via the deborphan package.

deborphan looks for orphaned packages, that is packages which are not required by any other package upon your system.

Flags

Flag Usage
(none) only list packages which are in the libraries section of the Debian archive, as these are the most likely candidates for removal
-s --show-section show the sections the packages are in
-z --show-size show the installed size in KiB of the listed packages
--guess-data show library (default) + data orphaned packages
--guess-all show all orphaned packages
There are many --guess-* commands available. See the GUESSING section of man deborphan.

Example

List orphaned packages :

deborphan
libssl1.0.0
libgnome2-bin
liblognorm1
libpsl0
libprocps3
libboost-iostreams1.55.0
deborphan --guess-all -sz
	111 main/net			libxtables10
	183 main/python			python3-cffi-backend
	352 main/java			libservlet2.5-java
	 53 main/perl			libclass-isa-perl
	458 main/admin			software-properties-common
	
This doesn't require root privileges since it just reads the installed packages lists.

Remove orphaned packages :

As root :

deborphan | xargs apt-get purge

Commands below have the -y flag which will uninstall packages with no prompt (but there should be no risk as apt-get is smart enough to avoid shooting its own foot ).

mail

dmesg

Usage

dmesg's main usage is to display messages from the kernel ring buffer. It lists events in the form :

[timestamp] message
where timestamp is the number of seconds.nanoseconds since the system booted.

Information reported by dmesg is also available in /var/log/kern.log

The kernel ring buffer :

The kernel ring buffer is a memory area containing kernel messages. It's a fixed-size FIFO area.

Flags

Flag Usage
-c --read-clear print all messages then clear the buffer
-T --ctime Print human-readable Timestamps
Be aware that the timestamp could be inaccurate!
-w --follow wait for new messages
-W --follow-new Wait and print only new messages
mail

declare

Usage

declare (or its synonym typeset) is used to change properties of variables

Flags

Flag Usage
-a myVariable declare myVariable is an indexed array (example)
-A myVariable declare myVariable is an associative array (example)
-f list existing Bash functions (names + code)
-F list existing Bash functions (names only)
-i myVariable declare myVariable is an integer
-r myVariable declare myVariable is read-only
Both are valid :
  • declare + assign at once :
    declare - myVariable=value
  • declare then assign :
    declare - myVariable
    myVariable=value

Example

Declare a variable as readonly :

declare -r a=1; echo $a; a=2
1
-bash: a: readonly variable
Or : readonly b=2; echo $b; b=1
2
-bash: b: readonly variable

readonly variables exist until the end of the shell process.

How to unset a readonly variable ? (source)

readonly a=1; echo $a
1
unset a
bash: unset: a: cannot unset: readonly variable
cat << EOF | gdb
> attach $$
> call unbind_variable("a")
> detach
> EOF
GNU gdb (GDB) 7.4.1-debian
Copyright (C) 2012 Free Software Foundation, Inc.

(gdb) Attaching to process 29720
Reading symbols from /bin/bash...(no debugging symbols found)...done.

Loaded symbols for /lib/x86_64-linux-gnu/libnss_files.so.2
0x00007fd61bed006e in waitpid () from /lib/x86_64-linux-gnu/libc.so.6
(gdb) $1 = 0
(gdb) Detaching from program: /bin/bash, process 29720
(gdb) quit
echo $a
(nothing)

What's the difference between declare and readonly ? (source)

The variable scope :
  • declare makes it local
  • readonly makes it global

As of bash 4.2, declare -gr seems to be identical to readonly.

mail

df

Usage

Report disk free space
You may face weird situations where
  • deleting a file doesn't seem to free any storage space
  • df reports 100% disk usage whereas du can't detect where storage is actually used
Read more about this here.
Should you need to list several filesystems (but not all of them) as well as the header line (describing data fields, but language-dependent), there's no need to use grep, just remember df accepts a list of filesystems :
df -h /var/lib/mysql /var/lib/mysql/backup
Sys. fich.		Taille	Util.	Dispo	Uti%	Monté sur			Language-dependant header line
/dev/mapper/vg1-mysql	55G	15G	37G	29%	/var/lib/mysql
/dev/mapper/vg2-dump	20G	9,9G	8,9G	53%	/var/lib/mysql/backup

Flags

Flag Usage
-h --human-readable print sizes in human readable format (e.g. 1K, 234M, 2G)
-i --inodes report inode usage instead of block usage
-P --portability use the POSIX output format :
  • no nice tabular display anymore
  • a single line per filesystem, even though the filesystem name is long
To be used in a df -P | grep context.
-T --print-type print filesystem type
This can be used to determine which filesystem a file belongs to :
df -T $HOME
Filesystem			Type	1K-blocks	Used	Available	Use%	Mounted on
/dev/mapper/caramba--vg-home	ext4	25066604	4086168	19684052	18%	/home

Example

Check filesystems for disk full/almost full :

  • When above 90% of used disk space, there may be No space left on device to create a new BIG file there
  • There may be no inodes left, even though some (kilo|mega|giga|tera)bytes are available
Check this with :
echo 'Disk usage :'; df -Ph | grep -E '(9.|100)%'; echo 'Inodes :'; df -Phi | grep '100%'

Alternate syntax :

This allows specifying a threshold much more easily :
df -Ph | tr -d '%' | awk '$5 > 90 { print $0 }'

Find usage percent of filesystem holding /path/to/myFile :

df is actually smart enough to determine which filesystem holds /path/to/myFile and report the usage :

df -h "$HOME/.bashrc"
Filesystem			Size	Used	Avail	Use%	Mounted on
/dev/mapper/caramba--vg-home	24G	3.9G	19G	18%	/home

One-liner for scripts :

df -h "$HOME/.bashrc" | awk '$6 ~ "^/" { print $5 }'
18%
mail

dig

Usage

dig : DNS lookup utility
Forward DNS lookup (name into IP) :
  • dig name
  • dig name recordType
  • dig @dnsServer name
Reverse DNS lookup (IP to name) :
dig -x ip.add.re.ss

Example

Investigate DNS lookup time :

#!/usr/bin/env bash

logFile=$0.csv
hostName='www.example.com'
serversList='DNS servers list'
echo "DNS Server;Time [ms]" > $logFile
for dnsServer in $serversList; do
	echo -n .
	echo -n "$dnsServer;" >> $logFile
	dig @$dnsServer $hostName | grep 'Query time' | cut -d ' ' -f 4 >> $logFile
done

Read TXT records of a domain :

dig google.com txt

Get a short response (IP only) :

Instead of dig www.example.com, which is rather verbose, get only the IP address using the +short flag : dig +short www.example.com
mail

dd

Usage

dd stands for data duplicator (source) but there is no consensus on the meaning of dd...

Flags

Flag Usage
if=file input file. This can be :
of=file output file
bs=n[unit] block size
  • n : n bytes
  • nkB : n*1000 bytes
  • nK : n*1024 bytes
  • nMB : n*1000*1000 bytes
  • nM : n*1024*1024 bytes
  • nGB : n*1000*1000*1000 bytes
  • nG : n*1024*1024*1024 bytes
bs=64k only makes the transfer go faster because dd will be reading blocks of 64k each instead of the default block size (source).
count=n generate n blocks of the specified size
iflag=FLAG1,FLAG2,FLAGn apply the specified flags to the read (input) file
oflag=FLAG1,FLAG2,FLAGn apply the specified flags to the written (output) file
seek offset
status=level the level of information to print to stderr :
  • none : suppresses everything but error messages (mimics a quiet mode)
  • noxfer : suppresses the final transfer statistics
  • progress : shows periodic transfer statistics

Flags :

  • direct : use direct I/O for data

Example

How to create a test file having a specific size ?

For 100MB of 0s :
dd if=/dev/zero bs=1M count=100 of=some/file
There is a faster method (limited to ext4, btrfs, xfs and ocfs2) : fallocate. This command actually preallocates blocks to a file but doesn't write the whole file, which will look empty.
Suffices like k, m, g and t stand for KiB, MiB, GiB and TiB respectively.
fallocate -l 100m some/file
mail

dpkg

Flags

Flag Usage
-b myPackage
--build myPackage
build the myPackage.deb package (details)
-c packageName
--contents packageName
show contents of packageName
packageName has to be available on the local filesystem
-i packageName install the package packageName
Before actually installing, check dependencies are met with (source) :
dpkg --dry-run -i packageName
packageName has to be available on the local filesystem
-I packageName
--info packageName
show information about packageName
packageName has to be available on the local filesystem
-l list all the installed packages
-L packageName List files installed by the package packageName
-P packageName Purge the package packageName : completely uninstall the package including binaries and configuration files
-r packageName remove the package packageName, leaving the configuration files untouched.
-s packageName
--status packageName
display status information about the package packageName. (this actually reads the package control file)
list packages on which packageName depends
-S someFile
--search someFile
display which package installed someFile. Check it :
directory='/usr/bin'; nbFiles=$(ls -1 "$directory" | wc -l); randomNumber=$(shuf -i 1-$nbFiles -n 1); randomFile=$(ls -1 "$directory" | sed -n "$randomNumber p"); echo "Random file : '$directory/$randomFile'"; dpkgSResult=$(dpkg -S "$directory/$randomFile"); echo "... is from package : '$dpkgSResult'"; packageName=$(echo "$dpkgSResult" | cut -d':' -f1); echo "... which also installed : "; dpkg -L "$packageName" | grep "$directory"
If this fails saying :dpkg-query: no path found matching pattern someFile, it may be because someFile is a symlink.

Exit Status

dpkg -l status codes (details) :

  • 1st character : expected status of the package
  • 2nd character : current status of the package
    • F : halF configured (configuration failed for some reason)
    • H : Half installed (installation failed for some reason)
    • U : Unpacked
    • W : triggers aWaiting (package is waiting for a trigger from another package)
    • c : configuration files are still present in the system
    • i : installed
    • n : not installed
    • t : triggers pending (package has been triggered)
  • 3rd character : error flag
    • : (empty) no error
    • R : Reinstall required (package broken, reinstallation required)
Common codes :
  • hi : installed and will stay in the current version (because of hold)
  • ii : package is installed and everything is going extremely well
  • rc : package has been removed (i.e. cleanly uninstalled) but its configuration files are still there
  • un : unknown + not installed, a package that has never been installed on this system

List of all existing codes :

Example

List dependencies of packageName (see also aptitude why) :

The packages packageName depends on :

  • dpkg -s packageName | grep "^Depends"
  • apt-cache show mysql-server | grep Depends
  • or even better : apt-cache depends mysql-server
Depends: mysql-server-5.5

The packages that depend on packageName (source) :

apt-cache rdepends mysql-server
mysql-server
Reverse Depends:
  mysql-server-5.5
 |yubikey-val		meaning of this | ? Read below.
 |yubikey-ksm
 |spip
 |prometheus-mysqld-exporter
  openstack-cloud-services

What does the | in the output of apt-cache rdepends packageName mean ? (source) :

As seen above, apt-cache rdepends packageName lists the packages that depend on packageName :
apt-cache rdepends mutt
mutt
Reverse Depends:
  t-prot
  notmuch-mutt
  devscripts
  urlview
  urlscan

 |sylph-searcher	sylph-searcher depends on mutt or an alternate package (shown by the |)

  mixmaster
 |metche
  lbdb

  automysqlbackup	automysqlbackup depends on mutt, and there's no alternative
the listed packages have some kind of dependency on mutt (Depends, Recommends, Suggests, Enhances, ...)
check it : apt-cache show sylph-searcher metche automysqlbackup | grep -E '^Package|mutt'
Package: sylph-searcher
Suggests: sylpheed | claws-mail | mutt | wl	one of these will suffice

Package: metche
Depends: debconf (>= 0.5) | debconf-2.0, mutt | mailx | mail-transport-agent, bzip2, ucf	debconf AND (mutt OR mailx OR mail-transport-agent) AND bzip2 AND ucf

Package: automysqlbackup
Recommends: mutt	no alternative

Check whether packageName is installed :

  • dpkg -l | grep packageName
  • dpkg -l packageName &>/dev/null && echo 'INSTALLED' || echo 'NOT INSTALLED'
mail

dmidecode

DMI table decoder. To extract the whole tables, just run dmidecode as root.
The -t (or --type) flag displays the available tables : Then, to get details about a specific component : dmidecode -t processor.
It is also possible to query tables numerically (codes available with man dmidecode). Information about the motherboard (2) and on-board devices (10) : dmidecode -t 2,10
mail

du

Usage

Estimate disk space usage
You may face weird situations where
  • deleting a file doesn't seem to free any storage space
  • df reports 100% disk usage whereas du can't detect where storage is actually used
Read more about this here.

Flags

Flag Usage
--apparent-size print apparent sizes, rather than disk usage; although the apparent size is usually smaller, it may be larger due to holes in ('sparse') files, internal fragmentation, indirect blocks, and the like
-c display a grand total
--max-depth depth (Linux)
-d depth (*BSD ?)
display an entry for all files and directories depth directories deep max-depth conflicts with s
-h display results in human readable format
-m display file size in megabytes
-S --separate-dirs ignore subdirectory sizeThis REALLY ignores subdirectories !
-s summarize : display only a total for each argument s conflicts with max-depth
-x only consider files that are on the same filesystem (=device) than the file given as parameter : du -x /. This prevents going into /mnt/... filesystems.

Example

Size of a directory :

du -sh /path/to/directory/

Size of children directories :

commands below rely on that sort is smart enough to sort human units
generic (lists directories only) :
du -h . --max-depth=1 | sort -hr | head -20
alternate (also lists files) :

The specified path must end with /*

du -xhs ./* | sort -hr

Size of files only contained in children directories (ignoring deeper directories) :

du -S -sh * | sort -nr

Find which directory is eating megabytes on the / filesystem :

du -mx / | sort -nr | less

After copying data from a server to another, the reported "disk usages" are different. Why ? (source : 1, 2, 3)

Keep in mind that :
  • du is about the disk usage. And disk usage != sum of file sizes.
  • du reports estimates (for byte-count precision of disk usage, consider --apparent-size)
It is entirely possible that the same set of files use different amount of disk space. This is because of the intricacies of the file system (including block size, internal fragmentation, ...) (source, details).