Utilities

alias

create, list, and delete command aliases

Print list of current aliases

alias
alias bc='bc -q -l'
alias ll='ls -l --color=tty'
alias r='fc -s'
alias temp='cat /proc/acpi/thermal_zone/THM/temperature'

Create new alias named "memu" (see: awk example)

alias memu="awk -f /home/eberg/scripts/memu.awk"

Remove alias for "temp"

unalias temp

apropos

search manual pages names and descriptions

Show matching commands for stat in section 1 of the manual

apropos -s 1 stat
acpi (1)             - Shows battery status information
kpsestat (1)         - compute octal mode from mode of existing file
lpq (1)              - show printer queue status
lpstat (1)           - print cups status information
mailstat (1)         - shows mail-arrival statistics
skill (1)            - send a signal or report process status
snice (1)            - send a signal or report process status
stat (1)             - display file or file system status
xtrapstats (1x)      - XTrap sample clients

at

queue jobs for later execution

Schedule a task to run at 11:05 a.m.

at 11:05am

Schedule a task to run 24 hours from now

at tomorrow

Teatime? schedule a task to run at 4:00 p.m.

at teatime

Schedule a task to run five minutes from now

at now + 5 minutes

Print command for scheduled job 22 in queue

at -c 22

atq

show jobs in queue

List jobs in the at queue for current user (tmtowtdi: at -l)

atq 
22      2006-02-15 11:05 a eberg
24      2006-02-15 16:00 a eberg
25      2006-02-15 09:13 a eberg
23      2006-02-16 09:08 a eberg

atrm

remove jobs from queue

Remove job ID from atq (tmtowtdi: at -r ID)

atrm ID

awk

pattern scanning and processing language

Print the login name and user's full name from /etc/passwd, separate output with a comma

awk -F: '{print $1 "," $5}' /etc/passwd | sort
ber,Brian Redman
bub,Bubbette McLeod
eb,Erik Berg
jmiz,John Mizel
sklarm,Mike Sklar

An awk for loop

awk 'BEGIN { for (i=1; i<=5; i++) print i }'
1
2
3
4
5

An awk while loop

awk 'BEGIN { i=1; while (i<=5) { print i; i++; } }'
1
2
3
4
5

Print amount of non-swapped physical memory (RSS) in use by current user

ps ux | awk '{ total += $6 } END { print "Mem usage: " total }'
Mem usage: 336128

More complex example of the above. Print physical memory usage of all users on system sorted by memory used. Save the following to a file named memu.awk

BEGIN {
    ps = "ps -eo user,rss | sed 1d"
    while ((ps | getline) > 0) {
        memtotal[$1] += $2
    }
    close(ps)

    for (name in memtotal)
        printf("%-8s %10d\n", name, memtotal[name]) | "sort -k2 -rn"
}

Execute the script directly or save it to an alias

awk -f memu.awk
eb           223420
root          81272
apache        45912
mysql         18492
ntp            4368
nscd           2556
xfs            1956
dbus            960
rpc             540

This example looks for 27/Apr in the web server log file. Then, it counts, sorts, and prints the IP address based on the number of times it showed up in the log

awk '/27\/Apr/ { num[$1]++ } \
END { for (ip in num) printf("%-15s %7d\n", ip, num[ip]) }' access_log|sort -k2 -n
66.x.65.19           10
192.x.112.194        11
24.x.217.45          11
66.x.103.72          14
70.x.17.225          15
206.x.249.251        16
64.x.85.75           17

Awk can be used to execute other commands with its system function. This is a crude and quick way to block the IP of a remote system launching PHP XML-RPC, drupal, mambo attacks

tail --follow=name /var/log/httpd/access_log | awk '/mambo|xmlrpc.php|drupal/ \
{ system("iptables -A INPUT -p tcp --dport 80 --source " $1 " -j DROP") }'

Sort disk usage output in human readable format. Change command to du -sb * to see actual file sizes, not the amount of disk being used. For greater precision, adjust the printf command. Save the following to a file named du.awk

#!/usr/bin/awk -f

BEGIN {
  FS="\t" # separate fields by tabstops--don't put tabs in your filenames, haxor
  du = "du -sB 1 * | sort -n"

  while ((du | getline) > 0) {
    if ($1 < 1024)
        printf("%dB\t%s\n", $1, $2)
    if (1024^1 <= $1 && $1 < 1024^2)
        printf("%.0fK\t%s\n", $1/1024^1, $2)
    if (1024^2 <= $1 && $1 < 1024^3)
        printf("%.1fM\t%s\n", $1/1024^2, $2)
    if (1024^3 <= $1)
        printf("%.2fG\t%s\n", $1/1024^3, $2)
  }
}

Execute the script directly. Note by including the shebang (#!/usr/bin/awk -f), it is not necessary to preclude awk -f on the command line

/home/eberg/scripts/du.awk
4K      Makefile
28K     CVS
28K     robots
44K     html
48K     css
48K     javascript
48K     xsl
56K     favicon
100K    tools
1.5M    notes

basename

strips directory paths from filenames

Remove leading directory and path information from NAME

basename /usr/bin/less
less

tmtowtdi: instead of forking basename use a bash feature instead. ${VAR##*/} will remove everything up to and including the last slash "/" from VAR. (see also: dirname)

filename=/usr/share/man/man8/ping.8.gz
echo ${filename##*/}
ping.8.gz

When both a directory prefix and a suffix need to be stripped, basename can help. This removes the leading directory and path information as well as the .png suffix

basename /home/eb/erikberg.com/notes/i/w3m_url.png .png
w3m_url

bc

an arbitrary precision calculator language

Start interactive bc session with a scale of 20

bc -l

Print 232 power

echo 2^32 | bc
4294967296

Print number of inches in a meter

echo 100/2.54 | bc -l
39.37007874015748031496

Convert 75 degrees Fahrenheit to Celsius (see: functions)

echo '(75-32)*(5/9)' | bc -l
23.88888888888888888865

bzip2

file compressor

Compress FILE

bzip2 FILE

Uncompress FILE.bz2

bunzip2 FILE.bz2

Uncompress bzipped tar archive FILE.tar.bz2

tar zjvf FILE.tar.bz2

Create bzipped tar archive FILE.tar.bz2 of the directory DIR

tar cjvf FILE.tar.bz2 DIR

cal

displays a calendar

Print previous, current, and next months

cal -3
    January 2006         February 2006           March 2006
Su Mo Tu We Th Fr Sa  Su Mo Tu We Th Fr Sa  Su Mo Tu We Th Fr Sa
 1  2  3  4  5  6  7            1  2  3  4            1  2  3  4
 8  9 10 11 12 13 14   5  6  7  8  9 10 11   5  6  7  8  9 10 11
15 16 17 18 19 20 21  12 13 14 15 16 17 18  12 13 14 15 16 17 18
22 23 24 25 26 27 28  19 20 21 22 23 24 25  19 20 21 22 23 24 25
29 30 31              26 27 28              26 27 28 29 30 31

Display current month with Monday as the first day of the week

cal -m
   February 2006
Mo Tu We Th Fr Sa Su
       1  2  3  4  5
 6  7  8  9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28

cat

concatenate files and print on the standard output

Concatenate FILE1 and FILE2 into FILE3

cat FILE1 FILE2 > FILE3

Append FILE2 to the end of FILE1

cat FILE2 >> FILE1

Print FILE showing tab stops, ends of lines, and non-printing characters

cat -A FILE

Number each line in FILE (see also: nl)

cat -n FILE

The hacker's way to program "Hello, world"

cat <<eof
Hello, world
eof
Hello, world

chkconfig

updates and queries runlevel information for system services

Print list of services from /etc/rc[0-6].d and their startup configurations

chkconfig --list

Show the startup configuration for the sendmail service

chkconfig --list sendmail
sendmail        0:off   1:off   2:off   3:on   4:off   5:off   6:off

Enable SERVICE to start when system enters runlevels three and five

chkconfig --level 35 SERVICE on

chroot

change root directory

Switch the root directory to /mnt

chroot /mnt

comm

compare two sorted files line by line

Compare two sorted files. Given:

FILE1	FILE2
-----   -----
a       d
b       e
c       f
x       x
y       y
z       z

Print lines that appear either in FILE1 or FILE2, but not both

comm -3 FILE1 FILE2
a
b
c
        d
        e
        f

Print lines unique to FILE1 in first column, common to both FILES in second column

comm -2 FILE1 FILE2
a
b
c
        x
	y
	z

Print lines unique to FILE2 in first column, common to both FILES in second column

comm -1 FILE1 FILE2
d
e
f
        x
        y
        z

Print lines unique to FILE1 in first column, lines unique to FILE2 in second column, and lines common to both FILES in third column

comm FILE1 FILE2
a
b
c
        d
        e
        f
                x
                y
                z

convert

convert between image formats and much more

Copy and convert FILE.jpg to FILE.png

convert FILE.jpg FILE.png

See http://www.imagemagick.org/ script/convert.php

cpio

copy files to and from archives

Copy all files in current directory and subdirectories to NEW_DIRECTORY

find . -depth -print0 | cpio -0pvd NEW_DIRECTORY

crontab

maintain and install crontab files

The original crontab author played a joke on every QWERTY keyboard user by making the options to modify a crontab file ( -e) and delete a crontab file ( -r) susceptible to disastrous typing mistakes. Because the chance to delete a crontab file accidentally instead of editing it is a simple mistype, I recommend always editing a local file and then using the crontab command to install it

crontab -l > crontab.0429

After making changes to the local file, install it

crontab crontab.0429

csplit

split a file based on a pattern

Create three files named xx00, xx01, and xx02 by splitting FILE on blank lines. Given FILE contains:

this will go to xx00
this will go to xx00
this will go to xx00

this will go to xx01
this will go to xx01

this will go to xx02
this will go to xx02
this will go to xx02
this will go to xx02
csplit FILE /^$/ {*}
63
43
85

cut

remove sections from each line of files

On line delimited by commas, print the 2nd and 5th fields

echo "1,2,3,4,5" | cut -d, -f2,5
2,5

Print the login name and user's full name from /etc/passwd, separate output with a tab

cut -d: -f1,5 --output-delimiter="    " /etc/passwd
eberg   Erik Berg

When dealing with a fixed width file with varying field lengths, cut works well on byte ranges. The following will print bytes 1-3 (the "RK" column) and bytes 23 to the end of the line (the "Pts" column). Given FILE contains:

RK  Name              Pts
 1  Duke              1617
 2  Michigan State    1558
 3  Kansas State      1422
 4  Ohio State        1379
 5  Pittsburgh        1325
 6  Villanova         1197
 7  Kansas            1078
 8  North Carolina    1021
 9  Florida            989
10  Syracuse           967
cut -b1-3,23- FILE
RK Pts
 1 1617
 2 1558
 3 1422
 4 1379
 5 1325
 6 1197
 7 1078
 8 1021
 9  989
10  967

cvs

Concurrent Versions System

Show differences between working files and files in CVS repository

cvs diff -u

Show differences between revision 1.2 and 1.1 in FILE

cvs diff -u -r1.2 -r1.1 FILE

Print status information about files in working directory and CVS repository

cvs status

date

display or set the system date and time

Format date string in YYYYMMDD format to MM/DD/YY format

date -d 20070727 +"%m/%d/%y"
07/27/07

Same, but without padding

date -d 20070727 +"%-m/%-d/%y"
7/27/07

Print date three months from now

date -d '3 months' +"%b %-d, %Y"
Oct 27, 2007

Print date three months ago

date -d '3 months ago' +"%b %-d, %Y"
Apr 27, 2007

Print number of seconds since January 1, 1970 (epoch)

date +"%s"
1185525771

Display seconds since the epoch in a more human friendly format

date -d 'Jan 1 1970 1185525771 seconds' +"%A %B %-d, %Y"
Friday July 27, 2007

dd

convert and copy a file

Create one megabyte file /tmp/foobar

dd if=/dev/zero of=/tmp/foobar bs=1024k cnt=1
1+0 records in
1+0 records out

df

report filesystem disk space usage

Print amount of disk space used and available on all filesystems

df -h
Filesystem            Size  Used Avail Use% Mounted on
/dev/hda3             9.7G  6.2G  3.1G  67% /
/dev/hda1              99M   15M   80M  16% /boot
none                  501M     0  501M   0% /dev/shm

Print number of inodes available, used, and percentage used

df -i
Filesystem            Inodes   IUsed   IFree IUse% Mounted on
/dev/hda3            1281696  296485  985211   24% /
/dev/hda1              26104      43   26061    1% /boot
none                  128172       1  128171    1% /dev/shm

diff

find differences between two files

Show differences between files foo and foo.orig in format suitable for patch command

diff -u foo.ORIG foo

Compare files in two directories

diff DIR1 DIR2

dircolors

color setup for ls and other GNU commands

Print all color codes and default color settings for ls

dircolors -p

dirname

strips non-directory suffix from filenames

Remove filename from NAME

dirname /usr/bin/less
/usr/bin

tmtowtdi: the same behavior can be duplicated by using a bash feature. ${VAR%/*} will start from the end of VAR and remove backwards up to and including the first slash "/" matched. (see also: basename)

filename=/usr/share/man/man8/ping.8.gz
echo ${filename%/*}
/usr/share/man/man8

dos2unix

convert DOS/MAC text files to UNIX

Convert FILE with carriage returns and newlines (\r\n) to newlines only (see also: unix2dos)

dos2unix FILE

du

estimate file space usage

Print amount of disk space used by files in the current directory and subdirectories

du -sh .
1.3M    .

Print amount of disk space used by files in the current directory only--don't include subdirectories

du -sSh .
248K    .

Print disk usage summary in kilobytes of all files and subdirectories in current directory, sorted by size. See awk example above for output in human readable format

du -sk * | sort -n
4       Makefile
28      CVS
28      robots
44      html
48      css
48      javascript
48      xsl
56      favicon
100     tools
1588    notes

dumpe2fs

Print ext2/ext3 filesystem information

Display detailed superblock information for device /dev/hda2. Useful, among other things, to show the block size of a filesystem

dumpe2fs -h /dev/hda2
dumpe2fs 1.32 (09-Nov-2002)
Filesystem volume name:   /d2
Last mounted on:          <not available>
Filesystem UUID:          c1a10944-ab5e-40c9-9c3c-d658f0331dab
Filesystem magic number:  0xEF53
Filesystem revision #:    1 (dynamic)
Filesystem features:      has_journal filetype needs_recovery sparse_super large_file
Default mount options:    (none)
Filesystem state:         clean
Errors behavior:          Continue
Filesystem OS type:       Linux
Inode count:              13533184
Block count:              27059484
Reserved block count:     1352974
Free blocks:              4749268
Free inodes:              13256343
First block:              0
Block size:               4096
Fragment size:            4096
Blocks per group:         32768
Fragments per group:      32768
Inodes per group:         16384
Inode blocks per group:   512
Filesystem created:       Wed Mar 24 10:51:56 2004
Last mount time:          Fri Feb 17 12:52:17 2006
Last write time:          Fri Feb 17 12:52:17 2006
Mount count:              17
Maximum mount count:      -1
Last checked:             Tue May  3 03:41:05 2005
Check interval:           0 (<none>)
Reserved blocks uid:      0 (user root)
Reserved blocks gid:      0 (group root)
First inode:              11
Inode size:               128
Journal UUID:             <none>
Journal inode:            8
Journal device:           0x0000
First orphan inode:       0

dumpkeys

show keyboard translation tables (must be used on console)

Show all compose key combinations. The letters can be typed by pressing compose key, then modifer, then second modifer. See also: /usr/share/X11/locale/en_US.UTF-8/Compose

dumpkeys --compose-only
compose '`' 'A' to 'À'
compose '`' 'a' to 'à'
compose '\'' 'A' to 'Á'
compose '\'' 'a' to 'á'
compose '^' 'A' to 'Â'
compose '^' 'a' to 'â'
compose '~' 'A' to 'Ã'
compose '~' 'a' to 'ã'
...

echo

display a line of text

Display all commands in /usr/sbin and /usr/bin

echo /usr/sbin/* /usr/bin/*

Beep

echo -e '\a'

Do not print trailing newline character

echo -n No newline here

env

run a program in a modified environment

Print the current shell environment variables

env

Run COMMAND with the LANG environment variable value set to C

env LANG=C COMMAND

expr

evaluate arguments as an expression

Add 22 + 29

expr 22 + 29

Return length of string (tmtowtdi: wc -c)

expr length sweaterdresses
14

Return first 7 characters of string, first character begins at 1

expr substr sweaterdresses 1 7
sweater

fc

fix command

Show last 10 commands

fc -l -10

Create alias "r" that repeats last command in history that starts with STRING (tmtowtdi: !STRING)

alias r='fc -s'
r STRING

file

determine file type

Print type (binary, image, script, etc.) of file for FILE

file FILE

find

search for files in a directory hierarchy

Print filename and size of files ending in .png that are more than 100k and less than 200k

find . -name "*.png" -size +100k -size -200k -printf "%-60p %6s\n"
./software/qlcrash/logo2.png                                 166613
./f1/montreal/raceday.png                                    118729
./stats.png                                                  104205
./new.png                                                    150399

Change files NOT owned by daemon to daemon

find . ! -user daemon -exec chown daemon {} \;

Change files that have permissions 777 (-rwxrwxrwx) to 644 (-rw-r--r--)

find . -perm 777 -exec chmod 644 {} \;

Print files in /var/log that were modified within the last 60 minutes

find /var/log -type f -mmin -60
/var/log/wtmp
/var/log/messages
/var/log/cron
/var/log/sa/sa15

Search for files by their inode number

find . -inum 1550908 -ls
1550908  112 -rw-r--r--   1 eberg    users      104205 Feb 18  2005 ./stats.png

Find regular files that begin with a "." followed by an alphanumeric character in current directory only--don't descend into subdirectories

find . -maxdepth 1 -name ".[[:alnum:]]*" -type f

fuser

identify processes using files or sockets

Print the PID of the program using TCP port 22

fuser 22/tcp
22/tcp:              13272

Print the PID of the program using UDP port 123

fuser 123/udp
123/udp:              4222

Print the PIDs of the programs accessing /bin/bash in ps style

fuser /bin/bash -v
                     USER        PID ACCESS COMMAND
/bin/bash            eberg     26579 ...e.  bash
                     eberg     26635 ...e.  bash
                     root      26804 ...e.  bash

Send SIGKILL signal to all processes that have /usr/share/YaST2 open

fuser -k /usr/share/YaST2

Send the preferred kill signal, SIGTERM, which gives processes a chance to cleanup properly

fuser -TERM -k /usr/bin/pine
/usr/bin/pine:       30127e

gdb

the GNU Debugger

Start an interactive debugger session with PROGRAM

gdb PROGRAM

Start an interactive debugger session with PROGRAM and a CORE file

gdb PROGRAM CORE

getconf

Query system configuration variables

Print the maximum number of characters a command line can contain

getconf ARG_MAX
131072

For more operating system limits see /usr/include/linux/limits.h

grep

print lines matching a pattern

Count number of lines matching PATTERN in FILE

grep -c PATTERN FILE

Count number of lines NOT matching PATTERN in FILE

grep -cv PATTERN FILE

Count total number of instances PATTERN occurs in FILE, useful when PATTERN is repeated multiple times on a single line

grep -o PATTERN FILE | wc -l

Print lines in FILE that don't contain any digits

grep -v [[:digit:]] FILE

Perform a case insensitive search for PATTERN in FILE

grep -i PATTERN FILE

Sometimes it's useful to see the previous and following lines after a match. This prints the two lines previous and the two lines following a match of the string OpenLDAP in file README

grep -C 2 OpenLDAP README

If you are attempting to compile in the LDAP functionality in pine, you
should get the OpenLDAP source, set up the libldap and liblber libraries,
and the include files. A usually easy way to do that is

--
using ldap-3.3.

Alternatively, OpenLDAP may already be included in your system libraries.
Solaris 8 is apparently set up this way. The script contrib/ldap-setup,
which is called by the build script, tries to detect this automatically.

Print line number and lines matching string sm_strlcpy in file dkim-filter.c

grep -n sm_strlcpy dkim-filter.c
1348:   sm_strlcpy(cc->cctx_host, host, sizeof cc->cctx_host);
1637:   sm_strlcpy(addr, from->hdr_val, sizeof addr);
2288:                   sm_strlcpy(hdr, "unknown", sizeof hdr);
2289:                   sm_strlcpy(val, "unknown", sizeof val);

Set two environment variables to add a little color to grep matches. GREP_COLOR will highlight matches in the specified color. Settings specified in GREP_OPTIONS will be used with each grep invocation. (see: dircolors for color codes)

export GREP_COLOR=01\;34
export GREP_OPTIONS=--color=auto
grep color tips.css
	color: #000000;
a:visited { color: #80080; }
	background-color:green;
	color:#ffffff;
	background-color: #080;
	color:#080;
	color:#080;
	background-color:white;
grep -C 1 OpenLDAP README
If you are attempting to compile in the LDAP functionality in pine, you
should get the OpenLDAP source, set up the libldap and liblber libraries,
and the include files. A usually easy way to do that is
--

Alternatively, OpenLDAP may already be included in your system libraries.
Solaris 8 is apparently set up this way. The script contrib/ldap-setup,

The -o option will print only the matched PATTERN on a line. Useful when matching on very long lines

grep -o PATTERN FILE

gzip

compress or expand files

compress FILE

gzip FILE

uncompress FILE_GZ

gzip -d FILE_GZ

Uncompress gzipped tar archive TAR_GZ

tar zxvf TAR_GZ

Create gzipped tar archive FILE_TAR_GZ of the directory DIR

tar czvf FILE_TAR_GZ DIR

hdparm

get/set hard disk parameters

Show hard drive settings for /dev/hda

hdparm /dev/hda
/dev/hda:
 multcount    = 16 (on)
 IO_support   =  0 (default 16-bit)
 unmaskirq    =  0 (off)
 using_dma    =  1 (on)
 keepsettings =  0 (off)
 readonly     =  0 (off)
 readahead    = 256 (on)
 geometry     = 58140/16/63, sectors = 30005821440, start = 0

Print performance statistics about /dev/hda hard drive

hdparm -tT /dev/hda
/dev/hda:
 Timing cached reads:   1232 MB in  2.00 seconds = 615.96 MB/sec
 Timing buffered disk reads:   74 MB in  3.04 seconds =  24.34 MB/sec

output the first part of files

Print the first 20 lines in FILE

head -20 FILE

Print lines 15-20 in FILE (tmtowtdi: sed -n 15,20p FILE)

head -20 FILE | tail -5

iconv

convert character encoding in file to another

Convert FILE from iso-8859-1 encoding to utf-8 encoding

iconv -f ISO-8859-1 -t UTF-8 FILE

Print list of coded character set names that can be used with -f and -t options

iconv -l

ident

identify RCS keyword strings in files

Show RCS keywords in file linuxtips.html

ident linuxtips.html
linuxtips.html:
     $Id: linuxtips.html,v 1.118 2013/01/15 20:22:52 eb Exp $

ipcs

provide information on ipc facilities

Show shared memory, semaphore, and message queue information

ipcs
------ Shared Memory Segments --------
key        shmid      owner      perms      bytes      nattch     status
0x00000000 1409024    root      600        33554432   9          dest
0x00000000 1245185    root      600        262144     1          dest

------ Semaphore Arrays --------
key        semid      owner      perms      nsems
0x00000000 458752     httpd     600        1
0x00000000 491521     httpd     600        1
0x00000000 524290     httpd     600        1

------ Message Queues --------
key        msqid      owner      perms      used-bytes   messages
0x00001f58 0          root       600        0            0

ipcrm

remove a message queue, semaphore set or shared memory id

Remove the shared memory segment SHMID from the system

ipcrm -m SHMID

join

join lines of two files on a common field

Given:

FILE1   FILE2
-----   -----
1 abc   1 def
2 ghi   2 jkl
3 mno   3 pqr
4 stu   4 vwx
5 yz

Join FILE1 and FILE2 on their common fields (i.e., 1-4)

join FILE1 FILE2
1 abc def
2 ghi jkl
3 mno pqr
4 stu vwx

Same, but also print lines from FILE1 that have no match

join -a1 FILE1 FILE2
1 abc def
2 ghi jkl
3 mno pqr
4 stu vwx
5 yz

Print lines from FILE1 that have no common field match in FILE2

join -v1 FILE1 FILE2
5 yz

kbdrate

reset the keyboard repeat rate and delay time

Show the current keyboard delay and repeat settings (For X Windows see: xset)

kbdrate
Typematic Rate set to 10.9 cps (delay = 250 ms)

Set the repeat rate for keyboard to 30.0 characters per second

kbdrate -r 30.0
Typematic Rate set to 30.0 cps (delay = 250 ms)

kill

send signal to a process

Suspend process PID

kill -STOP PID

Continue suspended process PID

kill -CONT PID

Print list of of valid signals

kill -l
 1) SIGHUP       2) SIGINT       3) SIGQUIT      4) SIGILL
 5) SIGTRAP      6) SIGABRT      7) SIGBUS       8) SIGFPE
 9) SIGKILL     10) SIGUSR1     11) SIGSEGV     12) SIGUSR2
 ...

The null signal, often used in scripts. Tests for valid PID

kill -0 PID

kpartx

create device maps from partition tables

Add map for LV /dev/system/bob. After this command is run, /dev/mapper/system-bob1 can be mounted.

kpartx -v -a /dev/system/bob
add map system-bob1 : 0 41929587 linear /dev/system/bob 63

Delete map for LV /dev/system/bob

kpartx -v -d /dev/system/bob
del devmap : system-bob1

last

show listing of last logged in users

Show last five logins on system

last -5
eberg       pts/5        :0.0             Sat Feb 11 14:56   still logged in
eberg       pts/4        :0.0             Sat Feb 11 14:48   still logged in
eberg       pts/3        :0.0             Sat Feb 11 14:32   still logged in
eberg       pts/3        :0.0             Sat Feb 11 14:27 - 14:27  (00:00)
eberg       pts/2        :0.0             Sat Feb 11 14:26   still logged in

wtmp begins Wed Feb  2 11:32:46 2005

Show last four reboots on system with kernel information

last -4 reboot
reboot   system boot  2.6.5-7.201-defa Mon Sep  5 10:49         (172+21:09)
reboot   system boot  2.6.5-7.193-defa Tue Aug  9 16:27         (26+06:46)
reboot   system boot  2.6.5-7.155.29-d Wed Jul 20 17:51         (46+05:22)
reboot   system boot  2.6.5-7.151-defa Sat Mar 26 02:06         (162+20:07)

wtmp begins Wed Feb  2 11:32:46 2005

ldconfig

configure dynamic linker run time bindings

After updating /etc/ld.so.conf this reads and updates /etc/ld.so.cache

ldconfig

Print list of libraries referenced in /etc/ld.so.cache

ldconfig -p
1062 libs found in cache `/etc/ld.so.cache'
libz.so.1 (libc6) => /usr/lib/libz.so.1
libz.so (libc6) => /usr/lib/libz.so
libx11globalcomm.so.1 (libc6) => /usr/lib/libx11globalcomm.so.1
libxslt.so.1 (libc6) => /usr/lib/libxslt.so.1
...

less

file pager

Page through FILE

less FILE

locate

print location of files on system

Print STRING in locate database

locate strcpy
/usr/lib/perl5/5.8.6/i386-linux-thread-multi/auto/POSIX/strcpy.al
/usr/share/man/man3p/strcpy.3p.gz
/usr/share/man/man3/strcpy.3.gz

logger

a shell command interface to syslog

Record MESSAGE to syslog

logger log some interesting info about a process or system status
tail -1 /var/log/messages
Mar  2 08:25:22 for eb: log some interesting info about a process or system status

look

display lines beginning with a given string

Show all words that match STRING at beginning of lines in /usr/share/dict/words

look usef
useful
usefullish
usefully
usefulness

ls

list directory contents

List files, sorted by size

ls -lS
total 24
-rw-rw-r--  1 eb eb 7584 Feb 11 02:10 trim.html
drwxrwxr-x  2 eb eb 4096 Feb 14 18:21 CVS
-rw-rw-r--  1 eb eb  247 May  2  2005 Makefile
-rw-rw-r--  1 eb eb    0 Feb 13 19:40 index.html

List files, reverse sorted by file timestamp

ls -ltr
total 24
-rw-rw-r--  1 eb eb  247 May  2  2005 Makefile
-rw-rw-r--  1 eb eb 7584 Feb 11 02:10 trim.html
-rw-rw-r--  1 eb eb    0 Feb 13 19:40 index.html
drwxrwxr-x  2 eb eb 4096 Feb 14 18:21 CVS

List inodes of files

ls -i
1569885 CVS  1570178 index.html    98129 Makefile   556185 trim.html

List file timestamps in very detailed ISO-8601 format

ls -l --time-style=full-iso
total 24
drwxrwxr-x  2 eb eb 4096 2006-02-14 18:21:54.000000000 -0800 CVS
-rw-rw-r--  1 eb eb    0 2006-02-13 19:40:44.000000000 -0800 index.html
-rw-rw-r--  1 eb eb  247 2005-05-02 15:09:31.000000000 -0700 Makefile
-rw-rw-r--  1 eb eb 7584 2006-02-11 02:10:58.000000000 -0800 trim.html

File timestamps can be printed in almost any format using date formatting. This prints the timestamp in MM/DD/YY format

ls -l --time-style="+%m/%d/%y"
total 24
drwxr-xr-x 2 eb eb 4096 02/14/06 CVS
-rw-r--r-- 1 eb eb    0 02/13/06 index.html
-rw-r--r-- 1 eb eb  247 05/02/05 Makefile
-rw-r--r-- 1 eb eb 7584 02/11/06 trim.html

List all files that begin with 2007, but don't end with .jpg via extended shell globbing (i.e., shopt -s extglob)

ls 2007*
2007.txt     20070703.jpg 20070722.jpg 20070914.jpg 20071119.jpg
20070408.gif 20070703.png 20070722.png 20070914.png 20071119.png
ls 2007!(*.jpg)
-bash: !: event not found
shopt -s extglob
ls 2007!(*.jpg)
2007.txt     20070408.gif 20070703.png 20070722.png 20070914.png 20071119.png

lsmod

program to show the status of modules in the Linux Kernel

List currently loaded kernel modules (tmtowtdi: cat /proc/modules)

lsmod
Module                  Size  Used by
i2c_dev                10049  0
i2c_core               21697  1 i2c_dev
sunrpc                146045  1
pcmcia                 38781  2
...

lsof

list open files

List all open files on system

lsof

List all IPv4 network files in use (-i6 shows IPv6)

lsof -i 4
COMMAND  PID  USER   FD   TYPE DEVICE SIZE NODE NAME
portmap 1969   rpc    3u  IPv4   5044       UDP *:sunrpc
portmap 1969   rpc    4u  IPv4   5048       TCP *:sunrpc (LISTEN)
nscd    2039  nscd    3u  IPv4   5409       TCP for.ebloft.sea:60484->fore.ebloft.sea:ldap (ESTABLISHED)
ntpd    2074   ntp    4u  IPv4   5350       UDP *:ntp
ntpd    2074   ntp    7u  IPv4   5378       UDP for.ebloft.sea:ntp
mysqld  2165 mysql    3u  IPv4   5399       TCP *:mysql (LISTEN)
bash    2837    eb    4u  IPv4  15702       TCP for.ebloft.sea:53903->fore.ebloft.sea:ldap (ESTABLISHED)
ssh     3368    eb    3u  IPv4  10624       TCP for.ebloft.sea:46982->fore.ebloft.sea:ssh (ESTABLISHED)

List all open files using port 22 (SSH)

lsof -i :22

Same as above, but using the service name instead of the port number

lsof -i :ssh

List all files in use for process that begins with STRING

lsof -c STRING

List all processes and files accessing directory /tmp

lsof +d /tmp
COMMAND     PID USER   FD   TYPE     DEVICE SIZE   NODE NAME
gnome-key  3592   eb    3u  unix 0xd8755200        9806 /tmp/keyring-pAmjqz/socket
firefox-b 19833   eb  169u  unix 0xca103e00      115000 /tmp/jpsock.150_06.19833
firefox-b 19833   eb  173u  unix 0xd1e60b00      115021 /tmp/jpsock.150_06.19833
mapping-d 22849   eb    3u  unix 0xce458800       56398 /tmp/mapping-eb
mapping-d 22849   eb    4u  unix 0xce458980       56402 /tmp/mapping-eb

List all processes and files accessing directory and subdirectories of /tmp

lsof +D /tmp
COMMAND     PID USER   FD   TYPE     DEVICE  SIZE   NODE NAME
java_vm   25068   eb  mem    REG        3,3 32768 261148 /tmp/hsperfdata_eb/25068
gconfd-2  26703   eb   10u  unix 0xcf358c80        76405 /tmp/orbit-eb/linc-684f-0-23d1a1aec6f86
gconfd-2  26703   eb   12u  unix 0xd3873380        76092 /tmp/orbit-eb/linc-684f-0-23d1a1aec6f86
gconfd-2  26703   eb   13wW  REG        3,3   602 182541 /tmp/gconfd-eb/lock/ior
gconfd-2  26703   eb   15u  unix 0xcf25ec80        83237 /tmp/orbit-eb/linc-684f-0-23d1a1aec6f86
gconfd-2  26703   eb   17u  unix 0xd3873680        85278 /tmp/orbit-eb/linc-684f-0-23d1a1aec6f86
gconfd-2  26703   eb   31u  unix 0xcb2afb00        91912 /tmp/orbit-eb/linc-684f-0-23d1a1aec6f86
gconfd-2  26703   eb   33u  unix 0xcb2af680       109082 /tmp/orbit-eb/linc-684f-0-23d1a1aec6f86
notificat 29242   eb   15u  unix 0xc4733c80        34965 /tmp/orbit-eb/linc-723a-0-2b8ed990d27da
notificat 29242   eb   16u  unix 0xc4733b00        34968 /tmp/orbit-eb/linc-723a-0-2b8ed990d27da
notificat 29242   eb   19u  unix 0xc291f500        76121 /tmp/orbit-eb/linc-723a-0-2b8ed990d27da
...

List all files in use by process PID

lsof -p PID

List all files in use by USER

lsof -u USER

Send kill signal to all (mail) processes on port 25

kill -HUP $(lsof -t -i :25)

lspci

list all PCI devices

List PCI devices on system

lspci
00:00.0 Host bridge: Intel Corporation 82845 845 (Brookdale) Chipset Host Bridge (rev 04)
00:01.0 PCI bridge: Intel Corporation 82845 845 (Brookdale) Chipset AGP Bridge (rev 04)
...

Show information about device using address 00:01 in extra verbose (-vv) format

lspci -vv -s 00:01
00:01.0 PCI bridge: Intel Corporation 82845 845 (Brookdale) Chipset AGP Bridge (rev 04)
        Control: I/O+ Mem+ BusMaster+ SpecCycle- MemWINV- VGASnoop- ParErr- Stepping- SERR+
        Status: Cap- 66MHz+ UDF- FastB2B+ ParErr- DEVSEL=fast >TAbort- <TAbort-
        Latency: 32
        Bus: primary=00, secondary=01, subordinate=01, sec-latency=32
        I/O behind bridge: 0000c000-0000cfff
        Memory behind bridge: fc000000-fdffffff
        Prefetchable memory behind bridge: f0000000-f3ffffff
        Secondary status: 66MHz+ FastB2B+ ParErr- DEVSEL=medium >TAbort- <TAbort-
        BridgeCtl: Parity- SERR- NoISA+ VGA+ MAbort- >Reset- FastB2B-

lsusb

list USB devices

List USB devices on system (tmtowtdi: cat /proc/bus/usb/devices)

lsusb
Bus 004 Device 001: ID 0000:0000
Bus 003 Device 001: ID 0000:0000
Bus 002 Device 001: ID 0000:0000
Bus 001 Device 001: ID 0000:0000

ltrace

library call tracer

Trace all library calls made by COMMAND

ltrace echo "Hello, world"
__libc_start_main(0x8048ea5, 2, 0xbffd9df4, 0x804a67c, 0x804a6cc <unfinished ...>
setlocale(6, "")                                 = "en_US.UTF-8"
bindtextdomain("coreutils", "/usr/share/locale") = "/usr/share/locale"
textdomain("coreutils")                          = "coreutils"
__cxa_atexit(0x8049309, 0, 0, 0x804c85c, 0xbffd9d68) = 0
getenv("POSIXLY_CORRECT")                        = NULL
getopt_long(2, 0xbffd9df4, "+", 0x804c700, NULL) = -1
fputs_unlocked(0xbffdb9f5, 0x2365e0, 0x804aad1, 0x804aac3, 0x804aabd) = 1
__overflow(0x2365e0, 10, 0x804aad1, 0x804aac3, 0x804aabdHello, world
) = 10
exit(0 <unfinished ...>
__fpending(0x2365e0, 0x72b878, 0xb7fdc6a0, 1, 1) = 0
+++ exited (status 0) +++

Print nice summary table of all library calls made by COMMAND

ltrace -c echo "Hello, world"
Hello, world
% time     seconds  usecs/call     calls      function
------ ----------- ----------- --------- --------------------
 47.55    0.000806         806         1 setlocale
 17.94    0.000304         304         1 __overflow
 11.03    0.000187         187         1 fputs_unlocked
  4.72    0.000080          80         1 getopt_long
  3.89    0.000066          66         1 __fpending
  3.83    0.000065          65         1 bindtextdomain
  3.72    0.000063          63         1 textdomain
  3.72    0.000063          63         1 __cxa_atexit
  3.60    0.000061          61         1 getenv
------ ----------- ----------- --------- --------------------
100.00    0.001695                     9 total

man

format and display the on-line manual pages

Display manual page for COMMAND

man COMMAND

Display man page for waitpid in section 3p

man 3p waitpid

Return list of man pages that contain STRING (tmtowtdi: apropos STRING)

man -k STRING

md5sum

compute and check MD5 message digest

Print MD5 checksum for FILE

md5sum FILE

Save output from md5sum to FILE. Use the --check option in conjunction with FILE to determine whether the MD5 checksums have changed.

md5sum linuxtips.html Makefile milters.html > FILE
md5sum --check FILE
linuxtips.html: OK
Makefile: OK
milters.html: OK

metamail

Consults mailcap file to determine how to display non-text (e.g., MIME) encoded files

Decode MIME message contained in FILE

metamail FILE

modinfo

program to show information about a Linux Kernel module

Print information about kernel MODULE

modinfo MODULE

Print information about all currently active kernel modules on system

awk '{ print $1 }' /proc/modules | while read module; do
modinfo $module
echo -e '\n======================\n'
done

mount

mount and unmount a filesystem

Mount /dev/dvd to /mnt

mount /dev/dvd /mnt

Make /dev available to /mnt/dev. Sometimes useful when working in chroot environments when access to files like /dev/urandom is needed

mount --bind /dev /mnt/dev

namei

follow a pathname until a terminal point is found

Follow symbolic link until an end point is found

namei /bin/sh
f: /bin/sh
 d /
 d bin
 l sh -> bash
   - bash

nice

run a program with modified scheduling priority

Run COMMAND at priority 19 (the lowest)

nice -n 19 COMMAND

nl

number lines of files

Number each non-empty line in FILE (tmtowtdi: cat -b)

nl FILE

Number each line in FILE (tmtowtdi: cat -n)

nl -ba FILE

nroff

shell script frontend to groff

print and format manual page for nice

zcat nice.1.gz | nroff -man | less

ntpq

standard NTP query program

Print NTP peer status

ntpq -p
     remote           refid      st t when poll reach   delay   offset  jitter
==============================================================================
*fore.ebloft.sea xx.71.9.63       4 u   68  256  377    0.827  -12.692   7.887

ntpstat

show network time synchronization status

Display NTP synchronization status

ntpstat
synchronised to NTP server (192.168.1.151) at stratum 5
   time correct to within 114 ms
   polling server every 256 s

od

dump files in octal and other formats

Print FILE in octal

od FILE

paste

merge lines of files

Merge lines of FILE1 FILE2

paste FILE1 FILE2

Merge lines in FILE separated by tabs. Given FILE contains:

Long Beach
Huntington Beach
Newport Beach
Laguna Beach
paste - - - < FILE
Long Beach  Huntington Beach    Newport Beach
Laguna Beach

patch

apply a diff file to an original file

Create a diff file between ORIGINAL and NEWFILE, then apply the changes in NEWFILE to ORIGINAL

diff -u ORIGINAL NEWFILE > CHANGES.patch
patch -p0 < CHANGES.patch

perldoc

Look up Perl documentation in Pod format

Display documentation for module IO::File

perldoc IO::File

pgrep

look up or signal processes based on name and other attributes

Print Process IDs matching PATTERN

pgrep PATTERN

Pass output from pgrep to ps for a full listing (see: functions to make this easily accessible)

ps uwp $(pgrep httpd)
USER       PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
root      2028  0.0  1.9  21896  9872 ?        Ss   06:45   0:00 /usr/sbin/httpd
apache    2032  0.0  0.5  18244  2728 ?        S    06:45   0:00 /usr/sbin/fcgi-
apache    2055  0.0  1.0  21896  5380 ?        S    06:45   0:00 /usr/sbin/httpd
apache    2056  0.0  1.0  21896  5380 ?        S    06:45   0:00 /usr/sbin/httpd

pinfo

curses based lynx-style info browser

Display Info document for COMMAND, most GNU commands are distributed with Info pages (see: info). Screenshot

pinfo COMMAND

printf

format and print data

Print thousands' grouping character (dependent on locale)

printf "%'d\n" $(echo 2^32 | bc)
4,294,967,296

Left pad number with zeros (tmtowtdi: seq -f "%05g" 1 5)

for ((i=1;i<=5;i++))
do
 printf "%05d\n" $i
done
00001
00002
00003
00004
00005

Print 1048576 in hexadecimal

printf "%x\n" 1048576
100000

Same, but precede output with '0x'

printf "%#x\n" 1048576
0x100000

Print 1048576 in octal

printf "%o\n" 1048576
4000000

Same, but precede output with '0'

printf "%#o\n" 1048576
04000000

Print hex value 0xff in decimal

printf "%d\n" 0xff
255

Print octal value 0377 in decimal

printf "%d\n" 0377
255

Print ascii characters for hex values 0x68, 0x74, 0x74, 0x70

printf "\x68\x74\x74\x70"
http

Print ascii character for decimal value 84

printf $(printf "\%o" 84)
T

ps

report a snapshot of the current processes

Report all running processes on the system UNIX System V style in long format

ps -efl
F S UID        PID  PPID  C PRI  NI ADDR    SZ WCHAN  STIME TTY          TIME CMD
4 S root         1     0  0  75   0    -   377 -      Feb09 ?        00:00:36 init
5 S root         2     0  0 -40   0    -     0 -      Feb09 ?        00:00:00 [migration/0]

Report all running processes on the system BSD-style in "unlimited width" output

ps auxww
USER       PID %CPU %MEM   VSZ  RSS TTY      STAT START   TIME COMMAND
root         1  0.0  0.0  1508  512 ?        S    Feb09   0:36 init
root         2  0.0  0.0     0    0 ?        SW   Feb09   0:00 [migration/0]

Show all processes owned by USER

ps -fu ldap
UID        PID  PPID  C STIME TTY          TIME CMD
ldap      4506     1  0 Feb17 ?        00:02:04 /usr/sbin/slapd -u ldap -h ldap:/// ldaps:///

pstree

display a tree of processes

Show process tree with command line arguments. Screenshot

pstree -a

pwd

print working directory

An often overlooked option for the bash pwd builtin is -P which prints the absolute working directory. Compare:

pwd
/usr/src/linux
pwd -P
/usr/src/linux-2.4.21-20.EL

renice

alter priority of running processes

Lower scheduling priority of all processes owned by USER

renice 10 -u USER

As root, increase scheduling priority of running process PID

renice -10 -p PID

rev

reverse lines of a file

Reverse the characters on each line in FILE

rev FILE

rpm

Red Hat Package Manager

Print what RPM package contains FILE

rpm -qf FILE

It is often useful to use the -qf options with which when trying to determine the package owner for a command

rpm -qf $(which df)
coreutils-5.2.1-48.1

Print information about RPM_PACKAGE

rpm -qi RPM_PACKAGE

List files in RPM_PACKAGE

rpm -ql RPM_PACKAGE

Install or Upgrade RPM_PACKAGE, ignore any dependencies

rpm -Uvh --nodeps RPM_PACKAGE

rsync

faster, flexible replacement for rcp

Synchronize files in LOCALDIR to HOST in REMOTEDIR

rsync -e ssh --delete -avzP LOCALDIR HOST:REMOTEDIR

scl

manage software collections (RHEL)

List installed software collections

scl -l
nodejs010
v8314

Configure nodejs010 environment to use in current bash shell

scl enable nodejs010 bash

script

make typescript of terminal session

Start a new shell where all input and output is saved to FILE

script FILE

scriptreplay

playback a typescript session at the same speed it was recorded

Save time information to TIMING and typescript to TYPESCRIPT.

script -t 2>TIMING TYPESCRIPT

After ending the typescript session, replay it using the following command.

scriptreplay TIMING TYPESCRIPT

sed

text editor

Edit FILE1, FILE2, FILE3 in place, replacing all occurrences of "iso-8859-1" with "UTF-8"

sed -i 's/iso-8859-1/UTF-8/g' FILE1 FILE2 FILE3

Print all lines that match PATTERN in FILE (tmtowtdi: grep)

sed -n '/PATTERN/p' FILE

Print all lines that don't match PATTERN in FILE (tmtowtdi: grep -v)

sed -n '/PATTERN/!p' FILE

Insert text "<!DOCTYPE html" at the first line in all files ending in .html

sed -i '1i <!DOCTYPE html' *.html

Append text "this is the last line" to the end of FILE

sed -i '$a this is the last line' FILE

Delete lines 1-5 from FILES

sed 1,5d FILES

Print all lines in syslog that were recorded from 10PM-2AM between February 15 and February 16

sed -n '/^Feb 15 22:/,/^Feb 16 02:/p' /var/log/messages | sed '$d'

Print lines line matching PATTERN and next five lines (tmtowtdi: grep -A 5 PATTERN FILE)

sed -n '/PATTERN/,+5p' FILE

Excellent resource for one-line sed scripts can be found at http:// pement.org/sed/ sed1line.txt

seq

print a sequence of numbers

Print numbers one through five

seq 1 5
1
2
3
4
5

tmtowtdi: use a bash feature for simple sequences, {x..y} where x and y are integers will expand to print each number between x and y (see also: looping)

for i in {1..5}
do
 echo $i
done
1
2
3
4
5

Print numbers 25-50, iterate by 5, separate output with space

seq -s' ' 25 5 50
25 30 35 40 45 50

Print numbers 5 million to 10 million, increment by 1 million, using thousands grouping separator

seq -f "%'.f" 5000000 1000000 10000000
5,000,000
6,000,000
7,000,000
8,000,000
9,000,000
10,000,000

Print numbers from 0-255 in hexadecimal

printf "%x\n" $(seq 0 255)
0
1
...
fe
ff

sfdisk

view (and set) partition information

List partitions on /dev/hda

sfdisk -l /dev/hda

Disk /dev/hda: 58140 cylinders, 16 heads, 63 sectors/track
Warning: The partition table looks like it was made
  for C/H/S=*/255/63 (instead of 58140/16/63).
For this listing I'll assume that geometry.
Units = cylinders of 8225280 bytes, blocks of 1024 bytes, counting from 0

   Device Boot Start     End   #cyls    #blocks   Id  System
/dev/hda1   *      0+     12      13-    104391   83  Linux
/dev/hda2         13    3647    3635   29198137+  8e  Linux LVM
/dev/hda3          0       -       0          0    0  Empty
/dev/hda4          0       -       0          0    0  Empty

Print total size of /dev/hda in 1K blocks. tmtowtdi: cat /proc/ide/hda/capacity (512-byte blocks)

sfdisk -s /dev/hda
29302560

sha256sum

print and check SHA256 message digest

Print SHA256 checksum for FILE

sha256sum FILE
b10bdb5dc695a9b182f7372a79e85c72a4455a538907bf59e5962bed6401cfa4 FILE

Verify that the checksums listed in file match what is on disk. Given a file named CHECKSUM contains:

55550e8b46b3a8080c4722ae87c5cafc60bb51778fe534760bee5775221393af  config.json
4ce3a8c8c11cd39de9967e4e81b13a76797d230a3ed0b2e78288106db085ff81  package.json
b3081f23981cb8eeed773303edb56e08009ceb5ab9029bd82736130bb0edaae4  xmlstats-proxy.js
sha256sum -c CHECKSUM
config.json: OK
package.json: OK
xmlstats-proxy.js: OK

shuf

randomly sort input lines

Randomly sort lines in FILE

shuf FILE

Randomly sort lines in FILE, limit output to 10 lines

shuf -n 10 FILE

smartctl

print and set configuration for SMART disks

Print information about device

smartctl -i /dev/sda
smartctl version 5.37 [i686-suse-linux-gnu] Copyright (C) 2002-6 Bruce Allen
Home page is http://smartmontools.sourceforge.net/

=== START OF INFORMATION SECTION ===
Model Family:     Hitachi Travelstar 7K100
Device Model:     HTS721010G9SA00
Serial Number:    MPDZN7Y0J8R6SL
Firmware Version: MCZIC15V
User Capacity:    100,030,242,816 bytes
Device is:        In smartctl database [for details use: -P show]
ATA Version is:   7
ATA Standard is:  ATA/ATAPI-7 T13 1532D revision 1
Local Time is:    Tue Oct 16 17:09:44 2007 PDT
SMART support is: Available - device has SMART capability.
SMART support is: Enabled

sort

sort lines of text files

Sort, merge, and remove duplicate entries from FILE1 and FILE2

sort -mu FILE1 FILE2

Sort on the first character in the fourth field using the underscore as the field separator

ls -1 *xml
20050425_ARI_at_LAD.xml
20050425_ATL_at_NYM.xml
20050425_BAL_at_BOS.xml
20050425_CHW_at_OAK.xml
20050425_CIN_at_CHC.xml
20050425_HOU_at_PIT.xml
20050425_MIN_at_DET.xml
20050425_PHI_at_WAS.xml
20050425_SD_at_SF.xml
ls *xml | sort -t _ -k4.1
20050425_BAL_at_BOS.xml
20050425_CIN_at_CHC.xml
20050425_MIN_at_DET.xml
20050425_ARI_at_LAD.xml
20050425_ATL_at_NYM.xml
20050425_CHW_at_OAK.xml
20050425_HOU_at_PIT.xml
20050425_SD_at_SF.xml
20050425_PHI_at_WAS.xml

split

split a file into pieces

Break up LARGEFILE into 10MB files named xaa, xab, xac, ... (use cat to reconstruct LARGEFILE)

split --bytes=10m LARGEFILE

Split FILE into 100 line files named xaa, xab, xac, ...

split -l 100 FILE

stat

Report detailed file or filesystem information

Display access, size, inode, permission, etc. information for file linuxtips.html

stat linuxtips.html
  File: `linuxtips.html'
  Size: 73850           Blocks: 160        IO Block: 4096   regular file
Device: fd00h/64768d    Inode: 6870173     Links: 1
Access: (0664/-rw-rw-r--)  Uid: (  500/      eb)   Gid: (  100/      eb)
Access: 2006-03-03 10:09:00.000000000 -0800
Modify: 2006-03-03 09:34:05.000000000 -0800
Change: 2006-03-03 09:34:05.000000000 -0800

Display information about FILESYSTEM

stat -f /
  File: "/"
    ID: 0        Namelen: 255     Type: ext2/ext3
Blocks: Total: 6808543    Free: 1013789    Available: 662353     Size: 4096
Inodes: Total: 7031360    Free: 6755474

strace

trace system calls and signals

Trace system calls for already running process PID

strace -f -s 1024 -p PID

Trace system calls for command ls, send strace output to /tmp/lsout

strace -f -s 1024 -o /tmp/lsout ls

strings

print the strings of printable characters in files

Extract printable characters from binary FILE

strings FILE

sysctl

get and set system parameters

Print all keys and values

sysctl -a

Print keys and values for file system parameters only

sysctl fs

Print value for SHMMAX (tmtowtdi: cat /proc/sys/kernel/shmmax)

sysctl kernel.shmmax
kernel.shmmax = 4294967295

Disable IPv6 forwarding (tmtowtdi: echo 0 > /proc/sys/net/ipv6/conf/all/forwarding)

sysctl -w net.ipv6.conf.all.forwarding=0
net.ipv6.conf.all.forwarding = 0

tac

concatenate and print files in reverse

Print FILE in reverse

tac FILE

An early MSFT encryption algorithm? :-)

tac FILE | rev

tail

output the last part of files

Print the last 30 lines of /var/log/messages and all new lines as the file grows

tail -30f /var/log/messages

Print the first 20 of the last 50 lines in FILE

tail -50 FILE | head -20

Continue to print all new lines of access_log even when file is archived and recreated by another process. The default behavior of tail is to follow a file descriptor, not a filename

tail --follow=name /var/log/httpd/access_log

tar

create and extract archives

Create new archive named example.tar consisting of all files in DIRECTORY

tar cvf example.tar DIRECTORY

Create new gzip-compressed archive named example.tar.gz consisting of all files in DIRECTORY

tar czvf example.tar.gz DIRECTORY

List contents of example.tar archive

tar tvf example.tar

List contents of gzip-compressed example.tar.gz archive

tar tzvf example.tar.gz

Extract contents of example.tar archive

tar xvf example.tar

tee

read from standard input and write to standard output and files

Write standard output from COMMAND to standard out and FILE

COMMAND | tee FILE

time

time a simple command or give resource usage

Display system, user, and elapsed real time from executing COMMAND

time uptime
 07:38:26 up 53 min,  3 users,  load average: 0.18, 0.15, 0.15

real    0m0.008s
user    0m0.000s
sys     0m0.004s

tnef

decode MSFT Transport Neutral Encapsulation Format (TNEF)

Display attachment names contained in winmail.dat file

tnef -t winmail.dat

Extract attachments contained in winmail.dat file

tnef winmail.dat

touch

change file timestamps

Create empty NEW_FILE

touch NEW_FILE

Set existing FILE's access and modification times to now

touch FILE

Create or set existing FILE's access and modification times to May 1 2:20 p.m. and 15 seconds

touch -d 'May 1 14:20:15' FILE

tput

print/query terminal settings

Print number of columns for the current terminal

tput cols

man terminfo for list of tput capability names

tr

translate or delete characters

Delete all lowercase vowels from standard input

tr -d aeiou <<eof
This command will remove all of the vowels from this
sentence--just like people who send text messages!
eof
Ths cmmnd wll rmv ll f th vwls frm
ths sntnc--jst lk ppl wh snd txt mssgs!

Convert all newlines to commas in FILE

tr '\n' ',' <FILE

ulimit

get and set user limits

Display all settings for current user

ulimit -a
core file size          (blocks, -c) 0
data seg size           (kbytes, -d) unlimited
file size               (blocks, -f) unlimited
pending signals                 (-i) 8186
max locked memory       (kbytes, -l) 32
max memory size         (kbytes, -m) unlimited
open files                      (-n) 1024
pipe size            (512 bytes, -p) 8
POSIX message queues     (bytes, -q) 819200
stack size              (kbytes, -s) 10240
cpu time               (seconds, -t) unlimited
max user processes              (-u) 8186
virtual memory          (kbytes, -v) unlimited
file locks                      (-x) unlimited

uniq

repeat or omit duplicate lines

Check for duplicate lines from the tenth field onward in FILE. Fields are separated by a space

uniq -f 10 FILE

Count frequency of words in FILE. Note this uses a simple pattern match for finding individual words, it will produce incorrect results for words containing punctuation such as hyphens and apostrophes. Given FILE contains:

Around the world, around the world
Around the world, around the world
Around the world, around the world
Around the world, around the world

Around the world, around the world
Around the world, around the world
Around the world, around the world
Around the world, around the world

Around the world, around the world
Around the world, around the world
Around the world, around the world
Around the world, around the world
tr -cs '[:alpha:]' '\n' < FILE | tr A-Z a-z | sort | uniq -c | sort -k2 -n
  24 around
  24 the
  24 world

unix2dos

convert UNIX text files to DOS/MAC

Convert FILE with newlines only to DOS format with carriage returns and newlines (see also: dos2unix)

unix2dos FILE

update- alternatives

configure symbolic links for default commands

Display system settings for java

update-alternatives --display java
java - status is auto.
 link currently points to /usr/lib/jvm/java-1.4.2-gcj-1.4.2.0/jre/bin/java
/usr/lib/jvm/java-1.4.2-gcj-1.4.2.0/jre/bin/java - priority 9
Current `best' version is /usr/lib/jvm/java-1.4.2-gcj-1.4.2.0/jre/bin/java.

Create or update link /usr/bin/java to point to a java binary installed under /opt/java/jsdk5.0/jdk/bin/java and a slave link for the manual page.

update-alternatives --install /usr/bin/java java /opt/java/jsdk5.0/jdk/bin/java 100 \
--slave /usr/share/man/man1/java.1 java.1 /opt/java/jsdk5.0/jdk/man/man1/java.1

Select which java version system link (/usr/bin/java) should use.

update-alternatives --config java

There are 2 programs which provide `java'.

  Selection    Command
-----------------------------------------------
      1        /usr/lib/jvm/java-1.4.2-gcj-1.4.2.0/jre/bin/java
*+    2        /opt/java/jsdk5.0/jdk/bin/java

Enter to keep the default[*], or type selection number: 1
Using `/usr/lib/jvm/java-1.4.2-gcj-1.4.2.0/jre/bin/java' to provide `java'.

vi(m)

text editor

Split current vi session window vertically with FILE. Screenshot

:vspl tips.css

Split current vi session window horizontally. Screenshot

:spl

Start vi in diff mode. Screenshot

vimdiff FILE1 FILE2

Format paragraph into 79 character lines

gqap

Print the value of char under cursor in dec, hex, and oct

ga

Rot13 encode/decode current line

g?g?

When replacing accent characters or control characters, use the decimal, octal, or hex value from the "ga" command combined with CTRL+v to locate the character. These three examples replace any ö chars with unaccented 'o' chars on the current line using the decimal value, the octal value (note the 'o' modifier), and the hex value (note the 'u' modifier).

:s/CTRL+v246/o/g
:s/CTRL+vo366/o/g
:s/CTRL+vu00f6/o/g

Display a list of all accented, control, and special characters

:digraphs

Rot13 encode/decode current line

g??

w3m

a text based Web browser and pager

View URL using this text-based Web browser (tip: install w3m image helper package). Screenshot

w3m URL

Page through FILE. An alternative more or less

w3m FILE

watch

Display the output of a command repeatedly

Monitor the output of df -h every 60 seconds

watch -n 60 df -h

wc

print the number of newlines, words, and bytes in files

Count the number of words in FILE

wc -w FILE

Count the number of bytes in line

echo -n "How many bytes?" | wc -c
15

which

shows the full path of (shell) commands

Show path to COMMAND as searched for in PATH environment variable

which w3m
/usr/bin/w3m

who

shows who is logged on

Show the current system runlevel (see also: runlevel)

who -r
         run-level 3  Jan 15 20:45                   last=S

xargs

build and execute command lines from standard input

Find regular files in current directory and subdirectories, pass the filenames to xargs to search for PATTERN

find . -type f -print0 | xargs -0 grep PATTERN

Print list of all user accounts on system, sorted alphabetically

awk -F: '{ print $1 }' /etc/passwd | sort | xargs echo
adm apache ber bin bub daemon . . . sklarm sshd sync uucp vcsa xfs

yes

output a string repeatedly until killed

Print STRING to standard output until killed or interrupted

yes STRING

Respond 'y' to all interactive questions from COMMAND

yes | COMMAND

yum

Yellowdog Updater Modified

Check for package updates on system

yum check-update

Update all packages on system

yum update

Search for packages matching NAME

yum search NAME

Clear yum cache

yum clean all

zdump

print time zone information

Print the start and end times of Daylight Saving Time for the US/Pacific time zone

zdump -v US/Pacific | grep 2008
US/Pacific  Sun Mar  9 09:59:59 2008 UTC = Sun Mar  9 01:59:59 2008 PST isdst=0
US/Pacific  Sun Mar  9 10:00:00 2008 UTC = Sun Mar  9 03:00:00 2008 PDT isdst=1
US/Pacific  Sun Nov  2 08:59:59 2008 UTC = Sun Nov  2 01:59:59 2008 PDT isdst=1
US/Pacific  Sun Nov  2 09:00:00 2008 UTC = Sun Nov  2 01:00:00 2008 PST isdst=0

Print current time for time zone

zdump US/Samoa
US/Samoa  Fri Dec  4 15:28:41 2009 SST

zip

package and compress (archive) files

List files contained in ZIPFILE archive

unzip -l ZIPFILE

Networking

curl

Transfer data from or to a server

Download webpage

curl https://erikberg.com

<!DOCTYPE html>
<html>
 <head>
 <meta charset="UTF-8">
 <title>Erik Berg</title>
 <meta name="viewport" content="width=device-width, initial-scale=1">
 <meta name="date" content="2004-09-28">
 <meta name="revised" content="2015-10-13T16:05:10-07:00">
...
</html>

Download webpage and save it to file named stuff.html

curl -o stuff.html https://erikberg.com/api
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100 23615    0 23615    0     0   173k      0 --:--:-- --:--:-- --:--:--  173k

Same, but request that the response uses gzip compression

curl --compressed -o stuff.html https://erikberg.com/api
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100  7971    0  7971    0     0  82149      0 --:--:-- --:--:-- --:--:-- 83031

Same, but suppress progress status

curl -s --compressed -o stuff.html https://erikberg.com/api

dig

DNS lookup utility

Print DNS TXT record for HOST

dig +short erikberg.com txt
"v=spf1 a mx -all"

host

DNS lookup utility

Show DNS A and MX records for example.com

host example.com
example.com has address 192.0.34.166

Show DNS SOA records for example.com

host -C example.com
Nameserver a.iana-servers.net:
	example.com has SOA record dns1.icann.org. hostmaster.icann.org. 200511
1500 7200 3600 1209600 86400
Nameserver b.iana-servers.net:
	example.com has SOA record dns1.icann.org. hostmaster.icann.org. 200511
1500 7200 3600 1209600 86400

ip

show / manipulate routing, devices, policy routing and tunnels

Show statistics about network device wlan0

ip -s link ls wlan0
3: wlan0: <BROADCAST,MULTICAST,UP> mtu 1500 qdisc pfifo_fast qlen 1000
    link/ether 00:90:4b:28:41:6b brd ff:ff:ff:ff:ff:ff
    RX: bytes  packets  errors  dropped overrun mcast
    12242310   38660    0       0       0       0
    TX: bytes  packets  errors  dropped carrier collsns
    46230383   58420    0       0       0       0

Show routing table for device eth1

ip route ls dev eth1
192.168.1.0/24  proto kernel  scope link  src 192.168.1.151
169.254.0.0/16  scope link
default via 192.168.1.1

Show routing policy rules

ip rule ls
0:      from all lookup local
32766:  from all lookup main
32767:  from all lookup default

Add route to 192.168.2.0/24

ip route add to 192.168.2.0/24 dev eth0

Delete route to 192.168.2.0/24

ip route del to 192.168.2.0/24 dev eth0

iptraf

interactive Colorful IP LAN Monitor

Start interactive network device monitoring tool. Screenshots: 1, 2, 3, 4

iptraf

mrtg

monitors traffic load on network links

Set the LANG environment variable to C and execute mrtg using the configuration set in /etc/mrtg/mrtg.cfg. This is a typical crontab entry for mrtg

env LANG=C /usr/bin/mrtg /etc/mrtg/mrtg.cfg --logging /var/log/mrtg.log

See http://oss.oetiker.ch/mrtg/

mtr

a network diagnostic tool

Display statistics for all routers packet travels through to reach HOST

mtr --interval 5 HOST

nc (netcat)

arbitrary TCP and UDP connections and listens

Issue HTTP GET request once a second to HOST on port 80 ten times (use ctrl+v + ctrl+m to insert CRLF at end of each line in HTTP request)

for ((i=1; i<=10; i++)) 
do
netcat HOST 80 <<eof
GET / HTTP/1.1
Host: Localhost

eof
sleep 1
done
HTTP/1.1 200 OK
Date: Sun, 12 Feb 2006 09:47:08 GMT
Server: Apache
Last-Modified: Mon, 30 Jan 2006 10:06:49 GMT
ETag: "78d2b-6d4-9ae90040"
Accept-Ranges: bytes
Content-Length: 1748
Connection: close
Content-Type: text/html; charset=UTF-8

Start netcat to listen on port 23456

netcat -l -p 23456

Connect and send FILE to HOST on port 23456

netcat HOST 23456 < FILE

netstat

print network connections, routing tables, interface statistics

Print all listening TCP servers

netstat -tln
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address               Foreign Address             State
tcp        0      0 0.0.0.0:3306                0.0.0.0:*                   LISTEN
tcp        0      0 0.0.0.0:111                 0.0.0.0:*                   LISTEN
tcp        0      0 :::80                       :::*                        LISTEN
tcp        0      0 :::22                       :::*                        LISTEN
tcp        0      0 :::443                      :::*                        LISTEN

Print all listening TCP servers and the owning PID/program name (root required)

netstat -tlnp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address               Foreign Address             State       PID/Program name
tcp        0      0 0.0.0.0:587                 0.0.0.0:*                   LISTEN      6702/sendmail
tcp        0      0 0.0.0.0:25                  0.0.0.0:*                   LISTEN      6702/sendmail
tcp        0      0 ::1:4190                    :::*                        LISTEN      6576/master
tcp        0      0 :::143                      :::*                        LISTEN      6576/master
tcp        0      0 :::22                       :::*                        LISTEN      6500/sshd

Print all listening UDP servers

netstat -uln
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address               Foreign Address             State
udp        0      0 0.0.0.0:111                 0.0.0.0:*
udp        0      0 192.168.1.255:123           0.0.0.0:*
udp        0      0 192.168.1.154:123           0.0.0.0:*
udp        0      0 127.0.0.1:123               0.0.0.0:*
udp        0      0 0.0.0.0:123                 0.0.0.0:*
udp        0      0 :::123                      :::*

Print routing table (tmtowtdi: route -en)

netstat -rn
Kernel IP routing table
Destination     Gateway         Genmask         Flags   MSS Window  irtt Iface
192.168.1.0     0.0.0.0         255.255.255.0   U         0 0          0 wlan0
169.254.0.0     0.0.0.0         255.255.0.0     U         0 0          0 wlan0
0.0.0.0         192.168.1.1     0.0.0.0         UG        0 0          0 wlan0

nmap

network exploration tool and security scanner

Show open ports and version information for HOST

nmap -sS -sV -O -vvv HOST

Scan 192.168.1.0 network and report what hosts respond to ICMP echo or connect requests to TCP ports 22 or 80

nmap -sP -PS22,80 192.168.1.0/24

Consult reverse DNS records for host names on NETWORK

nmap -sL NETWORK

ping

send ICMP ECHO_REQUEST to network hosts

ping HOST 100 times

ping -c 100 HOST

route

show / manipulate the IP routing table

Add default gateway 192.168.1.1 to interface eth0

route add default gw 192.168.1.1 eth0

Either of these commands delete the default gateway

route del default
route del -net 0.0.0.0

Add route to network 192.168.0.0/24 for interface eth0

route add -net 192.168.0.0 netmask 255.255.255.0 eth0

Add gateway 192.168.0.1 for 192.168.0.0 network for interface eth0

route add -net 192.168.0.0 netmask 255.255.255.0 gw 192.168.0.1 eth0

To delete the route for the 192.168.0.0 network

route del -net 192.168.0.0 netmask 255.255.255.0

scp

secure copy (remote file copy program)

Copy file foo to foobar as user eberg on host fore.ebloft.sea

scp foo eberg@fore.ebloft.sea:foobar
eberg@fore's password:
foo                                           100% 1024KB   1.0MB/s   00:01

snmpget

query SNMP entity

Print name, hardware, operating system and version information for localhost using output format -Os. See the snmpcmd manual page for a full list of output formats.

snmpget -Os -c public -v 1 localhost sysDescr.0
sysDescr.0 = STRING: Linux fore 2.6.18.8-0.7-default #1 SMP Tue Oct 2 17:21:08 UTC 2007 i686

snmpwalk

query SNMP entity and subtree

Query the system object identifer (OID) for localhost. For a list of OIDs, see /usr/share/snmp/mibs.

snmpwalk -Os -c public -v 1 localhost system
sysDescr.0 = STRING: Linux fore 2.6.18.8-0.7-default #1 SMP Tue Oct 2 17:21:08 UTC 2007 i686
sysObjectID.0 = OID: netSnmpAgentOIDs.10
sysUpTimeInstance = Timeticks: (545206) 1:30:52.06
sysContact.0 = STRING: Sysadmin (root@localhost)
sysName.0 = STRING: fore
sysLocation.0 = STRING: Server Room
...

ss

Expanded and enhanced update to netstat

Print all listening TCP servers

ss -tln
State      Recv-Q Send-Q        Local Address:Port          Peer Address:Port
LISTEN     0      128                      :::22                      :::*
LISTEN     0      128                       *:22                       *:*
LISTEN     0      128               127.0.0.1:5432                     *:*
LISTEN     0      128                     ::1:5432                    :::*
LISTEN     0      100                     ::1:25                      :::*
LISTEN     0      100               127.0.0.1:25                       *:*
LISTEN     0      100                      :::443                     :::*
LISTEN     0      1          ::ffff:127.0.0.1:8005                    :::*
LISTEN     0      100                      :::80                      :::*

ssh

OpenSSH SSH client

Login as USER at HOST

ssh -l USER HOST

Forward local port 9999 to REMOTE_HOST port 8118. The -f option instructs SSH to run in the background. The -N option keeps the remote SSH session open until it is manually killed.

ssh -f -N -L 9999:REMOTE_HOST:8118 localhost

tc

show / manipulate traffic control settings

Linux traffic control engine. . .

tc -s qdisc ls dev eth0
qdisc pfifo_fast 0: bands 3 priomap  1 2 2 2 1 2 0 0 1 1 1 1 1 1 1 1
 Sent 16381732 bytes 74313 pkt (dropped 0, overlimits 0 requeues 0)
 rate 0bit 0pps backlog 0b 0p requeues 0

tcpdump

dump traffic on a network

Print information about packets in even more verbose detail

tcpdump -vv

Save raw packets to FILE

tcpdump -w FILE

Print UDP packets on port 161 originating from HOST from previously saved FILE

tcpdump -r FILE udp and port 161 and src host HOST

Print packets from previously saved FILE that did not originate or terminate on the snmp (161) port

tcpdump -r FILE ! port snmp

traceroute

print the route packets take to network host

List all of the hops a packet goes through to reach HOST

traceroute HOST

whois

client for the whois service

Report owner, contact information for an IP address or domain name

whois 172.16.12.0
[Querying whois.arin.net]
[whois.arin.net]

OrgName:    Internet Assigned Numbers Authority
OrgID:      IANA
Address:    4676 Admiralty Way, Suite 330
City:       Marina del Rey
StateProv:  CA
PostalCode: 90292-6695
Country:    US

NetRange:   172.16.0.0 - 172.31.255.255
CIDR:       172.16.0.0/12
NetName:    IANA-BBLK-RESERVED
NetHandle:  NET-172-16-0-0-1
Parent:     NET-172-0-0-0-0
NetType:    IANA Special Use
...

wireshark (tshark)

interactively browse network traffic (Once known as ethereal and tethereal)

Show HTTP traffic from host 192.168.1.154

tshark src host 192.168.1.154 and port http
Capturing on eth1
  0.000000 192.168.1.154 -> 192.168.1.151 TCP 38377 > http [SYN] Seq=4122895926 Ack=0 Win=5840 Len=0
  0.001512 192.168.1.154 -> 192.168.1.151 TCP 38377 > http [ACK] Seq=4122895927 Ack=282940905 Win=1460 Len=0
  0.032672 192.168.1.154 -> 192.168.1.151 TCP 38377 > http [ACK] Seq=4122895927 Ack=282940905 Win=1460 Len=0
  0.033981 192.168.1.154 -> 192.168.1.151 HTTP GET / HTTP/1.1
  0.100101 192.168.1.154 -> 192.168.1.151 TCP 38378 > http [SYN] Seq=4134029462 Ack=0 Win=5840 Len=0

Capture traffic on loopback device

tshark -i lo

Capture RST packets on port 6881

tshark 'port 6881 and tcp[tcpflags] & tcp-rst !=0'

Performance

free

display amount of free and used memory in the system

Report on used and available memory on the system

free
             total       used       free     shared    buffers     cached
Mem:        515320     507272       8048          0      14072     318448
-/+ buffers/cache:     174752     340568
Swap:      1048568        128    1048440

iostat

report CPU and input/output statistics for devices and partitions

Report disk activity statistics every five seconds for /dev/hda and its partitions

iostat -t -k -d -p /dev/hda 5
Time: 09:14:34 PM
Device:            tps    kB_read/s    kB_wrtn/s    kB_read    kB_wrtn
hda               0.80         0.00         3.19          0         16
hda2              0.80         0.00         3.19          0         16
hda1              0.00         0.00         0.00          0          0

Time: 09:14:39 PM
Device:            tps    kB_read/s    kB_wrtn/s    kB_read    kB_wrtn
hda              12.18         0.00        62.28          0        312
hda2             15.57         0.00        62.28          0        312
hda1              0.00         0.00         0.00          0          0
...

top

display Linux tasks

Start top with an update every second. Screenshot

top -d1

vmstat

report virtual memory statistics

Report memory statistics every 5 seconds 12 times

vmstat 5 12
procs -----------memory---------- ---swap-- -----io---- --system-- ----cpu----
 r  b   swpd   free   buff  cache   si   so    bi    bo   in    cs us sy id wa
 0  0    128   7524  14144 318496    0    0    49    15  294   166 14  1 85  1
 0  0    128   7556  14160 318500    0    0     1   133  282    83 10  0 89  1
 0  0    128   7572  14160 318500    0    0     0     0  270    62 10  0 90  0
...

Security

iptables

administration tool for IPv4 packet filtering and NAT

Print all rules, preceded by the rule number

iptables -L -n --line-numbers
Chain FORWARD (policy ACCEPT)
num  target     prot opt source               destination

Chain INPUT (policy ACCEPT)
num  target     prot opt source               destination
1    ACCEPT     all  --  192.0.2.0/24         0.0.0.0/0
2    ACCEPT     icmp --  0.0.0.0/0            0.0.0.0/0           icmp type 8 limit: avg 1/sec burst 5
3    DROP       icmp --  0.0.0.0/0            0.0.0.0/0           icmp type 8

Chain OUTPUT (policy ACCEPT)
num  target     prot opt source               destination

Insert a rule at position two in the INPUT chain

iptables -I INPUT 2 ...

Append a rule after all of the previous ones in the INPUT chain

iptables -A INPUT ...

Delete rule number five in the INPUT chain

iptables -D INPUT 5

Delete all of the rules in the OUTPUT chain

iptables -F OUTPUT

iptables-save

save IP Tables

Save current rules to FILE in format that can be restored by iptables-restore command

iptables-save > FILE

Display all rules in IP tables with packet and byte counters

iptables-save -c
# Generated by iptables-save v1.3.0 on Fri Feb 17 02:34:59 2006
*filter
:FORWARD ACCEPT [0:0]
:INPUT ACCEPT [34305:24343182]
:OUTPUT ACCEPT [31459:4170095]
[0:0] -A INPUT -s 192.0.2.0/255.255.255.0 -j ACCEPT
[24:2448] -A INPUT -s 127.0.0.1 -j ACCEPT
[0:0] -A INPUT -i eth0 -p icmp -m icmp --icmp-type 8 -m limit --limit 1/sec -j ACCEPT
[0:0] -A INPUT -i eth0 -p icmp -m icmp --icmp-type 8 -j DROP
COMMIT
# Completed on Fri Feb 12 12:34:59 2006

iptables-restore

restore IP Tables

Delete any current rules and restore IP Tables from FILE created previously by the iptables-save command

iptables-restore < FILE

snort

open source network intrusion detection system

See http://www.snort.org

Desktop

alsamixer

soundcard mixer for ALSA soundcard driver, with ncurses interface

Manipulate sound settings for ALSA soundcard driver. Screenshot

alsamixer

display

ImageMagick's very fast image viewer

Display BMP, GIF, JPEG, PNG, SVG, PDF, TIFF FILE

display FILE

eog

GNOME's slightly slower image viewer

Display BMP, GIF, JPEG, PNG FILE

eog FILE

evince

PDF and PostScript viewer

View PDF or PostScript FILE

evince FILE

gimp

an image manipulation and paint program

Capable graphics program. See http://manual.gimp.org

inkscape

Scalable Vector Graphics (SVG) editor

Aims to compete with the likes of Adobe Illustrator and Corel Draw. Native save format is W3C standard SVG. See http://www.inkscape.org

Export existing SVGFILE to PNGFILE

inkscape --export-png=PNGFILE SVGFILE

mplayer

media player

Stream media (mpg, mov, wmv, mp3, avi, etc.) file from URL

mplayer URL

xmms

Audio player

Plays and streams MPEG, Ogg Vorbis, WAV, etc. formats

xmms

xdpyinfo

Display X Server settings

Print screen resolution and DPI

xdpyinfo | grep -C1 resolution
  dimensions:    1920x1200 pixels (332x210 millimeters)
  resolution:    147x145 dots per inch
  depths (7):    24, 1, 4, 8, 15, 16, 32

xset

user preference utility for X

Show X Server settings for user

xset q
Keyboard Control:
  auto repeat:  on    key click percent:  0    LED mask:  00000000
  auto repeat delay:  300    repeat rate:  62
  auto repeating keys:  00ffffffdffffbbf
                        fadfffdfffdfe5ef
                        ffffffffffffffff
                        ffffffffffffffff
  bell percent:  50    bell pitch:  400    bell duration:  100
Pointer Control:
  acceleration:  5/2    threshold:  4
Screen Saver:
  prefer blanking:  yes    allow exposures:  yes
  timeout:  0    cycle:  0
Colors:
  default colormap:  0x20    BlackPixel:  0    WhitePixel:  16777215
Font Path:
  unix/:7100
Bug Mode: compatibility mode is disabled
DPMS (Energy Star):
  Standby: 7200    Suspend: 9240    Off: 14280
  DPMS is Enabled
  Monitor is On
File paths:
  Config file:  /etc/X11/xorg.conf
  Modules path: /usr/X11R6/lib/modules
  Log file:     /var/log/Xorg.0.log

Set your friend's keyboard to repeat at three characters per second

xset r rate 250 3

Bonus: make the mouse super fast

xset m 50/2 4

After the laugh, reset mouse to the default, usually a more appropriate setting

xset m default

xwd/xwud

dump an image of an X window/image displayer

Save screen dump of display to FILE

xwd -out FILE

Display xwd screen dump from FILE

xwud -in FILE

xwininfo

shows information about window

Display size, geometry, name, color depth, etc information about selected window

xwininfo

vncserver

launches X Server for VNC

Start vncserver with desktop width and height of 1024x768 pixels

vncserver -geometry 1024x768

Stop the vncserver running on display :2

vncserver -kill :2

vncviewer

attaches to VNC server

Connect to VNC server running on HOST on display :2

vncviewer HOST:2

Bash Shell Scripting

Looping

Read input from FILE into variables name1, name2, name3

while read name1 name2 name3
do
 echo $name1 $name2 $name3
done < FILE

Iterate from one to ten

for (( i=1; i<=10; i++ ))
do
 echo $i
done

Another simple bash way

for i in {1..10}
do
 echo $i
done

Another way to do the same thing using seq

for i in $(seq 1 10)
do
 echo $i
done

Same thing using a while loop

i=1
while (( i<=10 ))
do
 echo $i
 let i+=1
done

Loop over elements in an array

ary=(foo bar baz foobar)
for ((i=0; i < ${#ary[@]}; i++))
do
  echo ${ary[$i]}
done

foo
bar
baz
foobar

Loop over elements in an associative array (available in bash version 4 and later)

declare -A hash=([apple]=red [banana]=yellow [guanabana]=green)
for key in ${!hash[@]}
do
  echo $key: ${hash[$key]}
done

apple: red
guanabana: green
banana: yellow

Conditionals

An if-else conditional testing two numbers for equality

if [ $foo -eq $bar ]
then
 echo $foo equals $bar
elif [ $foo -lt $bar ]
then
 echo $foo is less than $bar
elif [ $foo -gt $bar ]
then
 echo $foo is greater than $bar
fi

Another valid way to test two numbers for equality. Note: parameter expansion happens automatically within the ((expression)) construct.

if (( foo == bar ))
then
 echo $foo equals $bar
else
 echo $foo does not equal $bar
fi

An if-else conditional testing two strings

if [ $foo = $bar ]
then
 echo strings $foo and $bar are the same
else
 echo strings $foo and $bar differ
fi

Arithmetic Evaluation

Arithmetic expressions are evaluated inside ((expression)). If the expression evaluates to true, 1 is returned, otherwise 0. Use $((expression)) to return the result of the expression.

for i in {1..100}
do
 if (( (i % (3*5)) == 0 ))
 then
  echo $i: FizzBuzz
 fi
done
15: FizzBuzz
30: FizzBuzz
45: FizzBuzz
60: FizzBuzz
75: FizzBuzz
90: FizzBuzz

let i=2
echo $((i ** 4))
16
echo $((i > 4))
0
echo $((i < 4))
1
echo $((i == 2))
1
echo $((i += 2))
4
echo $i
4

let i=0
while ((i < 10))
do
 echo $((++i))
done
1
2
3
4
5
6
7
8
9
10

Functions

store a series of commands for later execution

Save function named "psg" to search command line for first argument which is then passed to ps for output (see: pgrep)

psg () { ps uwwp $(pgrep -f ${1:?Specify a search term}) 2>/dev/null; }
psg ssh
USER       PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
root      2061  0.0  0.1   4568  1016 ?        Ss   08:46   0:00 /usr/sbin/sshd
eb        2623  0.0  0.1   3864   688 ?        Ss   08:46   0:00 /usr/bin/ssh-agent
eb        2819  0.0  0.3   4708  1820 pts/2    S+   09:10   0:00 ssh erikberg.com
eb        2919  0.0  0.3   4540  1860 pts/6    S+   09:25   0:00 ssh fore

Create a function named f2c to convert Fahrenheit to Celsius using the first argument to perform the conversion (see: bc)

f2c () { printf "%g\n" $(echo "(${1:?Input Missing}-32)*5/9" | /usr/bin/bc -l); }
f2c 81
27.2222

And function c2f to convert the other way

c2f () { printf "%g\n" $(echo "${1:?Input Missing}*(9/5)+32" | /usr/bin/bc -l); }
c2f 10
50

Display all defined functions. (tmtowtdi: declare -f)

typeset -f
c2f ()
{
    printf "%g\n" $(echo "${1:?Input Missing}*(9/5)+32" | /usr/bin/bc -l)
}
f2c ()
{
    printf "%g\n" $(echo "(${1:?Input Missing}-32)*5/9" | /usr/bin/bc -l)
}
psg ()
{
    ps uwwp $(pgrep -f ${1:?Specify a search term}) 2>dev/null
}

Remove function NAME

unset -f NAME

Tips

For filenames containing spaces, rename files replacing spaces with underscores

ls
01 Don't Panic.m4a
02 Caring Is Creepy.m4a
03 In the Waiting Line.m4a
04 New Slang.m4a

for file in *[[:blank:]]*
do
 mv "$file" ${file// /_}
done

ls
01_Don't_Panic.m4a
02_Caring_Is_Creepy.m4a
03_In_the_Waiting_Line.m4a
04_New_Slang.m4a

Say you have copied text with your mouse and now want to process that text further with a quick script. Saving the clipboard contents to an intermediary file or using cat is not necessary. Just use a Here Document.

while read line
do
 echo processing $line
done <<eof
paste clipboard contents
eof