date

displays or sets the system date and time (see also hwclock).

date [options] ... [+format]

date [-u | --utc | --universal] [MMDDhhmm[[CC]YY][.ss]]

MM - month, DD - day, hh - hour, mm - minute, CC - century, YY - year, ss - second. If DDMMhhmm... is omitted, ~ displays the current date, otherwise the specified date will be set (to set the date you must be root).

date 020410002011.30

set the system date to 2011-Feb-04 10:00:30;

date +%u

show the day of the week (1..7);

date +%e

show the day of the month without leading 0s (1..31);

date '+%B %d'

output the current date in a format including the full month name and the day of the month;

date +%a -d '1 day ago'

show abbreviated weekday name of the previous day;

date +%A --date='20 days'

show full weekday name of the date that is 20 days ahead;

date -d '100 days'

show the date of the day that is 100 days ahead from now;

date --date='3 months 1 day'

show the date of the day 3 months and 1 day hence;

date --date='25 Dec' +%j

show the day of year of Christmas in the current year;

date --date='21 Feb 2011' +%s

show the num of seconds since '00:00:00 1970-01-01 UTC' up to the specified date (the time is 00:00:00, if not specified);

date --date='21 Feb 2011 00:00' +%s

like previous; the result in both cases is 1298235600;

date --set='21:00:00'

set time to 21:00;

date --set="Feb 10 10:15 2007"

set date to 2007-Feb-10, set time to 10:15;

Some options

--help    --version

-d str

display time described by str (--date=str);

-s str

set time described by str (--set=str);

-u, --utc, --universal

display or set Universal Time Coordinated;

Format
%% literal %;
%a locale’s abbreviated weekday name (Sun..Sat);
%A locale’s full weekday name (Sunday..Saturday);
%b locale’s abbreviated month name (Jan..Dec);
%B locale’s full month name (January..December);
%c locale’s date and time;
%C century (year divided by 100 and truncated to int);
%d day of month (01..31);
%D date (mm/dd/yy);
%e day of month, blank padded (1..31);
%F same as %Y-%m-%d;
%g 2-digit year corresponding to %V week number;
%G 4-digit year corresponding to %V week number;
%h same as %b;
%H hour (00..23);
%I hour (01..12);
%j hday of year (001..366);
%k hour (0..23);
%l hour (1..12);
%m month (01..12);
%M minute (00..59);
%n a newline;
%N nanoseconds (000000000..999999999);
%p locale’s upper case AM or PM indicator;
%P locale’s lower case am or pm indicator;
%r time, 12-hour (hh:mm:ss [AP]M);
%R time, 24-hour (hh:mm);
%s seconds since ’00:00:00 1970-01-01 UTC’ (GNU ext);
%S second (00..60);
%t a horizontal tab;
%T time, 24-hour (hh:mm:ss);
%u day of week (1..7 / Mon, Tue, Wed, Thu, Fri, Sat, Sun);
%U week number of year with Sun as 1st day of week (00..53);
%V week number of year with Mon as 1st day of week (01..53);
%w day of week (0..6 : Sun, Mon, Tue, Wed, Thu, Fri, Sat);
%W week number of year with Mon as 1st day of week (00..53);
%x locale’s date representation (mm/dd/yy);
%X locale’s time representation (%H:%M:%S);
%y past two digits of year (00.99);
%Y year (1970…);
%z RFC-2822 style numeric timezone (-0500), non-std ext.;
%Z time zone (e.g., EDT);

dd

copies a file, converting and formatting according to the options.

dd if=input_file of=output_file [options]

Working with CD/DVD:

dd if=/dev/sr0 of=cdimage.iso

write image of CD (DVD) to a file (cdimage.iso); old (PATA) devices can have names like /dev/hdc, /dev/cdrom, etc;

dd if=cdimage.iso of=/dev/sr0

create new CD/DVD using image file cdimage.iso; of course you need an appropriate device (CD/DVD Writer) and a blank CD-R (DVD+R) disk;

Backup / restore disk partition:

dd if=/dev/sda3 conv=sync,noerror bs=64K | gzip -c > root.img.gz

backup disk partition, compress, overwrite an existing file;

gunzip -c root.img.gz | dd of=/dev/sda3 conv=sync,noerror

restore partition using a compressed image of that partition;

Backup / restore MBR and partition table:

dd if=/dev/sda of=mbr.img bs=512 count=1

save Master Boot Record and partition table to mbr.img;

dd if=mbr.img of=/dev/sda

restore MBR and partition table using file mbr.img;

dd if=mbr.img of=/dev/sda bs=446 count=1

restore MBR, do not touch the existing partition table;

Backup / restore GPT:

GPT table length depends on the number of partitions, and you should not do anything without a preliminary research. First, you must find the exact number of partitions. Assuming dev is /dev/sda:

parted -ms /dev/sda print

(parted is usually present by default). The number of bytes to read would be:

N = (128 * n) + 1024

where n is the number of partitions found by prev cmd. Now, to save GPT to file sda-part.gpt, use:

dd if=/dev/sda of=sda-part.gpt bs=1 count=N

To restore GPT to the same HDD/SSD, or to a new HDD/SSD of the same or larger size (!):

dd if=sda-part.gpt of=/dev/sda bs=1 count=N

If you find this too complicated, try sgdisk.

Random sequences:

dd if=/dev/random bs=1 count=5 | xxd -ps

create a 40-bits random key (xxd converts it to a 10 char string);

dd if=/dev/urandom of=pdatfs bs=1M count=16

create a 10M file (pdatfs) and fill it with random data;

Old examples with floppy disks:

dd if=stage1 of=/dev/fd0 bs=512 count=1

dd if=stage2 of=/dev/fd0 bs=512 seek=1

(Red Hat, Fedora?) create a GRUB boot floppy (stage1/2 files must be in /usr/share/grub/i386-pc);

dd if=bzImage of=/dev/fd0

copy file bzImage to a floppy disk;

dd if=disk2.img of=/dev/fd0 ibs=18k count=80

create floppy diskette using file disk2.img, copy only 1440K;

Some options
bs=n read & write n bytes at a time;
cbs=n convert n bytes at a time;
conv=lst convert file according to a [comma-sep.] keywords list;
count=n copy only n ibs-sized input blocks;
ibs=n read n bytes at a time;
if=file read from file instead of stdin;
obs=n write n bytes at a time;
of=file write to file instead of stdout;
seek=n skip n obs-sized blocks at start of output;
skip=n skip n ibs-sized blocks at start of input;
Following options may be used in keywords list with conv
ascii from EBCDIC to ASCII;
ebcdic from ASCII to EBCDIC;
ibm from ASCII to alternated EBCDIC;
block pad nl-terminated rec with spaces to cbs-size;
unblock  replace trailing spaces in cbs-size rec with nl;
lcase change upper case to lower case;
ucase change lower case to upper case;
noerror continue after read errors;
notrunc do not truncate the output file;
swab swap every pair of input bytes;
sync pad every input block with NULs to ibs-size; if used with block or unblock, pad with spaces;

ddrescue

(GNU ddrescue) is a data recovery tool. It copies data from one file or block device to another, trying to rescue data in case of read errors.

ddrescue -n -b2048 /dev/cdrom cdimage logfile

rescue a CD-ROM: copy CD image to a file (cdimage), then, if operaion succeeds, you can burn it to a blank CD-ROM;

ddrescue -d -b2048 /dev/cdrom cdimage logfile

similar to prev (copy CD image to a file), but use direct access;

ddrescue -n -b 2048 /media/VIDEO_TS/FILE.VOB .

try to copy a damaged file from a DVD to the current dir on HDD (mind a dot at the end); this is a good first pass to save quickly those blocks that can be saved easily, and to estimate the sizes of damaged areas; then, if the damaged area is not that big, you can try the same cmd without -n; NOTE! Some protected DVDs cannot be copied file by file, and ~ may fail absolutely in such case, though DVD can be played without problems;

ddrescue --fill=+ /dev/zero /dev/sda4 logfile

wipe the good sectors; the drive will still test bad (unreadable sectors); this is a good way of wiping the failing drive before sending it for repairment or returning back to shop;

ddrescue --fill=- -D /dev/zero /dev/sda1

force the drive to remap the bad sectors (assuming there are few of them and no deprication); we are trying to rewrite bad sectors and may be the drive would manage to reallocate them to new spare sectors; actually, this procedure may fail;

ddrescue [options] infile outfile [logfile]

Options

-h    --help    -V    --version    -v    --verbose

-b n, --block-size=n

HW block size (in bytes) of input device (default is 512);

-B, --binary-prefixes

show units with binary prefixes; by default SI prefixes (powers of 1000) are used;

-c n, --cluster-size=n

num of sectors to copy at a time (by default n = 128, i.e., 64KiB at a time, provided sector size is 512 bytes);

-C, --complete-only

limit rescue to the blocks listed in logfile, don't read new data beyond the logfile limits (useful when reading from devices of undefined size, like raw dev)

-d, --direct

use direct disk access for input file (must be device or HDD partition); if sector size is not correctly set, all reads will fail;

-D, --synchronous

use synchronous writes for output file;

-e n, --max-errors=n

max num of error areas allowed before giving up; defaults to infinity;

-F t, --fill-type=t

fill given type areas with the input file data;

-g, --generate-logfile

generate an approximate logfile from the input and output files of the original rescue run;

-i p, --input-position=p

starting position in input file (in bytes); by default p = 0; in fill mode it refers to the original input file;

-m file, --domain-logfile=file

restrict rescue domain to the blocks marked as finished in file; this is useful if dest drive fails during the rescue;

-n, --no-split

stop after trimming pass, don't try to rescue the most difficult parts of the file as this may take a lot of time;

-o p, --output-position=p

starting position in output file (in bytes); defaults to input pos; bytes before p are not touched;

-q, --quiet

quiet operation;

-r n, --max-retries=n

exit after n retries (default is 0; -1 means infinity); each sector is tried once per pass, to retry bad sectors detected on a prev pass, specify non-zero n;

-R, --retrim

mark all failured block inside the rescue domain as non-trimmed before beginning the rescue;

-s n, --max-size=n

max size of input data to be copied (in bytes); use this when ~ cannot determine the size of input device;

-S, --sparse

use sparse writes for output file;

-t, --truncate

truncate outfile to zero size before writing;

Numbers may be followed by multipliers:

b = blocks

k = kB = 103 = 1000

Ki = KiB = 210 = 1024

M = 106 = 1000000

Mi = MiB = 220 = 1048576

G = 109

Gi = GiB = 230

df

displays the amount of disk space available on the filesystem containing each file arg. file can also be a device name, but fs must be mounted. Without this arg the space available on all currently mounted filesystems is shown.

df -m

report disk space usage in MB (all mounted filesystems);

df -h

report disk space usage in human readable format;

df -i /u02/templates

report inode usage in fs where /u02/templates resides;

df [options] [file] ...

Some options

--help    --version

-a, --all

include filesystems having 0 blocks;

-B n, --block-size=n

use n-byte blocks;

-h, --human-readable

use human readable format;

-H, --si

like prev, but use powers of 1000, not 1024M;

-i, --inodes

list inode info instead of block usage;

-k

use 1K blocks (like --block-size=1K);

-l, --local

limit listing to local filesystems;

-P, --portability

use the POSIX output format;

-t t, --type=t

limit listing to the filesystems of the specified type;

-T, --print-type

print filesystem type;

-x t, --exclude-type=t

exclude from listing filesystems of the specified type;

--no-sync

do not invoke sync before getting usage info (default);

--sync

invoke sync before getting usage info;

diff

finds differences between two files.

diff -y msg01.txt msg02.txt

find differences between msg01.txt and msg02.txt, show results in a side by side format;

diff -r /etc ./bkp/etc

compare directories recursively (that is, compare each file in the first dir to the corresponding file in the second dir);

diff [options] file1 file2

If one of the specified files is a directory, ~ looks in this dir for an appropriate file (named like another file specified on the cmd line). If both args are dirs, then corresponding files in these dirs are compared in alphabetical order. The procedure is not recursive unless -r is given. Actual contents of dirs [taken as files] are never compared.

Some options

--help    -v    --version

-a, --text

treat all files as text and compare them line-by-line, even if they do not seem to be text;

-bignore variations in amount of white space;
-Bignore variations in amount of blank lines;
-cuse the context output format;

-C [n], --context[=n]

use the context output format, showing n lines of context (default is 3);

-d change the algorithm to find a smaller set of changes;

-D name

make merged if-then-else format output, conditional on the pre-processor macro name;

-e, --ed

make output that is a valid ed script;

-f make output that looks like an ed script, but has changes in the order they appear in the file;

-F regexp

in context and unified format, for each hunk of differences, show some of the last preceding line matching regexp;

-H use heuristics to speed handling of large files that have numerous scattered small changes;

-i, --ignore-case

ignore variations in case;

-I regexp, --ignore-matching-lines=regexp

ignore changes that just insert/delete lines matching regexp;

-l, --paginate

pass the output through pr to paginate it;

-L label, --label=label

use label instead of the file name in the context format and unified format headers;

-n, --rcs

output RCS-format diffs (similar to -f except that each command specifies the number of lines affected);

-N, --new-file

in directory comparision, if a file is found in only one dir, treat it as present but empty in the other dir;

-p associate all changes with the corresponding C-functions;
-P in directory comparision, if a file appears only in the second dir, treat it as present but empty in the other dir;
-q report only whether the files differ, without details;

-r, --recursive

in dir comparision, recursively compare subdirs;

-s report when two files are the same;

-S file

when comparing dirs, start with the specified file (may be used to resume an aborted procedure);

-t expand TABs to spaces in the output to preserve the alignment of TABs in the input files;
-T output a TAB rather than a space before the text of a line in normal or context format;
-u use the unified output format;

-U [n], --unified[=n]

use the unified output format, showing n lines of context (default is 3);

-w ignore white spaces when comparing lines;

-W n, --width=n

use the specified width of columns in side by side output format;

-x pattern

when comparing dirs, ignore files and subdirs whose basenames match the specified pattern;

-X file

when comparing dirs, ignore files and subdirs whose basenames match any pattern from the specified file;

-y use the side by side output format;

dig

gets domain info from the DNS servers. You can find some useful definitions in DNS-related info. DIG means "domain information groper".

dig [@server] [options] [name] [type] [queryopt]

A typical invocation looks like:

dig @server name type

server (name or IPv4/IPv6 address) is DNS to be used (by default those listed in /etc/resolv.conf will be used), name is the name of the resource record to be looked up (usually domain or host name), type is any valid query type like ANY, A (default), MX, SIG, etc.

dig mail.acme.com -t A

get the IP address of the host mail.acme.com;

dig @195.133.122.20 mydomain.net

show records for mydomain.net using the specified name server (195.133.122.20);

dig mydomain.net -t mx

show MX records (mail servers) for mydomain.net;

dig -x 128.9.0.32

show the hostname of the node with IP 128.9.0.32;

dig +qr www.acme.com any -x 127.0.0.1 mydomain.ru ns +noqr

make three lookups in one cmd: an ANY query for www.acme.com, a reverse lookup of 127.0.0.1 and a query for the NS record of mydomain.ru; a global query option +qr is applied to all queries except the last one that is affected by the local query option +noqr;

dig +multiline @ns-pri.example.com
2.0.192.in-addr.arpa NS

query against the authoritative server for the NS in 2.0.194.in-addr.arpa; this is usually the first check you should do to verify if the name server setup is correct;

dig 2.0.192.in-addr.arpa +nssearch

look for the authoritative servers for 2.0.192.in-addr.arpa;

dig is mostly used as cmdline tool, but it also has a batch mode of operation for reading lookup requests from a file (see -f option).

Query type
A network address (default);
ANY all information about specified domain;
MX mail exchangers;
NS name servers;
SOA zone of authority record;
HINFO host information;
AXFR zone transfer;
IXFR incremental zone transfer;
TXT arbitrary number of strings;
Query class

IN (Internet, default); HS (Hesiod); CH (Chaosnet);

Some options
-h help;
-v version;
-4 use IPv4 query transport only;
-6 use IPv6 query transport only;

-b addr

set the src IP addr of the query (must be one of the valid host's IPs or 0.0.0.0 or ::);

-c query-class

set query class (default is IN);

-f file

specify the name of a file containing a list of query specs (one per line) which are to be executed successively; lines beginning with ';', '#', or '\n' are ignored; other options may still appear on cmdline, and will be in effect for each query;

-k file

specify TSIG key file (to sign queries sent by ~ and their responses using transaction signatures);

-m enable memory usage debugging;

-p nn

use the specified port instead of DNS standard 53;

-q name

set query name to name;

-t query-type

set query type (default is A);

-x addr

reverse lookup; addr is either IPv4 dotted-decimal notation, or IPv6 colon-delimited address;

-y name:key

specify named base64 TSIG key (unsafe);

Some query options

+nssearch

try to find the authoritative name servers for the zone containing the name being looked up and display the SOA record that each name server has for the zone;

+[no]tcp

use (do not use) TCP when querying name servers (default is UDP for all query types except AXFR/IXFR);

+time=t

set query timeout in seconds (def is 5s, min is 1s);

+trace

follow the delegation tree from the root down and show all the steps in the recursion;

+tries=n

set the number of tries for UDP queries (default is 3);

+retry=n

set the number of retries for UDP queries (default is 2);

+domain=name

set the default domain name;

+[no]showsearch

set whether to show intermediate search results;

+[no]short

provide a terse answer;

+[no]cmd

control display of command line;

+[no]comments

control display of comment lines;

+[no]qr

print the query as it is sent (default is 'no');

+[no]all

set or clear all display flags;

+[no]multiline

print records like SOA in a verbose multi-line format with human-readable comments;

dos2unix, unix2dos

dos2unix converts text files from DOS/Mac to Unix format, unix2dos converts text files from Unix to DOS/Mac format. Ubuntu users, look for fromdos and todos.

dos2unix [options] file

unix2dos [options] file

dos2unix *txt

convert all files with the matching names in the current dir from DOS to Unix format (it's assumed that all files are text);

dos2unix -k *html

convert all HTML files in the current dir from DOS to Unix format preserving original date stamp;

dos2unix

get input from stdin and write output to stdout;

unix2dos -k *html

convert all HTML files in the current dir from Unix to DOS format preserving original date stamp;

Options

-h    --help    -V    --version

-c mode, --convmode mode

set conversion mode, which can be ascii (default), iso, mac;

-k, --keepdate

keep the date stamp of the output file the same as the date stamp of the input file;

-q, --quiet

quiet mode, suppress all warning msgs;

du

shows disk space usage in 1K blocks. However, if POSIXLY_CORRECT env variable is set, then block size is 512 bytes.

du -hs

show grand total for the current dir;

du --max-depth=1 -h

show grand total for each subdir of the current dir in human readable format (i.e. numbers with letters K, M, G);

du --block-size=1 --max-depth=1

show grand total in bytes for each subdir of the current dir;

du -k -max-depth=1 | sort -nr

show grand total in kilobytes for each subdir of the current dir sorted by size in reverse order (largest first);

du [options][file] ...

Options

--help    --version

-a, --all

write counts for all files, not just directories;

-B n, --block-size=n

use n-byte blocks;

-b, --bytes

print size in bytes; equivalent to --apparent-size --block-size=1;

-c, --total

produce a grand total;

-D dereference file that is a symbolic link;

-H, --si

use powers of 1000, not 1024;

-h, --human-readable

use human readable format;

-k, --kilobytes

like --block-size=1K;

-l, --count-links

count sizes many times if hard linked;

-L, --dereference

dereference all symbolic links;

-P, --no-dereference

don't follow any symbolic links (default);

-0, --null

end each output line with '\0' rather than a newline;

-S, --separate-dirs

do not include size of subdirs;

-s, --summarize

display only a total for each arg;

-x, --one-file-system

skip dirs on different filesystems;

-X file, --exclude-from=file

exclude files that match any pattern in file;

--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, etc;

--exclude=pattern

exclude files that match the pattern; shell pattern is assumed (?, *), not a regexp;

--max-depth=n

print the total for a dir or file only if it's n or fewer levels below; --max-depth=0 is equivalent to --summarize;

dumpe2fs

displays ext2/ext3/ext4 filesystem info.

dumpe2fs -h /dev/sda4

show fs superblock info (all that you want, usually);

dumpe2fs -b /dev/hda7

display the bad blocks list for the specified filesystem;

dumpe2fs [options] device

Some options
-b print the blocks which are reserved as bad in the filesystem;

-ob n

use block n as a superblock when examining a filesystem (for badly corrupted filesystems);

-oB n

use blocks of n bytes when examining a filesystem (for badly corrupted filesystems);

-f force ~ to display a filesystem even though it may have some feature flags which ~ may not understand;
-h only display the superblock info;
-i display fs data from an image file created by e2image, using device as the pathname to the image file;
-x print the detailed group info block numbers in hex format;
-V show version and exit;