Tuesday, February 28, 2012

Recently edited file

Very often I've got the situation when I edit some files remotly in the directory where there are many files. To see recently edited files I'm doing:

ls -ltr

They appear as the last ones on the list. The second trick is:

ls -lt | head

Then they appear as first

Monday, March 29, 2010

Upper case, lower case

To change the letters in the string to upper or lower case we can use tr command:

$ echo "Hello world" | tr a-z A-Z
HELLO WORLD

$ echo "Hello world" | tr A-Z a-z
hello world

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).