summaryrefslogtreecommitdiffstats
path: root/software/unix-tricks.page
blob: af7a1efea375a8e592961b7bcb32af15b1c2771a (plain)
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
---
format: rst
toc: no
...

=====================================
UNIX Tricks
=====================================

See also `freebsd </software/freebsd-tricks>`_ and 
`debian </software/debian>`_ tricks.

Fork many processes with `xargs`
-----------------------------------
To fork off 10 instances of sleep with incremented lengths, 5 at a time::

    $ seq 10 20 | xargs -n 1 -P 5 sleep

Remapping a Keyboard Key to Meta
-----------------------------------
I like having a Meta key (aka "Windows key") for use with system keyboard
shortcuts (change desktop etc), but some laptop keyboards don't have an extra
button. To remap one of the Alt keys, you need to know it's code: on FreeBSD
6.3 it was: ``Alt_R keysum 0xffea keycode 113``. To get the right mapping under
X11, read the directions in ``/usr/X11R6/lib/X11/xkb/README``. To get the right
mapping under the console, edit ``/usr/share/syscons/keymaps/us.iso.kbd`` and
change 'ralt' to 'meta'.

Figlet
---------------------
figlet is the horizontal banner.

thttpd
---------------

thttpd is a super small/light http web server with almost no features. To 
server up the working directory as cgi to local host, use::

    $ thttpd -p 8080 -h localhost -D

To allow CGI for PHP scripts:

    $ thttpd -p 8080 -h localhost -D -c \*.php

Simple "undelete"
--------------------
This oneliner is useful for recovering recently deleted files from unix
filesystems, assuming you know a reasonably unique string in that file. Change
the -A and -B options (lines of context before and after the unique string) as
appropriate, and go through the output file to clean up the text.

    $ sudo grep "bookmark_bar" --binary-files=text -B 10 -A 1000 /dev/sda5 | tee undeleted_content.txt

Misc
----
``tput`` is useful for controlling the terminal cursor position, font style,
etc. 

OpenSSL Primes
-----------------
See if a number is prime with ``openssl prime 2011``.

SSH Tunnel
------------
Try `autossh <http://www.harding.motd.ca/autossh/>`_ to keep tunnels open.

printf
------

The `printf` command is much more powerful than `echo`.

stderr in Red
---------------

Via `stack overflow <http://serverfault.com/questions/59262/bash-print-stderr-in-red-color/59299#59299>`_:

    $ command 2> >(while read line; do echo -e "\e[01;31m$line\e[0m"; done)

Mount an .img file as loopback device
-----------------------------------------

Either:

    mount -o loop distro.img /mnt

Or:

    losetup /dev/loop0 distro.img
    mount /dev/loop0 /mnt

Search and Replace String in Multiple Files
---------------------------------------------
Using sed:

    sed -i 's/OldString/NewString/g' *.txt

File-based Network Access
---------------------------------------------

You can directly access network sockets as if they were files from bash using
the virtual devices ``/dev/tcp/HOSTNAME/PORT`` and ``/dev/udp/HOSTNAME/PORT``.