<?php include('HEADER.php'); ?>

<h1>Linux Commands</h1>

<p>A little of this is Debian specific, but most of it works on any Linux or unix-like system.</p>

<p>Also, here are some <a href="#commandlinefu">top tips from commandlinefu.com</a>.</p>

<p>The <a href="http://www.debian.org/doc/manuals/debian-reference/">Debian Reference</a> is useful.</p>

<h2>Automation</h2>
<ul>
<li><a href="#at">at</a></li>
<li><a href="#crontab">crontab</a></li>
<li><a href="#watch">watch</a></li>
</ul>

<h2>Filesystems, disks, block devices</h2>
<ul>
<li><a href="#file">file</a></li>
<li><a href="#findmnt">findmnt</a></li>
<li><a href="#lsblk">lsblk</a></li>
</u>

<h2>Help</h2>
<ul>
<li><a href="#man">man</a></li>
<li><a href="#whatis">whatis</a></li>
</ul>

<h2>IO &amp; Disk Usage</h2>
<ul>
<li><a href="#du">du</a></li>
<li><a href="#iotop">iotop</a></li>
</ul>

<h2>Misc</h2>
<ul>
<li><a href="#chroot">chroot</a></li>
<li><a href="#fc-list">fc-list</a></li>
<li><a href="#import">import</a></li>
<li><a href="#ldd">ldd</a></li>
<li><a href="#logger">logger</a></li>
<li><a href="#look">look</a></li>
<li><a href="#notify-send">notify-send</a></li>
<li><a href="#rlwrap">rlwrap</a></li>
<li><a href="#sudosu">sudo su</a></li>
<li><a href="#xfontsel">xfontsel</a></li>
<li><a href="#xlsfonts">xlsfonts</a></li>
</ul>

<h2>Network</h2>
<ul>
<li><a href="#ethtool">ethtool</a></li>
<li><a href="#iftop">iftop</a></li>
<li><a href="#ip">ip</a></li>
<li><a href="#iptables">iptables</a></li>
<li><a href="#iptraf">iptraf</a></li>
<li><a href="#lsof-i">lsof -i</a></li>
<li><a href="#netcat">netcat (nc)</a></li>
<li><a href="#nethogs">nethogs</a></li>
<li><a href="#nload">nload</a></li>
<li><a href="#nmap">nmap</a></li>
<li><a href="#ping">ping</a></li>
<li><a href="#sar">sar</a></li>
</ul>

<h2>Package Management</h2>
<ul>
<li><a href="#apt-cachesearch">apt-cache search</a></li>
<li><a href="#apt-cacheshow">apt-cache show</a></li>
<li><a href="#apt-file">apt-file search</a></li>
<li><a href="#dpkg-l">dpkg -l</a></li>
<li><a href="#dpkg-lcap">dpkg -L</a></li>
<li><a href="#dpkg-s">dpkg -S</a></li>
</ul>

<h2>Processes</h2>
<ul>
<li><a href="#kill">kill</a></li>
<li><a href="#lsof">lsof</a></li>
<li><a href="#nice">nice</a></li>
<li><a href="#pidof">pidof</a></li>
<li><a href="#pstree">pstree</a></li>
<li><a href="#sar">sar</a></li>
<li><a href="#strace">strace</a></li>
<li><a href="#time">time</a></li>
<li><a href="#timeout">timeout</a></li>
<li><a href="#top">top</a></li>
<li><a href="#ulimit">ulimit</a></li>
</ul>

<h2>Shell &amp; Files</h2>
<ul>
<li><a href="#awk">awk</a></li>
<li><a href="#cd-">cd -</a></li>
<li><a href="#diff">diff</a></li>
<li><a href="#file">file</a></li>
<li><a href="#find">find</a></li>
<li><a href="#grep">grep</a></li>
<li><a href="#history">history</a></li>
<li><a href="#lessf">less +F</a></li>
<li><a href="#md5sum">md5sum</a></li>
<li><a href="#nl">nl</a></li>
<li><a href="#rsync">rsync</a></li>
<li><a href="#sed">sed</a></li>
<li><a href="#ssh">ssh</a></li>
<li><a href="#stat">stat</a></li>
<li><a href="#tac">tac</a></li>
<li><a href="#tar">tar</a></li>
<li><a href="#vimdiff">vimdiff</a></li>
<li><a href="#xargs">xargs</a></li>
<li><a href="#zcat">zcat, zgrep, zless</a></li>
</ul>

<h2>Sound</h2>
<ul>
<li><a href="#avconv">avconv</a> (ffmpeg)</li>
<li><a href="#pacmd">pacmd</a></li>
</ul>

<h2>System &amp; Kernel</h2>
<ul>
<li><a href="#lsmod">lsmod</a></li>
<li><a href="#lspci">lspci</a></li>
<li><a href="#lsusb">lsusb</a></li>
<li><a href="#modprobe">modprobe</a></li>
<li><a href="#service">service</a></li>
<li><a href="#uptime">uptime</a></li>
</ul>

<h2>Time</h2>
<ul>
<li><a href="#ntpq">ntpq</a></li>
<li><a href="#ntpdate">ntpdate</a></li>
</ul>

<h2>Xorg</h2>
<ul>
<li><a href="#xrandr">xrandr</a></li>
<li><a href="#xrdb">xrdb</a></li>
</ul>

<h2 id="apt-cachesearch"><code>apt-cache search</code> finds packages</h2>

<p>Search for packages related to foo with <code>apt-cache search foo</code>. It accepts regular expressions. <code>--full</code> outputs more info for each package. <code>--names-only</code> searches only the package names (like <code>apt-cache pkgnames |grep human</code>, but also outputs the short package descriptions).</p>

<h2 id="apt-cacheshow"><code>apt-cache show <i>foo</i></code> shows info for package <i>foo</i></h2>

<h2 id="apt-file"><code>apt-file search foo</code> finds files in packages</h2>

<h2 id="at"><code>at</code> runs a command at scheduled time</h2>

<pre>
$ at 20:04
warning: commands will be executed using /bin/sh
at&gt; export DISPLAY=:0
at&gt; zenity --warning --title="Alarm" --text="Tea time!"
at&gt; ^D
job 1 at Mon Oct 20 20:04:00 2013
$
$ at -f myjob.sh 18:30 tuesday
warning: commands will be executed using /bin/sh
job 9 at Tue Oct 22 18:30:00 2013
</pre>

<p>The -f flag executes commands in a file.</p>

<p><code>atq</code> shows pending jobs. <code>atrm <i>jobnumber</i></code> deletes a job.</p>

<h2 id="avconv"><code>avconv</code> converts media files</h2>

<p>convert .flac files in current directory to .mp3:</p>

<pre>for a in *.flac; do   &lt; /dev/null avconv -i "$a" -qscale:a 0 "${a[@]/%flac/mp3}"; done</pre>

<p>(avconv is Debian's ffmpeg.)

<h2 id="awk"><code>awk</code> processes text records</h2>

<p>See my <a href="http://paulgorman.org/technical/awk.txt">awk notes</a>.</p>

<pre>$ echo "one two three" | awk '{print $2}'
two

$ awk -F':' '{print $1,$6}' &lt; /etc/passwd | column -t
root         /root
daemon       /usr/sbin
bin          /bin
sys          /dev
sync         /bin
games        /usr/games
man          /var/cache/man
lp           /var/spool/lpd

$ awk '$6 ~/[0-9]$/' /etc/fstab | awk '$6 &gt;1' | column -t
/dev/mapper/vg0-Boot  /boot    ext4  defaults  0  2
/dev/mapper/vg0-Home  /home    ext4  defaults  0  2
/dev/mapper/vg0-Var   /var     ext4  defaults  0  2
/dev/sdb1             /backup  ext4  defaults  0  2</pre>

<h2 id="cd-"><code>cd -</code> to previous directory</h2>

<p><code>cd -</code> changes to previous working directory, so you can switch back and forth between two directories.</p>

<h2 id="chroot"><code>chroot ~/jail /bin/foo</code> confines foo to use path as its root directory</h2>

<h2 id="crontab"><code>crontab</code> schedules jobs</h2>

<p>Edit your crontab with <code>crontab -e</code>. List your current crontab with <code>crontab -l</code>. The crontab fields are:</p>

<pre>
*     *     *   *    *        command to be executed
-     -     -   -    -
|     |     |   |    |
|     |     |   |    +----- day of week (0 - 6) (Sunday=0)
|     |     |   +------- month (1 - 12)
|     |     +--------- day of        month (1 - 31)
|     +----------- hour (0 - 23)
+------------- min (0 - 59)</pre>

<p>Crontab entry examples:</p>

<pre>15 * * * * echo “Run at 15 min past every hour”
22 8 * * * echo “This command is run daily at 8:22 am”
22 20 * * * echo “This command is run daily at 8:22 pm”
* 4 * * Sun echo “Runs at 4 am every Sunday”
42 3 1 * * echo “Runs 3:42 am every 1st of the month”
01 * 19 07 * echo “This command is run hourly on the 19th of July”</pre>

<h2 id="diff"><code>diff</code> compares files</h2>

<pre>diff --side-by-side file1.txt file2.txt</pre>

<p><code>colordiff</code> is a colorizing wrapper for diff, but it's not installed by default.</p>

<p>See also <a href="#vimdiff">vimdiff</a>.</p>

<h2 id="dpkg-l"><code>dpkg -l</code> lists installed packages</h2>

<p><code>dpkg -l | grep foo</code> see if package foo is installed.</p>

<h2 id="dpkg-lcap"><code>dpkg -L</code> lists files installed by package</h2>

<p><code>dpkg -L foo</code> lists all the files installed by package foo.</p>

<h2 id="dpkg-s"><code>dpkg -S</code> names package that installed file</h2>

<p><code>dpkg -S /foo/bar</code> names the package that supplied the file.</p>

<h2 id="du"><code>du</code> for disk usage</h2>

<p>Just as <code>df -h</code> shows what percentage of each volume is in use, <code>du -hs <i>filename</i></code> shows the size of a directory (or file). The <code>-s</code> flag is <i>summary</i>; dropping it shows the size of each subdirectory. <code>du -ah</code> shows the size of each file in in a directory:</p>

<pre>
$ du -ah Books/ | sort -nr | head -n 5
1008K   Books/Technical/onlisp.pdf
976K    Books/Technical/Eloquent JavaScript/img
908K    Books/Technical/Regular_Expression_Pocket_Reference_Second_Edition.pdf
868K    Books/DnD/Adventures/the_pod_caverns_of_the_sinister_shroom.pdf
848K    Books/Technical Papers/diveintopython-html-5.4.zip
$
$ sudo du -d 1 -h /var |sort -h
4.0K	/var/local
4.0K	/var/opt
4.0K	/var/tmp
8.0K	/var/mail
16K	/var/gopher
3.4M	/var/www
6.3M	/var/spool
13M	/var/backups
75M	/var/log
279M	/var/lib
2.7G	/var/cache
3.1G	/var
$ 
</pre>

<h2 id="ethtool"><code>ethtool</code> described network interface</h2>

<p><code>sudo ethtool eth0</code> shows a lot of info about the interface, including its link mode and the advertised link modes of its partners, and whether it detects link. It can also force various settings, like <code>sudo ethtool -s eth0 autoneg off duplex full</code> to force full duplex mode.</p>

<h2 id="fc-list"><code>fc-list</code> list fonts recognized by fontconfig</h2>

<h2 id="file"><code>file</code> shows file type</h2>

<p><code>file</code> classifies the types of files supplied as arguments. <code>file -s /dev/sdb1</code> reads a special block or character file, which can indicate whether data or a filesystem exists on it.</p>

<pre>23:15 bava ~ $ sudo file -s /dev/sdb1
/dev/sdb1: Linux rev 1.0 ext4 filesystem data, UUID=93a51594-79c4-4ef9-8fc7-c5d8aa3953a4 (needs journal recovery) (extents) (large files) (huge files)
23:18 bava ~ $ file emacstest.txt
emacstest.txt: ASCII text
23:18 bava ~ $ file june
june: symbolic link to `/home/paulgorman/Documents/2014/june/'
23:19 bava ~ $ file marks
marks: directory</pre>

<h2 id="find">find <span style="font-size:0.6em;">(and note 'xargs')</span></h2>

<pre class="code">paulgorman@falstaff:~$ find -iname pingwan*
./bin/pingwan.pl
./repo/pingwan
./repo/pingwan/.hg/store/data/pingwan.pl.i
./repo/pingwan/pingwan.pl
paulgorman@falstaff:~$
paulgorman@falstaff:~$ find -iname pingwan.pl -exec ls -lh {} \;
-rw-r--r-- 1 paulgorman paulgorman 9.1K May 10 09:19 ./bin/pingwan.pl
-rw-r--r-- 1 paulgorman paulgorman 7.1K Jan 19  2010 ./repo/pingwan/pingwan.pl
paulgorman@falstaff:~$ 
paulgorman@falstaff:~$ find -iname pingwan.pl | <b>xargs</b> grep Bloomfield
./bin/pingwan.pl:#       name    =&gt;  'Bloomfield Villas Clubhouse',
./bin/pingwan.pl:        name    =&gt;  'Bloomfield Villas Lsg',
./bin/pingwan.pl:        name    =&gt;  'Glens of Bloomfield',
./repo/pingwan/pingwan.pl:#		name	=&gt;	'Bloomfield Villas Clubhouse',
./repo/pingwan/pingwan.pl:		name	=&gt;	'Bloomfield Villas Lsg',
./repo/pingwan/pingwan.pl:		name	=&gt;	'Glens of Bloomfield',
paulgorman@falstaff:~$ 
</pre>

<p>Or find files modified within the last hour:</p>

<pre>find ~ -type f -mtime 0</pre>

<p><code>find</code> has <em>lots</em> of options. Some useful ones include <code>-not -maxdepth <i>N</i> -name -iname -type [fd...] -perm <i>NNN</i> -regex <i>pattern</i></code>.</p>

<p><code>find .</code> recursively lists all the files under the current directory.</p>

<h2 id="findmnt"><code>findmnt</code> shows a tree of mounted filesystems</h2>

<pre>
$ findmnt
TARGET                       SOURCE                               FSTYPE      OPTIONS
/                            /dev/mapper/LVM_Volume_Group_01-Root ext4        rw,relatime,errors=remount-ro,data=orde
├─/sys                       sysfs                                sysfs       rw,nosuid,nodev,noexec,relatime
│ └─/sys/fs/fuse/connections fusectl                              fusectl     rw,relatime
├─/proc                      proc                                 proc        rw,nosuid,nodev,noexec,relatime
│ └─/proc/sys/fs/binfmt_misc binfmt_misc                          binfmt_misc rw,nosuid,nodev,noexec,relatime
├─/dev                       udev                                 devtmpfs    rw,relatime,size=10240k,nr_inodes=39911
│ └─/dev/pts                 devpts                               devpts      rw,nosuid,noexec,relatime,gid=5,mode=62
├─/run                       tmpfs                                tmpfs       rw,nosuid,noexec,relatime,size=3194316k
│ ├─/run/lock                tmpfs                                tmpfs       rw,nosuid,nodev,noexec,relatime,size=51
│ └─/run/shm                 tmpfs                                tmpfs       rw,nosuid,nodev,noexec,relatime,size=63
├─/boot                      /dev/mapper/LVM_Volume_Group_01-Boot ext4        rw,relatime,stripe=4,data=ordered
├─/home                      /dev/mapper/LVM_Volume_Group_01-Home ext4        rw,relatime,data=ordered
├─/var                       /dev/mapper/LVM_Volume_Group_01-Var  ext4        rw,relatime,data=ordered
│ └─/var/lib/nfs/rpc_pipefs  rpc_pipefs                           rpc_pipefs  rw,relatime
└─/backup                    /dev/sdb1                            ext4        rw,relatime,data=ordered
</pre>

<h2 id="grep">grep</h2>

<p>To grep which files in the current directory contain foo: <code>grep -lr foo .</code>.</p>

<h2 id="history"><code>history</code> shows shell command history</h2>

<p>One of my favorite shell aliases:</p>

<pre>alias h='history 60 | sort -k2 | uniq -f2 | sort -bn'</pre>

<h2><code>import</code> makes screenshots</h2>

<p>Use the Imagemagick utility <code>import</code> to capture screenshots:</p>

<pre>import bat.png
import -frame foo.png
import -window root bar.png
</pre>

<h2 id="iftop"><code>iftop</code> monitors network activity</h2>

<p><span class="code">iftop</span> is top for network activity. If you want to turn off DNS lookups run it with the -n flag or just hit "n" while it's running to toggle lookups. The "p" key toggle display of ports.</p>

<h2 id="import"><code>import</code> takes a screenshot</h2>

<p><code>import -window root ~/foo.png</code> does screen capture. It's part of ImageMagick.</p>

<h2 id="iotop"><code>iotop</code> monitors disk usage</h2>

<p><span class="code">iotop</span> is top for disc reads and writes. The flags <code>-ao</code> are useful. <code>-a</code> shows accumulated I/O instead of bandwidth. <code>-o</code> shows only processes or threads actively doing I/O.</p>

<h2 id="ip"><code>ip</code> do network stuff</h2>

<code>ip addr show
ip link show
ip --stats link
ip addr add 192.168.1.100/24 dev eth0
ip link set eth0 up
ip route show
ip route add default via 192.168.1.1
ip route show cache
ip route flush cache
ip neighbor show</code>

<p>Note that <code>ip route show</code> won't necessarily show <em>all</em> routes; see ip-rule(8). <code>ip route show table all</code> <em>will</em> show all the routes.</p>

<p><code>ip</code> replaces <code>ifconfig</code>, <code>route</code>, <code>iptunnel</code>, and <code>arp</code>.</p>

<h2 id="iptables"><code>iptables</code> filter packets (firewall)</h2>

<p>Kernel packet filter. Rules are organized into chains, where a packet is matched against each rule in the chain. Chains are grouped into tables; by default the tables are filter (the default table), nat, mangle, raw, and security. The man page is good.</p>

<pre>$ sudo iptables --list --verbose
$ sudo iptables --table nat --numeric --list
</pre>

<p>Use <code>ip6tables</code> for IPv6.</p>

<h2 id="iptraf"><code>iptraf</code> monitors network traffic</h2>

<p><code>iptraf</code> monitors (and can capture) network traffic (connections, ports, type, etc.). It's sort of like <code>netstat</code>, but ongoing rather than a single run (and more colorful).</p>

<h2 id="kill"><code>kill</code> sends signals to processes</h2>

<p>The <code>-l</code> flag lists the signals. <code>kill -TERM pid</code> is the default signal if none is specified. The <code>-KILL</code>, <code>-INT</code>, <code>-HUP</code>, <code>-STOP</code>, and <code>-CONT</code> signals are often used.</p>

<p>Before sending SIGKILL (9), try sending SIGTERM (15) to give the process a chance to clean up after itself.</p>

<p><code>killall foo</code> signals processes by name rather than pid.</p>

<h2 id="ldd"><code>ldd /bin/foo</code> lists shared libraries used by /bin/foo</h2>

<pre>$ ldd /bin/bash
    linux-vdso.so.1 (0x00007fff555fe000)
    libncurses.so.5 => /lib/x86_64-linux-gnu/libncurses.so.5 (0x00007f53c4285000)
    libtinfo.so.5 => /lib/x86_64-linux-gnu/libtinfo.so.5 (0x00007f53c405b000)
    libdl.so.2 => /lib/x86_64-linux-gnu/libdl.so.2 (0x00007f53c3e56000)
    libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007f53c3aae000)
    /lib64/ld-linux-x86-64.so.2 (0x00007f53c44cc000)</pre>

<h2 id="lessf"><code>less +F</code> is a better </code>tail</code></h2>

<p><code>less +F <i>filename.log</i></code> shows continuing updates of the end of a file, like <code>tail -f <i>filename.log</i></code> but better. If you interrupt <code>less</code> (to search or whatever) you can resume following with <i>SHIFT-F</i>.</p>

<h2 id="lsblk"><code>lsblk</code> shows block devices</h2>

<pre>
$ lsblk
NAME                                MAJ:MIN RM   SIZE RO TYPE MOUNTPOINT
sda                                   8:0    0   2.7T  0 disk 
├─sda1                                8:1    0     2M  0 part 
└─sda2                                8:2    0     2T  0 part 
  ├─LVM_Volume_Group_01-Root (dm-0) 254:0    0 558.5G  0 lvm  /
  ├─LVM_Volume_Group_01-Boot (dm-1) 254:1    0   284M  0 lvm  /boot
  ├─LVM_Volume_Group_01-Home (dm-2) 254:2    0 745.1G  0 lvm  /home
  ├─LVM_Volume_Group_01-Var (dm-3)  254:3    0 745.1G  0 lvm  /var
  └─LVM_Volume_Group_01-Swap (dm-4) 254:4    0    60M  0 lvm  [SWAP]
sdb                                   8:16   0 931.5G  0 disk 
└─sdb1                                8:17   0 931.5G  0 part /backup
sr0                                  11:0    1  1024M  0 rom  
</pre>

<h2 id="logger"><code>logger</code> logs messages to syslog</h2>

<pre>$ logger "This is a test of the logger command."
$ logger -p local7.info "Logger can Facility to mark the message source and a Level to mark the message severity."</pre>

<p>See <code>logger(1)</code> and <code>syslog(3)</code>.</p>

<h2 id="look"><code>look</code> looks up entries in a dictionary (or other file)</h2>

<pre>$ look pseudo
pseudo
pseudonym
pseudonym's
pseudonyms
$ look pseudo websters1913-29765-8.txt 
pseudo-martyr, pseudo-philosopher. Also used adjectively.
pseudobacteria. Sternberg.
pseudodipte.] (Arch.)
pseudodox which judgeth evil good, and darkness light." T. Adams.
pseudopipte.] (Arch.)
pseudopipte.] (Arch.)
pseudovarium.
pseudonavicula stage.
pseudopodia, by means of which they move about and take their food.
pseudoneuropterous insects of the genus Termes. These insects are</pre>

<h2 id="lsof"><code>lsof</code> finds processes using a file or port, vise versa</h2>

<p><code>lsof</code> tells which process has a file open (including sockets, directories, device files, etc.). It has many other options, like finding which files a process has open.</p>

<pre>
paulgorman@falstaff:~$ lsof -c gvim
COMMAND   PID       USER   FD   TYPE             DEVICE SIZE/OFF       NODE NAME
gvim    29723 paulgorman  cwd    DIR              254,0    12288   15106050 /home/paulgorman
gvim    29723 paulgorman  rtd    DIR              254,0     4096          2 /
gvim    29723 paulgorman  txt    REG              254,0  2277440   15327526 /usr/bin/vim.gnome
...
paulgorman@falstaff:~$ lsof /home/paulgorman
COMMAND     PID       USER   FD   TYPE DEVICE SIZE/OFF     NODE NAME
wmiirc      740 paulgorman  cwd    DIR  254,0    12288 15106050 /home/paulgorman
wmiirc      776 paulgorman  cwd    DIR  254,0    12288 15106050 /home/paulgorman
x-termina   777 paulgorman  cwd    DIR  254,0    12288 15106050 /home/paulgorman
bash        778 paulgorman  cwd    DIR  254,0    12288 15106050 /home/paulgorman
...
paulgorman@falstaff:~$ lsof -i
COMMAND    PID       USER   FD   TYPE   DEVICE SIZE/OFF NODE NAME
chrome   23339 paulgorman   73u  IPv4 54618659      0t0  TCP falstaff.local:37098->10.100.225.77:https (ESTABLISHED)
rdesktop 28066 paulgorman    4u  IPv4 54470880      0t0  TCP falstaff.local:41502->db.example.com:3389 (ESTABLISHED)
perl     29498 paulgorman    3u  IPv4 54620210      0t0  UDP falstaff.local:47310->ht-terminal-2.example.com:echo
</pre>

<p id="lsof-i"><code>lsof -i</code> shows what processes have ports open.</p>

<h2 id="md5sum"><code>md5sum</code> calculates a file's checksum</h2>

<p>Calculate the checksum for a file:</p>

<pre>paulgorman@falstaff:~$ md5sum buddys_menu.pdf 
9f6de035f72c16711e786e8f955135d9  buddys_menu.pdf
paulgorman@falstaff:~$ </pre>

<p>Note that <code>md5sum -c sums.txt</code> compares files to checksums listed in the text file like:</p>

<pre>99c3a849f6e9a0d143f057433c7f4d84  debian-6.10-desktop-amd64.iso
b950a4d7cf3151e5f213843e2ad77fe3  debian-6.10-desktop-i386.iso
a3494ff33a3e5db83669df5268850a01  debian-6.10-desktop-powerpc.iso
2f44a48a9f5b4f1dff36b63fc2115f40  debian-6.10-server-amd64.iso
cd6c09ff8f9c72a19d0c3dced4b31b3a  debian-6.10-server-i386.iso</pre>

<h2 id="lsmod"><code>lsmod</code> lists loaded kernel modules.</h2>

<h2 id="lspci"><code>lspci</code> shows connected PCI devices.</h2>

<p>The <code>-v</code> or <code>-vv</code> flags show more detail.</p>

<h2 id="lsusb"><code>lsusb</code> shows connected USB devices.</h2>

<p>The <code>-v</code> or <code>-vv</code> flags show more detail.</p>

<h2 id="man"><code>man</code> manual pages</h2>

<pre>$ man -S1 -k cpu
lscpu (1)            - display information on CPU architecture
taskset (1)          - retrieve or set a process's CPU affinity</pre>

<p><code>-S</code> specifies the section (1 is "executable programs or shell commands"). <code>-k</code> searches short descriptions for keywords; like <code>apropos</code>. Remember <code>man man</code>.</p>

<h2 id="modprobe"><code>modprobe</code> manipulates kernel modules.</h2>

<p>The default behavior is to insert the module named in the argument into the running kernel, along with its dependencies if any (e.g. &mdash; <code>modprobe mymod</code>). Prefer <code>modprobe</code> over <code>insmod</code> and <code>rmmod</code>. The <code>-l</code> flags lists all available modules. The <code>-r</code> flag removes a module from the kernel.</p>

<h2 id="netcat">netcat</h2>

<p>Like <code>cat</code>, but works on the network. It does a bunch of diffrent stuff.</p>

<p>Test network connectivity between box A (10.0.0.100) and B.<br>
On A (acting as the server): <code>nc -l -p 4444</code><br>
On B (acting as the client): <code>nc 10.0.0.100 4444</code><br>
We can then type messages back and forth.
</p>

<p>A quick port scan of the first thousand ports:<br />
<code>nc -zvn 10.0.0.14 1-1000 2&gt;&amp;1 |grep succeeded</code>, then grab the banner of an open port:<br />
<code>netcat -v 10.0.0.14 21</code></p>

<p>Ad hoc file tranfer. On the receiving box:<br />
<code>nc -l 9988 &lt; foo.txt</code>, and on the sending box:<br />
<code>nc --n 10.0.0.14 9988 &gt; foo.txt</code></p>

<p><code>dd</code> over the network. On the source box:<br />
<code>dd if=/dev/sda | nc -l 9988</code>, and on the destination box:<br />
<code>nc -n 10.0.0.14 9988 | dd of=/dev/sda</code></p>

<h2 id="nethogs">nethogs</h2>

<p><code>nethogs</code> monitors network usage <em>by process</em>.</p>

<h2 id="nice"><code>nice</code> sets a process's CPU usage</h2>

<p>Set process priority (CPU) for a given process with:</p>

<pre class="prettyprint">nice -n 18 myprocess</pre>

<p>Both positive and negative numbers can be used. Higher numbers are a lower priority. 19 has the least priority. You can also readjust the priority of an already running process using <code class="prettyprint">renice</code>.</p>

<h2 id="nl"><code>nl</code> numbers the lines in a file</h2>

<h2 id="nload"><code>nload</code> visualizes network load</h2>

<p><span class="code">nload</span> is pretty useful for monitoring real-time traffic in and out of an interface.</p>

<h2 id="nmap"><code>nmap</code> scans networks</h2>

<p><code>nmap -sn 10.0.0.0/24</code> to discover IP's in use on the /24 (255.255.255.0) subnet. (In earlier versions, <code>-sn</code> was <code>-sP</code>.)</p>

<h2 id="notify-send"><code>notify-send</code> message to desktop</h2>

<p><code>notify-send "My message"</code> appears as a desktop notification, if libnotify-bin and a notification daemon like dunst are installed.</p>

<h2 id="ntpdate">ntpdate</h2>

<p><code>ntpdate -d 10.0.0.1</code> queries/tests a network time server. The <code>-d</code> flag is debugging mode, and will not alter the time on the local machine; <code>ntpdate</code> can also be used to set the local time.</p>

<h2 id="ntpq">ntpq</h2>

<p><code>ntpq -p</code> prints the status of time servers:</p>

<pre>$ ntpq -p
     remote           refid      st t when poll reach   delay   offset  jitter
==============================================================================
*clock1.albyny.i .CDMA.           1 u  366 1024  377   37.155    0.533   1.720
 15.126.193.186  192.5.41.40      2 u   9d 1024    0   29.098    0.950   0.000
+sisdb01.muskego 173.255.240.184  3 u  886 1024  377   20.897   -1.808   1.592
 barricade.rack9 209.51.161.238   2 u  32d 1024    0   17.961   -0.608   0.000
 10.0.0.1        198.7.57.182     4 u  894 1024  377    0.354  114.271   4.180
</pre>

<h2 id="pacmd"><code>pacmd</code> reconfigures PulseAudio</h2>

<p>Linux sound is kind of a mess, because there are various sound layers and alternatives. Assuming you're using PulseAudio, <code>pacmd list-sinks</code> displays the sound output devices ("sinks") that Pulse knows about; the current/default device is starred. <code>pacmd set-default-sink N</code> changes the current/default output/sink (where <i>N</i> is the number of the device shown by <code>pacmd list-sinks</code>). <code>pacmd list-sources</code> displays the input devices; <code>pacmd set-default-source N</code> changes the current/default input/source device.</p>

<p>Test sound output: <code>cat /dev/urandom | pacat</code></p>

<p>A quick and dirty test mic test: <code>arecord -f cd -c1 | aplay</code>.</p>

<h2 id="pidof"><code>pidof</code> gives ID of named process</h2>

<p>Returns the process ID of any processes matching a supplied name.</p>

<pre>paulgorman@bava:~$ pidof xterm
16841 16789 16737
paulgorman@bava:~$ pidof xterm | xargs kill</pre>

<h2 id="ping"><code>ping</code> can show a summary while running</h2>

<p><code>CTRL |</code> (pipe) forces <code>ping</code> to print the summary statistics without stopping the flow of packets.</p>

<h2 id="pstree"><code>pstree</code> prints a process tree</h2>

<h2 id="pv"><code>pv</code> views pipes</h2>

<p>Insert <code>pv</code> in any pipeline to view its progress:</p>

<pre>paulgorman@bava:~$ dd if=/dev/lvgroup/terminal | gzip -c | pv -c &gt; /dev/null
6.47GB 0:16:36 [98.9kB/s] [    &lt;=&gt;                ] </pre>

<h2 id="rlwrap"><code>rlwrap</code> adds realine support</h2>

<p><code>rlwrap</code> give readline support to things that don't have it, like <code>rlwrap sbcl</code>.</p>

<h2 id="rsync"><code>rsync</code> syncs files</h2>

<p>Local to remote (flags archive, don't overwrite newer files, compress, use human-readable numbers):</p>

<p class="code">rsync -e ssh -auzh SOURCE user@host:DESTINATION</p>

<p>Remote to local:</p>

<p class="code">rsync -e ssh -auvzh user@host:SOURCE DESTINATION</p>

<p>A trailing slash on the path means to copy the <em>contents</em> of the directory. No trailing slash means to copy the directory <em>itself</em>.</p>

<p>The <code>--delete</code> option tells rsync to delete files from the receiving side that don't exist on the sending side.</p>

<p class="code">rsync -e ssh -auvzh --delete ./foo/ user@host:foo/</p>

<p>The <code>--dry-run</code> option shows what it <i>would</i> do with those arguments, without actually making changes.</p>

<h2 id="sed"><code>sed</code> edits streams</h2>

<p>See my <a href="http://paulgorman.org/technical/sed.txt">sed notes</a>.</p>

<pre>$ echo "All the king's horses and all the king's men..." | sed 's/king/dog/g'
All the dog's horses and all the dog's men..</pre>

<p>Change multiple files:</p>

<pre>$ cat a.conf
All the dogs went home.
$
$ cat b.conf 
I fed the dogs lunch.
I fed the dogs dinner.
$
$ for i in ./*; do
&gt; sed -i 's/dog/cat/g' $i
&gt; done
$
$ cat a.conf 
All the cats went home.
$
$ cat b.conf 
I fed the cats lunch.
I fed the cats dinner.
</pre>

<h2 id="service"><code>service</code> controls init scripts</h2>

<pre>$ sudo service apache2 reload
[ ok ] Reloading web server: apache2.</pre>

<p>Using <code>service</code> is preferred over calling init scripts directly (e.g. &mdash; <code>sudo /etc/init.d/apache2 reload</code>), because calling the init script directly doesn't guarantee the service will start with the expected environment (i.e. &mdash; the environment it had at boot time).</p>

<h2 id="sar"><code>sar</code></h2>

<p><span class="code">sar</span> is one of the tools from the sysstat package.</p>

<pre class="code">paulgorman@falstaff:~$ sar 1 3
Linux 2.6.32-5-amd64 (falstaff) 	09/29/2011 	_x86_64_	(8 CPU)

09:50:24 AM     CPU     %user     %nice   %system   %iowait    %steal     %idle
09:50:25 AM     all      1.66      0.13      0.89      0.00      0.00     97.32
09:50:26 AM     all      1.37      0.12      0.87      0.00      0.00     97.63
09:50:27 AM     all      1.77      0.00      0.83      0.00      0.00     97.40
Average:        all      1.60      0.08      0.86      0.00      0.00     97.45
paulgorman@falstaff:~$ 
paulgorman@falstaff:~$ sar -n DEV 1 1
Linux 2.6.32-5-amd64 (falstaff) 	09/29/2011 	_x86_64_	(8 CPU)

10:16:43 AM     IFACE   rxpck/s   txpck/s    rxkB/s    txkB/s   rxcmp/s   txcmp/s  rxmcst/s
10:16:44 AM        lo      0.00      0.00      0.00      0.00      0.00      0.00      0.00
10:16:44 AM      eth0      1.00      3.00      0.07      0.19      0.00      0.00      0.00
10:16:44 AM      pan0      0.00      0.00      0.00      0.00      0.00      0.00      0.00
10:16:44 AM  vboxnet0      0.00      0.00      0.00      0.00      0.00      0.00      0.00

Average:        IFACE   rxpck/s   txpck/s    rxkB/s    txkB/s   rxcmp/s   txcmp/s  rxmcst/s
Average:           lo      0.00      0.00      0.00      0.00      0.00      0.00      0.00
Average:         eth0      1.00      3.00      0.07      0.19      0.00      0.00      0.00
Average:         pan0      0.00      0.00      0.00      0.00      0.00      0.00      0.00
Average:     vboxnet0      0.00      0.00      0.00      0.00      0.00      0.00      0.00
paulgorman@falstaff:~$ 
</pre>

<div style="font-size:0.8em;">
<p>Other tools from sysstat:</p>
<ul>
<li>sar collects and displays ALL system activities statistics.</li>
<li>sadc stands for “system activity data collector”. This is the sar backend tool that does the data collection.</li>
<li>sa1 stores system activities in binary data file. sa1 depends on sadc for this purpose. sa1 runs from cron.</li>
<li>sa2 creates daily summary of the collected statistics. sa2 runs from cron.</li>
<li>sadf can generate sar report in CSV, XML, and various other formats. Use this to integrate sar data with other tools.</li>
<li>iostat generates CPU, I/O statistics</li>
<li>mpstat displays CPU statistics.</li>
<li>pidstat reports statistics based on the process id (PID)</li>
<li>nfsiostat displays NFS I/O statistics.</li>
<li>cifsiostat generates CIFS statistics.</li>
</ul>
</div>

<p><code>service</code> controls and reports on init scripts.</p>

<pre>paulgorman@falstaff:~$ sudo service ssh status
[sudo] password for paulgorman: 
sshd is running.
paulgorman@falstaff:~$ 
paulgorman@falstaff:~$ sudo service --status-all
 [ + ]  acpid
 [ ? ]  alsa-utils
 [ - ]  anacron
 [ + ]  apache2
 [ + ]  atd
 [ + ]  avahi-daemon
 [ ? ]  binfmt-support
 [ + ]  bluetooth
 [ - ]  bootlogd
...
paulgorman@falstaff:~$ 
paulgorman@falstaff:~$ sudo service cups restart
Restarting Common Unix Printing System: cupsd. </pre>

<h2 id="ssh"><code>ssh -X hostname</code> with X11 redirection</h2>

<p>To work around having to run an X client as root on the remote box:</p>

<pre>~ $ sudo su -
X11 connection rejected because of wrong authentication.
ROOT! ~ # xauth merge ~paulgorman/.Xauthority</pre>

<h2 id="stat"><code>stat</code> for detailed file info</h2>

<p><code>stat <i>filename</i></code> shows all kinds of stuff about the file, including its permissions in octal format, the number of links to it, and its last modification time.</p>

<h2 id="strace"><code>strace</code> tracks process system calls</h2>

<p>If a command reliably produces an error, <code>strace -f -o /tmp/trace.txt mycommand</code> may give a clue if you look for the error string in its output.</p>

<h2 id="sudosu"><code>sudo su</code> don't</h2>

<p>It's generally better to use <code>sudo -s</code>; it preserves more of your regular user environment, and provides better accountability. <code>sudo -i</code> is similar, but runs the login shell specified in the user's passwd entry; it may be useful if you need to sudo into a particular user's login environment. Both are superior to <code>sudo su</code> in most cases.</p>

<pre>21:17 bava ~ $ sudo -s
Temperature: 70.9 F (21.6 C)
Relative Humidity: 58%
Wind: from the NE (040 degrees) at 5 MPH (4 KT)
Sky conditions: clear
ROOT! bava ~ # pwd
/home/paulgorman
ROOT! bava ~ # whoami
root
ROOT! bava ~ # ls ~
august     Dropbox              june          output.xkb    tmp
bin        foo.png              bar.pdf       Pictures      Videos
Books      emacstest.txt        marks         Public        websters1913-29765-8.txt
Desktop    emacstest.txt~       mbox          repo          xboxdrv
Documents  gPodder              Music         server-0.xkb  zine_cover_art_WORKING
Downloads  july                 Notes         Templates
ROOT! bava ~ # exit
exit
21:28 bava ~ $ sudo -i
ROOT! bava ~ # pwd
/root
ROOT! bava ~ # whoami
root
ROOT! bava ~ # ls ~
backup.log  bin  crontab.txt
ROOT! bava ~ # exit
logout
21:29 bava ~ $</pre>

<h2 id="tar"><code>tar</code> archives files</h2>

<p>Create archive with compression:</p>
<p class="code">tar -cvzf newarchive.tgz myfile</p>

<p>If you're archive directory foo, but don't want to include subdirectory bar:</p>
<p class="code">tar -cvzf newarchive.tgz foo --exclude foo/bar</p>

<p>Add foo to existing archive bar.tar:</p>
<p class="code">tar -rvf bar.tar foo</p>

<p>Unpack an archive (extract, verbose, gzip, file):</p>
<p class="code">tar -xvzf myarchive.tgz</p>

<p>List the contents of existing archive (without unpacking):</p>
<p class="code">tar -tzf myarchive.tgz</p>

<h2 id="tac"><code>tac</code> like <code>cat</code>, but last line first</h2>

<p class="code">tac /var/log/apache2/error.log | less</p>

<h2 id="time"><code>time</code> process execution duration</h2>

<p>Show how long a process ran:</p>

<pre>paulgorman@falstaff:~/2011$ time ping 10.0.36.1
PING 10.0.36.1 (10.0.36.1) 56(84) bytes of data.
64 bytes from 10.0.36.1: icmp_req=1 ttl=254 time=19.5 ms
64 bytes from 10.0.36.1: icmp_req=2 ttl=254 time=41.6 ms
64 bytes from 10.0.36.1: icmp_req=63 ttl=254 time=18.0 ms
64 bytes from 10.0.36.1: icmp_req=64 ttl=254 time=33.9 ms
^C
--- 10.0.36.1 ping statistics ---
64 packets transmitted, 64 received, 0% packet loss, time 63035ms
rtt min/avg/max/mdev = 17.904/26.518/55.178/9.103 ms

real	1m3.119s
user	0m0.004s
sys	0m0.008s
paulgorman@falstaff:~/2011$
</pre>

<h2 id="timeout"><code>timeout</code> kills command after duration</h2>

<pre>timeout 2s weather dtw</pre>

<h2 id="top"><code>top</code> monitors the system</h2>

<ul>
<li><code>h</code> for help</li>
<li><code>Z</code> picks colors</li>
<li><code>x</code> highlights the sort column</li>
<li>Left and right carrots change the sort column</li>
<li><code>R</code> reversed to sort order</li>
<li><code>V</code> turns on tree mode to show process parents and children, which is interesting if not always useful</li>
<li><code>i</code> toggles idle tasks</li>
<li>If you hit <code>W</code> immediately before quiting with <code>q</code>, top will save your current settings as its defaults.</li>
</ul>

<p>If I just wanted to watch java processes:</p>

<pre>top -p `pidof java | sed 's/\s/,/g'`</pre>

<p><span class="code">htop</span> is worth a look.</p>

<h2 id="ulimit"><code>ulimit</code> limits a process's memory</h2>

<p>Limit the resources (in this example, memory and virtual memory in kb) used by a process (sets limits on that particular shell):</p>

<pre class="prettyprint">ulimit -m 512000 -v 512000</pre>

Show the limits for the current shell:

<pre class="prettyprint">ulimit -a</pre>

<h2 id="uptime"><code>uptime</code> summarizes system health</h2>

<pre>paulgorman@bava:~$ uptime
 22:22:42 up 4 days,  2:27,  1 user,  load average: 0.05, 0.16, 0.25</pre>

<p>Note the load average number too. This is how many process had to wait to run during the last one, five, and fifteen minutes.</p>

<h2 id="watch"><code>watch</code> something repetatively</h2>

<p>The <code>watch</code> command executes another command at a specified interval (defaulting to every two seconds).<p>

<pre>watch -cdn1 free
watch -cd 'ls -l | grep log'</pre>

<p>(-c interprets ANSI color codes, -d highlights changes, and -n<i>N</i> sets the interval.)</p>

<h2 id="whatis"><code>whatis</code> the function of a program?</h2>

<p>I use <code>whereis</code> all the time, but forget about <code>whatis</code>.</p>

<pre>paulgorman@falstaff:~$ whatis sed
sed (1)              - stream editor for filtering and transforming text
paulgorman@falstaff:~$ 
</pre>

<h2 id="vimdiff"><code>vimdiff</code> is diff in vim</h2>

<p>The utility of diff with the power of vim.</p>
<ul>
<li><span class="code">do</span> Get changes from other window into the current window.</li>
<li><span class="code">dp</span> Put the changes from current window into the other window.</li>
<li><span class="code">]c</span> Jump to the next change.</li>
<li><span class="code">[c</span> Jump to the previous change.</li>
<li><span class="code">Ctrl-W Ctrl-W</span> Switch to the other split window.</li>
</ul>

<h2 id="xargs"><code>xargs</code> combines commands</h2>

<h2 id="xfontsel"><code>xfontsel</code> GUI for selecting an X font</h2>

<h2 id="xlsfonts"><code>xlsfonts</code> lists X fonts</h2>

<h2 id="xrandr"><code>xrandr --output DVI-0 --brightness 0.5</code> reduces brightness</h2>

<p><code>xrandr</code> can also do stuff like rotate the screen.</p>

<h2 id="xrdb"><code>xrdb ~/.Xdefaults</code> reloads X settings</h2>

<h2 id="zcat"><code>zcat, zgrep, zless</code> work on compressed files</h2>

<h2>Creating a swapfile</h2>

<pre>$ sudo -s
# fallocate -l 8G /swapfile
# chmod 600 /swapfile
# mkswap /swapfile
# swapon /swapfile
</pre>

<p>Add to <code>/etc/fstab</code>:</p>

<pre>/swapfile none swap defaults 0 0</pre>

<p><code>free -m </code> should show your swap.</p>

<h2 id="commandlinefu">Tips from <a href="http://www.commandlinefu.com/commands/browse/sort-by-votes">commandlinefu.com</a></h2>

<dl>
<dt>command !$</dt>
<dd>Substitute last argument from last command</dd>
<dt>sudo !!</dt>
<dd>Re-run last command through sudo</dd>
<dt>python -m SimpleHTTPServer</dt>
<dd>Server current directory at <a href="http://localhost:8000/">http://localhost:8000/</a></dd>
<dt>:w !sudo tee %</dt>
<dd>In vim, save if you forgot to start editing with elevated privileges</dd>
<dt>^foo^bar</dt>
<dd>Re-run last command, but replace foo with bar</dd>
<dt>mtr example.com</dt>
<dd>mtr is like ping with traceroute</dd>
<dt>[SPACE] command</dt>
<dd>Commands prepended with a space are not saved in your command history</dd>
<dt>mount | column -t</dt>
<dd>Display mount in nice columns. Note: <code>findmnt</code> is nice, if available.</dd>
<dt>echo "ls -l" | at midnight</dt>
<dd>Schedule a one-off command</dd>
<dt>sudo netstat -tlnp</dt>
<dd>List listening ports, with controlling process ID</dd>
<dt>mount -t tmpfs tmpfs /mnt -o size=1024m</dt>
<dd>Make a RAM disk</dd>
<dt>man ascii</dt>
<dd>Show ASCII character table</dd>
<dt>(cd /tmp && ls)</dt>
<dd>Jump to directory, run command, and return to current directory</dd>
<dt>net rpc shutdown -I ipAddressOfWindowsPC -U username%password</dt>
<dd>Shut down a windows box. 'net rpc' to see other commands.</dd>
<dt>python -m smtpd -n -c DebuggingServer localhost:1025</dt>
<dd>Start simple email server that shows messages on STDOUT</dd>
<dt>rm !(*.foo|*.bar|*.baz)</dt>
<dd>Delete all files that don't match the pattern. The extglob option must be enabled (<code>shopt -s extglob</code>) for negative wildcards to work when pattern matching. And you don't want any spaces between the pipes. It would be a fine idea to test the above with <code>ls !(*.foo|*.bar|*.baz)</code></dd>
<dt>man hier</dt>
<dd>Describes the filesystem hierarchy</dd>
<dt>mount /path/to/file.iso /mnt/cdrom -oloop</dt>
<dd>Mount an ISO image</dd>
<dt>watch -n 1 mysqladmin --user=<user> --password=<password> processlist</dt>
<dd>Monitor queries being run by MySQL</dd>
<dt>rename 's/-/_/' ./*</dt>
<dd>Rename files in current directory according to pattern</dd>
<dt>chmod --reference file1 file2</dt>
<dd>Makes file2 permissions the same as file1</dd>
<dt>tar -tf <file.tar.gz> | xargs rm -r</dt>
<dd>Remove files also found in archive</dd>
</dl>

<?php include('../FOOTER.php'); ?>
