Tuesday, August 25, 2009

Generating strong passwords

Sometimes we need to make up the password. Normally the password should be easy to remember but hard to guess. I show you how to generate strong passwords that are not easy to remember :). We'll use for this purpose /dev/urandom device, which generates random data. Suppose we want to generate 10 passwords, each 8 characters long. To achieve this we type:

$ cat /dev/urandom | tr -dc 'a-zA-Z0-9' | fold -8 | head -10
lUrBzyuv
RuDq498i
mOzJMs4Y
s0lUpMZd
LmwuN6Sy
fubIOO6g
KKynsPs4
dFH4W0Ux
3JAbwJQD
PofhRhTS

The fold command breaks each line after n characters, so defines the length of the password, the head command shows first n lines so defines the number of passwords. The tr deletes (-d) all but specified (-c) characters. The idea comes from here.

Sunday, August 23, 2009

Where to put beer?

Our desk is full of papers. Where to put the cold beer? Type eject and eject -t after drinking :).

Saturday, August 22, 2009

Shrinking files

What can be done to make the executable files smaller? When we get the normal ELF file we can first strip it and then compress it in a way it will decompress during each run. We've got the binary file hello which prints hello world:

$ ls -l hello
-rwxr-xr-x 1 berdzi users 6230 2009-08-22 18:04 hello

It's 6230 bytes big. We will strip it with strip command. Strip removes all debugging symbols from the binary. They are very helpful when we want to debug the file with i.e. gdb, whereas they not necessary in normal use. Stripping them saves a lot of space:

$ strip hello
$ ls -l hello
-rwxr-xr-x 1 berdzi users 2748 2009-08-22 18:09 hello

Now the file has only 2748 bytes. To make it smaller we can use gzexe. It will compress the file in place - it will be decompressed while each execution. This saves extra bytes increasing runtime. Let's see:

$ gzexe ./hello
./hello: 53.9%
$ ls -l hello
-rwxr-xr-x 1 berdzi users 2092 2009-08-22 18:12 hello

This will create two files: hello and hello~ where the second one is the original file and if we sure the compressed version is ok, we can remove the source. We can always decompress the compressed file with -d switch.
Let's compare the execution time of compressed file and the original:

$ time ./hello~
Hello world

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

$ time ./hello
Hello world

real 0m0.066s
user 0m0.016s
sys 0m0.040s

This particular binary executes 16 times slower (in this case in almost unnoticable).

The lines

If you want to check how many lines the file has, use wc -l command:

$ cat /etc/shells | wc -l
6

To see the content of the file along with line numbers use cat -n:

$ cat -n /etc/shells
1 /bin/bash
2 /bin/tcsh
3 /bin/csh
4 /bin/ash
5 /bin/ksh
6 /bin/zsh

To get the particular line from the file use head and tail. To see the 4th line from /etc/shells, type:

$ cat /etc/shells | head -4 | tail -1
/bin/ash

Wednesday, August 19, 2009

(S)hell fire :)




Although there's summer, you may sometimes feel cold. What can you do to warm up? Run aafire and admire beautiful ASCII art fire in the console.

File integrity

What happens if someone hack into our system and modify the binaries. At first they look the same and behave normally but underneath they can do some bad things. The simple way to control things is to create md5 hashes of every binary after system installation and keep it safe somewhere on CD or other external drive. Let's do checksums file (as root):

# find / -type f -perm /111 -print | xargs md5sum > checksums

We find all executable files and then create md5 hashes for them in the checksums file. Now if we're suspect any problems we can always compare the hashes in the system with the hashes in the checksums file, manually:

# md5sum /bin/ls
a7d0f168866236756bafed5357e7e039 /bin/ls
# grep /bin/ls checksums
a7d0f168866236756bafed5357e7e039 /bin/ls

or with this script:

#!/bin/bash
if [ "$(grep "${1}" checksums| cut -f1 -d' ')" == "$(md5sum "${1}" | cut -f1 -d ' ')" ]; then echo "OK
"; else echo "FAILED"; fi

Run this way:

$ ./sumcmp.sh /bin/ls
OK

Monday, August 17, 2009

Searching wikipedia with dig (or host)

Dig is the tool to get information from DNS. As I read here, suprisingly, it can also be used to query wikipedia (english) from the command line. The syntax is:

dig +short txt <keyword>.wp.dg.cx

Here's the query for 'linux' term:

$ dig +short txt linux.wp.dg.cx
"Linux (commonly pronounced in English\; variants exist) is a generic term referring to Unix-like computer operating systems based on the Linux kernel. Their development is one of the most prominent examples of free and open source software collaboration\; t" "ypically all the underlying source code can be used, freely modified, and redistributed by anyone under the terms of the... http://a.vu/w:Linux"

We can simplify things, writing small script wiki.sh:

#!/bin/bash
dig +short txt "${1}.wp.dg.cx"

We can make the output prettier with this script (found here):

#!/bin/sh
COLUMNS=`tput cols`
dig +short txt "${1}".wp.dg.cx | sed -e 's/" "//g' -e 's/^"//g' -e 's/"$//g' -e 's/ http:/\n\nhttp:/' | fmt -w $COLUMNS

We can also use host command to do the same:

$ host -t txt linux.wp.dg.cx

Sunday, August 16, 2009

Mass files renaming

Ever needed to change the extensions of the multiply files from one to the other? If you i.e. want to change all the files ended by .php3 to .php you could use the following command:

$ ls -d *.php3 | sed 's/\(.*\).php3$/mv "&" "\1.php"/' | sh

What we use here is sed replacing the part of the string (thanks to regular expression) with mv command and piping it to sh to which executes it. The detailed explanation of this process can be found here.

Saturday, August 15, 2009

Detect and monitor CPU cores




If you have several CPUs on your system or multicore CPU, you can detect number of cores with this simple command:

$ cat /proc/cpuinfo | grep processor | wc -l
2

To determine the usage of each core you can use standard top command. By default it shows only summary usage, however you can toggle between summary and per core usage with the '1' key after starting the top.

Thursday, August 13, 2009

Bc as cpu benchmark

My previous post (http://tuxshell.blogspot.com/2009/08/shell-as-calculator.html) was about making math calculation in shell. I showed how to calculate PI number up to n digits. I thought, together with time command it can be used as CPU benchmark. This kind of calculation is cpu intensive and takes some time. Let's calculate up to 5000 digits after period. We measure the time of doing it with time command and look for real value:

$ time echo "scale=5000; a(1)*4" | bc -l

My computer did it within 37.194s. How about yours?

Shell as calculator

The standard Linux shell (bash) can serve us as calculator. The bash itself is only limited to integer numbers, however there is also bc command. Bc stands for binary calculator and allows to do numbers of calculations using floating point math as well as conversions. First let's look at standard shell possibilites:

$ echo $((2+2)
4

$ echo $((2+2*(3*4)))
26

modulo operator

$ echo $(10 % 3)
3

power operator

$ echo $((2**3))
8

bitwise operators (bitshifting):

$ echo $((2<<3))
16

$ echo $((10>>1))
5

There is one trick as to bitshifts. Shifting the number by 1 bit left means multiplying by 2, shifting it by 1 bit right means dividing by 2. Bitshifting is faster than multiplying but it has the meaning in assembly language programs, not the linux shell :).
Of course we can assign the result to variabe to use it later:

$ VALUE=$((100/4))
$ echo $VALUE
25

Bash is good only for integer calculations. Bc is much more powerfull tool. To calculate anything with bc we usually pipe the output of echo
to bc:

$ echo 2+2 | bc
4

echo 2.56*2.66 | bc
6.80

We can set the precision with scale parameter:

$ echo "scale=4; 2.56*2.66" |bc
6.8096

To calculate the power we use ^ (in bash it means bitwise negation):

$ echo 2^10 | bc
1024

Bc as base converter :)

Apart from simple calculation bc can be used as base converter. To convert among decimal, hex and binary numbers we use ibase (input base) and obase (output base) parameters. The default base is 10. Let's see some examples:

$ echo "obase=16; 255" | bc
FF

$ echo "obase=2; 255" | bc
11111111

However to convert from binary to decimal we have to set obase as hex number (10 is A). The reason for that is 10 in binary is 2, so the obase would be 2, becase ibase is set to 2 and interprets decimal 10 decimal as binary 2:

$ echo "ibase=2; obase=A; 1000" | bc
8

When the switch -l is used the bc is preloaded with math library and default scale is set to 20. This allows to do some more advanced calculations such trigonomery math. The argument is given as radians. To calculate sine of 30 radians type:

$ echo "s(30) | bc -l"

We can calculate PI very easy up to many digits after period (set with scale):

$ PI=$(echo "scale=10; 4*a(1)" | bc -l)
$ echo $PI
3.1415926532

Now we can convert radians to degrees with the formula:

degrees=radians*PI/180

Shell calculator

It's worth to write short script which can be used as calculator.

#!/bin/bash
echo "scale=4; $1" | bc

Save it as calc and give execution rights:

$ chmod u+x calc

From the root shell copy it somewhere i.e. to /usr/bin to make it visible system-wide.

$ su -c "cp calc /usr/bin/"

Use it this way:

$ calc 6.5*6.5
42.25


Wednesday, August 12, 2009

Tee time

Have you ever needed to write something to file and watch it on the screen at the same time? If so, tee command is your friend. You can pipe anything to tee and it will show it on the screen as well as write to the file (or even better - multiple files) at the same time. The useful switch -a means append mode - the data are added at the end of file instead of erasing it. Let's see and write current time and date to three files - date1, date2 and date3:

$ date | tee -a date1 date2 date3

Grabbing links with lynx

Lynx is terminal web browser. We can use it however to grab all the links from given webpage. Let's try to grab all the links from linuxgazette.net:

$ lynx --dump --listonly linuxgazette.net

References

1. http://linuxgazette.net/lg.rss
2. http://linuxgazette.net/lg.rdf
3. http://linuxgazette.net/
4. http://linuxgazette.net/index.html
5. http://linuxgazette.net/
6. http://linuxgazette.net/faq/index.html
7. http://linuxgazette.net/lg_index.html
8. http://linuxgazette.net/mirrors.html
9. http://linuxgazette.net/mirrors.html
10. http://linuxgazette.net/search.html
11. http://linuxgazette.net/archives.html
12. http://linuxgazette.net/authors/index.html
13. http://lists.linuxgazette.net/mailman/listinfo/
14. http://linuxgazette.net/jobs.html
15. http://linuxgazette.net/contact.html
16. http://linuxgazette.net/
17. http://linuxgazette.net/165/index.html
18. http://linuxgazette.net/164/index.html
19. http://linuxgazette.net/163/index.html
20. http://linuxgazette.net/162/index.html
21. http://linuxgazette.net/archives.html
22. http://linuxgazette.net/current/TWDT165.pdb
23. http://linuxgazette.net/cgi-bin/pdb.cgi
24. http://linuxgazette.net/ftpfiles/lg-165.tar.gz
25. http://linuxgazette.net/ftpfiles/lg-base.tar.gz
26. http://linuxgazette.net/ftpfiles/lg-base-new.tar.gz
27. http://linuxgazette.net/ftpfiles/
28. http://linuxgazette.net/mirrors.html
29. http://linuxgazette.net/ftpfiles/README
30. http://linuxgazette.net/faq/general.html#ftp
31. http://linuxgazette.net/ftpfiles.txt
32. http://linuxgazette.net/mirrors.html#Arabic
33. http://linuxgazette.net/mirrors.html#French
34. http://linuxgazette.net/mirrors.html#Indonesian
35. http://linuxgazette.net/mirrors.html#Portuguese
36. http://linuxgazette.net/mirrors.html#Russian
37. http://linuxgazette.net/mirrors.html#Spanish
38. http://linuxgazette.net/mirrors.html#Thai
39. http://linuxgazette.net/lg.rss
40. http://linuxgazette.net/lg.rdf
41. http://linuxgazette.net/current/
42. http://fullhartsoftware.com/
43. http://linuxgazette.net/faq/author.html
44. http://linuxgazette.net/jobs.html
45. http://linuxgazette.net/faq/ask-the-gang.html
46. http://linuxgazette.net/faq/members-faq.html
47. http://www.tldp.org/
48. http://linuxgazette.net/
49. http://linuxgazette.net/copying.html
50. http://lwn.net/Articles/63383/
51. http://www.graphics-muse.com/
52. http://www.isc.tamu.edu/~lewing/linux/

The --dump switch dumps all content of the webpage, the --listonly switch make only links to show. This option also shows "hidden links" - links pointed by images and buttons. If we want only the pure list of the links we can additionally use the cut and tr commands to cut unnecessary characters. We will sort (with sort) them by name and eliminate repetitive ones (with uniq). Note: you always have to sort text before piping it to uniq command:

$ lynx --dump --listonly linuxgazette.net | grep http | cut -f2- -d'.' | tr -d ' ' | sort | uniq

http://fullhartsoftware.com/
http://linuxgazette.net/
http://linuxgazette.net/162/index.html
http://linuxgazette.net/163/index.html
http://linuxgazette.net/164/index.html
http://linuxgazette.net/165/index.html
http://linuxgazette.net/archives.html
http://linuxgazette.net/authors/index.html
http://linuxgazette.net/cgi-bin/pdb.cgi
http://linuxgazette.net/contact.html
http://linuxgazette.net/copying.html
http://linuxgazette.net/current/
http://linuxgazette.net/current/TWDT165.pdb
http://linuxgazette.net/faq/ask-the-gang.html
http://linuxgazette.net/faq/author.html
http://linuxgazette.net/faq/general.html#ftp
http://linuxgazette.net/faq/index.html
http://linuxgazette.net/faq/members-faq.html
http://linuxgazette.net/ftpfiles.txt
http://linuxgazette.net/ftpfiles/
http://linuxgazette.net/ftpfiles/README
http://linuxgazette.net/ftpfiles/lg-165.tar.gz
http://linuxgazette.net/ftpfiles/lg-base-new.tar.gz
http://linuxgazette.net/ftpfiles/lg-base.tar.gz
http://linuxgazette.net/index.html
http://linuxgazette.net/jobs.html
http://linuxgazette.net/lg.rdf
http://linuxgazette.net/lg.rss
http://linuxgazette.net/lg_index.html
http://linuxgazette.net/mirrors.html
http://linuxgazette.net/mirrors.html#Arabic
http://linuxgazette.net/mirrors.html#French
http://linuxgazette.net/mirrors.html#Indonesian
http://linuxgazette.net/mirrors.html#Portuguese
http://linuxgazette.net/mirrors.html#Russian
http://linuxgazette.net/mirrors.html#Spanish
http://linuxgazette.net/mirrors.html#Thai
http://linuxgazette.net/search.html
http://lists.linuxgazette.net/mailman/listinfo/
http://lwn.net/Articles/63383/
http://www.graphics-muse.com/
http://www.isc.tamu.edu/~lewing/linux/
http://www.tldp.org/

Monday, August 10, 2009

Figlet - the better banner



Figlet is small tool a bit similar to described earlier (http://tuxshell.blogspot.com/2009/08/banner.html) banner command. It allows to create pretty nice ASCII art letters out of ordinary text. You can download it from http://www.figlet.org.

Installation

$ wget ftp://ftp.figlet.org/pub/figlet/program/unix/figlet222.tar.gz
$ tar zxvf figlet222.tar.gz
$ cd filglet222

We can set the default installation directory as well as the default fonts directory by editing the Makefile. I had to comment the following line:

# DEFAULTFONTDIR = fonts

to avoid installation error. Let's build and install:

$ make
$ su -c 'make install'

Usage

When no arguments are present, the program waits for input . Type what you want and see the result. It will render the text given as the first argument:

$ figlet hello world
 _          _ _                            _     _
| |__ ___| | | ___ __ _____ _ __| | __| |
| '_ \ / _ \ | |/ _ \ \ \ /\ / / _ \| '__| |/ _` |
| | | | __/ | | (_) | \ V V / (_) | | | | (_| |
|_| |_|\___|_|_|\___/ \_/\_/ \___/|_| |_|\__,_|

We can see all available fonts by listing *.flf files /usr/local/share/figlet/:

$ ls *.flf

banner.flf bubble.flf lean.flf script.flf small.flf smslant.flf
big.flf digital.flf mini.flf shadow.flf smscript.flf standard.flf
block.flf ivrit.flf mnemonic.flf slant.flf smshadow.flf term.flf


The -f switch selects the font:

$ figlet -f shadow shadow


| |
__| __ \ _` | _` | _ \\ \ \ /
\__ \ | | | ( | ( | ( |\ \ \ /
____/_| |_|\__,_|\__,_|\___/ \_/\_/

We can also set text justification with -c , -l, -r and -x switches:
$ figlet -l left

_ __ _
| | ___ / _| |_
| |/ _ \ |_| __|
| | __/ _| |_
|_|\___|_| \__|

$ figlet -c center
_
___ ___ _ __ | |_ ___ _ __
/ __/ _ \ '_ \| __/ _ \ '__|
| (_| __/ | | | || __/ |
\___\___|_| |_|\__\___|_|

$ figlet -r right
_ _ _
_ __(_) __ _| |__ | |_
| '__| |/ _` | '_ \| __|
| | | | (_| | | | | |_
|_| |_|\__, |_| |_|\__|
|___/

When sending to figlet the output from other file it's good to use -p switch. It switches the figlet into paragraph mode, which eliminates unnecessary line breaks when piping a multi-line file:

$ figlet -p < /etc/motd

The -k, -s and -S switches control the kerning of the letters. We can achieve the mirror effect by using the -R option which prints the text from right to left:

$ figlet -kR hello

_ _ _
___ | || | ___ | |__
/ _ \ | || | / _ \| '_ \
| (_) || || || __/| | | |
\___/ |_||_| \___||_| |_|

Combining figlet with tr command ale proper fonts leads us to some more interesting effects:

$ figlet -f lean hello | tr ' _/' '/  '
/////////////////////////////////////////
//// ////////////////// // ///////////
/// ////// //// // //// ////
// //// // // // // //// ///
/ //// // //////// // // //// ////
//// //// // // //// ///////
/////////////////////////////////////////
/////////////////////////////////////////

Of course we can pipe output of any command to the figlet. The imagination is the limit.

$ date | figlet
_____ _ _ _
|_ _| _ ___ / \ _ _ __ _ / / |
| || | | |/ _ \ / _ \| | | |/ _` | | | |
| || |_| | __/ / ___ \ |_| | (_| | | | |
|_| \__,_|\___| /_/ \_\__,_|\__, | |_|_|
|___/
___ ___ _________ _ ___ ____ _____ ____ _____
/ _ \ / _ \ _|___ / ___|_/ |/ _ \ / ___| ____/ ___|_ _|
| | | | (_) (_) |_ \___ (_) | | | | | | | _| \___ \ | |
| |_| |\__, |_ ___) |__) || | |_| | | |___| |___ ___) || |
\___/ /_/(_)____/____(_)_|\___/ \____|_____|____/ |_|

____ ___ ___ ___
|___ \ / _ \ / _ \ / _ \
__) | | | | | | | (_) |
/ __/| |_| | |_| |\__, |
|_____|\___/ \___/ /_/
We can run the full screen clock based on figlet:

$ watch -n1 "date +%H:%M:%S | figlet -k -c"

ski jump



Yet another funny ASCII game. This time asciijump - ski jumps in ascii. Visit the homepage of the project at: http://asciijump.prv.pl/.

Quick installation:

$ wget http://otak.k-k.pl/asciijump/tgz/asciijump-1.0.2beta.tar.gz
$ tar zxvf asciijump-1.0.2beta.tar.gz
$ cd asciijump-1.0.2beta
$ ./configure
$ make

Run asciijump to play.

0verkill - the console deathmatch game


Source: http://artax.karlin.mff.cuni.cz/~brain/0verkill/

You like play deathmatch games? Now you can use console for this. Some guys made a brilliant ASCII only 2D deathmatch game called 0verkill. Visit it's homepage at http://artax.karlin.mff.cuni.cz/~brain/0verkill/.

Installation:

Download the source

$ wget http://artax.karlin.mff.cuni.cz/~brain/0verkill/release/0verkill.tgz

Unpack

$ tar zxvf 0verkill.tgz

and compile:

$ cd 0verkill
$ ./configure
$ make

Now you type 0verkill and play. You can also run a server with:

$ server <port number>

Sunday, August 9, 2009

Secure files deletion

Have you ever wondered what happens when you delete a file? If you think it disappears you're wrong. The standard linux filesystems (ext2/ext3) only zero the pointers to the data blocks, however the data still remain and can be retrieved with some effort. There are some ways however to securely delete the file. The secure deletion usually means multiple overwriting the file with random patterns and zeros in the end. The last phase is made to hide the file shreding. After these steps we can issue rm command to remove the file. To achieve this we can use dd command:

$ dd if=/dev/urandom of=file2delete count=$(ls -s file2delete|cut -f1 -d' ') bs=1K
$ dd if=/dev/zero of=file2delete count=$(ls -s file2delete|cut -f1 -d' ') bs=1K
$ rm -f
file2delete

There is also the shred command which does the same (in a better way :). By default it overwrites the data with random patterns 25 times (the -n switch specifies the exact number). The -z switch causes zeroing the file in the end. The -u switch truncates and removes the file permanently. Removing is optional, because shred can be used to secure deletion of the whole hard disk or partition. Let's delete the file2delete file:

$ shred -z -u -v file2delete
shred: file2delete: pass 1/26 (random)...
shred: file2delete: pass 2/26 (cccccc)...
shred: file2delete: pass 3/26 (666666)...
shred: file2delete: pass 4/26 (aaaaaa)...
shred: file2delete: pass 5/26 (db6db6)...
shred: file2delete: pass 6/26 (249249)...

shred: file2delete: pass 7/26 (222222)...
shred: file2delete: pass 8/26 (924924)...

shred: file2delete: pass 9/26 (ffffff)...

shred: file2delete: pass 10/26 (777777)...
shred: file2delete: pass 11/26 (111111)...
shred: file2delete: pass 12/26 (b6db6d)...
shred: file2delete: pass 13/26 (random)...
shred: file2delete: pass 14/26 (bbbbbb)...

shred: file2delete: pass 15/26 (6db6db)...
shred: file2delete: pass 16/26 (444444)...
shred: file2delete: pass 17/26 (dddddd)...
shred: file2delete: pass 18/26 (000000)...
shred: file2delete: pass 19/26 (888888)...
shred: file2delete: pass 20/26 (333333)...
shred: file2delete: pass 21/26 (492492)...
shred: file2delete: pass 22/26 (999999)...
shred: file2delete: pass 23/26 (eeeeee)...
shred: file2delete: pass 24/26 (555555)...
shred: file2delete: pass 25/26 (random)...
shred: file2delete: pass 26/26 (000000)...

shred: file2delete: removing
shred: file2delete: renamed to 00000000000

shred: 00000000000: renamed to 0000000000
shred: 0000000000: renamed to 000000000
shred: 000000000: renamed to 00000000
shred: 00000000: renamed to 0000000

shred: 0000000: renamed to 000000
shred: 000000: renamed to 00000
shred: 00000: renamed to 0000
shred: 0000: renamed to 000
shred: 000: renamed to 00
shred: 00: renamed to 0
shred: file2delete: removed

Shred has one disadvantage. It doesn't have recursive mode, so we can't delete the whole directory at once. We can however overcome this with little shell script:

#!/bin/bash
if [ -z "$1" ]; then
echo "usage: $0 <file> or <directory>"
exit
fi

if [ -d "$1" ]; then
find "$1" -type f -exec shred -u '{}' \;
rm -rf "$1"
elif [ -f "$1" ]; then
shred -u "$1"
fi

There are also other tools for this purpose. One of them is srm, which can be found at http://srm.sourceforge.net. It has recursive deletion and several erase modes (one of them is compatible with OpenBSD rm command).

Saturday, August 8, 2009

ASCII Star Wars


Sometimes you can find on the web very funny and creative stuff. Someone did the whole episode of star wars in ASCII mode. You can watch it with telnet command. Type:

$ telnet towel.blinkenlights.nl

Nice watching :)

Thursday, August 6, 2009

Banner

Ever heard of ASCII art? It's kind of art where images and signs are composed of many characters. They look good if you look at them from further distance or print it on paper and hang on the wall. Banner is little tool which creates such images. Type what you want to see as huge sign as the first argument and see the result. Here's the output of command (the -w switch defines the width of the banner):

$ banner -w 40 abc




Printing manuals

The command man is very useful, however what if we want to move the content to the paper. The col is our friend here. To change every manual page into normal printable text file we type:

$ man ls | col -b > manls.txt

What happened here? The command col filters reverse line feeds and replaces white-space characters with tabs. We can now print manls.txt or go one step further. Using groff formating system and ps2pdf, which converts postscript files into pdf we can convert any manual to PDF file:

$ man ls | col -b | groff -Tps -fC | ps2pdf - > manls.pdf

This command passes our txt formatted manual into groff which converts it to postscript (-Tps), additionally we choose the courier font family as default (-fC, -fT is Times New Roman, -fA is Arial), because it looks better. In the end ps output is passed to ps2pdf which converts it to pdf and everything is put into manls.pdf file.
We can of course convert other files this way. Man is only an example.

Wednesday, August 5, 2009

Fortune cookies

On some systems (i.e. Slackware) after logging to the system you can see funny sentences. The fortune command is responsible for that. This small utility displays random aphorisms from its database. Type fortune and you see something like that:

$ fortune
You can only live once but if you do it right, once is enough.

Run it several times and see various aphorisms. When run with -o switch it can show you only the potentially offensive ones. The -s switch selects only the short aphorisms and the -l switch selects the long ones.

All the epigrams are placed in several files (without .dat extension) in /usr/share/games/fortunes/. These files are normal text files. The sentences are separated with '%' sign, so you can easily look through them. If we want the command to get sentences only from specific file, we give name of this file as the first argument:

$ fortune startrek

will give us only quotes from the star trek.

Creating our own sentences files

We can easily create our own data file. First we have to create normal text file like this:

My first sentence
%
My second sentence
%
Good bye

Let's call it ourfortunes. Now we have to to create the .dat file - this is the binary file, which allows random access to all the sentences from our text file. We do this with strfile command:

$ strfile ourfortunes

The file ourfortunes.dat will be created. Now we have to move both files to /usr/share/games/fortunes. We can use our own fortunes:

$ fortune ourfortunes
Good bye

One man and friends

Linux has a lot of commands and that's why it can look scary at the beginning. What you really need is to know how and where to look for help. There are several commands that can help us:

man,
info,
pinfo,
whatis ( = man -f),
apropos ( = man -k),

man gives you the description of every command (unless it doesn't have the manual). If you want to find out about man, type:

$ man man

Sometimes there are more than one manual for specific command. If we want to get to the exact manual page we type:

man <man page> <command>

getopt is good example of this. The man page 1 describes the C language function , whereas page 3 describes bash shell command.

info is an interactive help system, where you can navigate through. You can type just info
to get the full manual or type info info to get the information on info command. There is one more command called pinfo, which gives you the same as info, however it's more colourful and based on lynx-like (console webbrowser) menu. You can cycle through info pages with n and p. Quit with q.

whatis is really useful and allows us to get short description of linux commands. This is what we can find out about ls command:

$ whatis ls

ls (1) - list directory contents
ls (1p) - list directory contents

By the way the same effect we can be achievied using man -f.

Another useful command is apropos. It looks for text strings inside whatis database. Typing:

$ apropos man

gives us all the commands and their descriptions containing 'man' string somewhere inside. The same effect gives us man -k command.

To see what the commands in /bin/ directory do we type:

$ whatis $(ls /bin) | grep -v aprioprate

The beginning

First time when I installed Linux about 9 years ago, I decided to use text mode only, because I wanted to taste a bit of *nix magic. When console arrived I logged in and was a bit confused. So I start typing commands such as: help, info, ?, etc... Nothing happened. What I did was to renistall all the system and turn on graphic mode by default. It was some version of Mandrake.
Some time has passed and now I really like black magic of the linux console and I find it very, very useful. Despite many modern Linux distros around I still use Slackware, because I think it's good old-style Linux without any fireworks I don't need. In this blog I'll try to show you that Linux is not scary and how you can use it's magic :).