ru Русский

Reticularium

NETWORKS PLACE

Cause of many mistakes. Applies anywhere, I am going to provide a shell examples.

So this is what can twist your brain:

1
2
3
4
$ date +"%s"
1321920962
$ date -d "1970/01/01 +1321920962 sec" +"%s"
1321917362

Unixtime (POSIX time) is a number of seconds passed since the beginning of year 1970. So in the example above I get this number with date +"%s", then add this number to the epoch (1970/01/01) and get a different number!

The reason of this is very simple:

1
2
3
4
$ date -d "1970/01/01" +"%s"
-3600
$ date -u -d "1970/01/01" +"%s"
0

Unixtime is always UTC

It is a well-known fact, but it’s so easy to miss:

1
2
3
4
5
6
$ date ; date -u
Tue Nov 22 01:31:42 CET 2011
Tue Nov 22 00:31:42 UTC 2011
$ date +"%s" ; date -u +"%s"
1321921903
1321921903

That is, for example, you might have local and UTC datetime variables defined in your code, perfectly distinguishable. And then at some point you decide to just format it with +"%s" to unixtime, and they become equal :)

BUT:

1
2
3
4
5
$ dt='Tue Nov 22 01:31:42'
$ date -d "$dt" +"%s"
1321921902
$ date -u -d "$dt" +"%s"
1321925502

Note that the second number is bigger. Seems wrong because my local CET time is one hour ahead. But it is absolutely correct. It is bigger because in the first case to be converted to unixtime this variable must be converted to UTC first (minus 1 hour), so it becomes ‘Tue Nov 22 00:31:42 UTC 2011’ while in the second case it is considered as ‘Tue Nov 22 01:31:42 UTC 2011’. In other words, $dt is a string, not a date. This is why the -u option converts it to date like this:

1
2
3
4
$ date -d "$dt"
Tue Nov 22 01:31:42 CET 2011
$ date -u -d "$dt"
Tue Nov 22 01:31:42 UTC 2011

String can’t be converted to unixtime, so it is converted to date first, then the result is converted to UTC if needed and this result is then converted to unixtime.

M-Script project has been moved to its new home: http://m-script.org

It’s been under heavy development lately: old scripts have been renewed and enhanced, folders reorganized, files moved to proper locations, default monitors made more informative.

It’s also been published on Github

Useful to sync gems across servers. Converts gem list --local output to a bunch of gem install commands. Rails related gems are removed from the initial list.


gem list --local | grep -v '\*' | grep -v ^$ | grep -v ^action | grep -v ^active | grep -v ^rails | grep -v ^rack | grep -v ^rake | sed 's|(|-v=|' | sed 's|)||' | sed 's|^\(.*\),|\1\n\1|' | sed 's|=.*[[:space:]]|=|' | sed 's|^|gem install |'

In fact, it isn’t. Let me explain.

When you see the next time a complaint like this, note what exactly that complaint is related to. I mean operating system. I am sure you’ll find out that it’s some sort of binary distributives like Centos or Ubuntu. But they are not exactly Linux! They are what they are: Linux-based operating systems distributable in binary form. You can’t refer to them as to Linux, because 1) Linux is a kernel, not an operation system, and 2) being a monolithic kernel, it’s supposed to be carefully configured and compiled natively for each system. And operating system (GNU), should be compiled against this kernel. And the whole toolchain, too.

You might say, well, why does it matter? Is there any difference?

Yes, there is, and it’s huge. Let me show you.

Read the article

Command mount --rbind mounts recursively, which is handy, but umount has no recursive capabilities, so several umount commands are needed to unmount. The easiest way to do this is to look into /proc/mounts :

cat /proc/mounts

Even better:

cat /proc/mounts | awk '{print $2}'

Take a look and if you can grep needed mountpoints out of it, just add |grep <something> | sort -r | xargs umount to the above line.

I am used to ask Linux utility date about everything related to date/time, like date -d "next friday" or date -d "+245 min", but it doesn’t answer one question I have pretty often: what time is it now in certain timezone?

So I had to create a simple script for this:

1
2
3
4
5
6
7
#!/bin/bash

localtime=`date +"%x %X"`
zonediff=`date -d "$1" +"%H:%M"`
hours="`echo $zonediff | cut -d':' -f1` hours"
minutes="`echo $zonediff | cut -d':' -f2` minutes"
date -d "-$hours -$minutes" +"%x %X"

Zone here is represented by alphabetic abbreviation, that is if you named this script e.g. “timein”, you use it like this:

timein EDT

Old-time problem comes back: coreutils doesn’t roll back to calls supported by your older kernel. This results in errors like:


Failed 'touch .unpacked': No such file or directory

or:


Failed 'touch .unpacked': Bad address

Funny little helpful hack taken here

1
2
3
4
mv /bin/touch /bin/oldtouch
echo '#!/bin/sh' > /bin/touch
echo 'echo -n >> "$1"' >> /bin/touch
chmod +x /bin/touch

Don’t forget to restore it back later.

Taken from the Fedora forum

..........

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#modprobe dm-mod
#vgscan
  Reading all physical volumes.  This may take a while...
  Found volume group "VolGroup00" using metadata type lvm2
# vgchange -a y VolGroup00
# lvdisplay
  --- Logical volume ---
  LV Name                /dev/VolGroup00/LogVol01
  VG Name                VolGroup00
. . .
  --- Logical volume ---
  LV Name                /dev/VolGroup00/LogVol00
  VG Name                VolGroup00
# mkdir /mnt/VolGroup00/01
# mount /dev/VolGroup00/LogVol01 /mnt/VolGroup00/01

Reference the man pages for lvm, pvdisplay, vgdisplay, lvcreate, lvdisplay, lvextend

..........

As you can see, you need two things: dm-mod kernel module and LVM2 package (apt-get install lvm2 on Debian, emerge lvm2 on Gentoo and so on)

My personal note: LVM provides several useful features but has one huge drawback. If your disk or file system crashes, you won’t be able to access your LVM partitions using any liveCD. At least I don’t know any that would have both dm-mod module and LVM2 package.

Well, quite self-explanatory. Only Bash and Ruby. Options “folder” and “name” are examples, replace them with what you need… Shared in hope this can help someone.

Read the article

The subject is extremely annoying. I am not that lazy to add a comment symbol myself. This would have been a good idea though, if it wasn’t sometimes needed to insert blocks of code via clipboard. With auto-comment on, if I insert

some line
# some comment
some line

I get

some line
# some comment
# some line

because there is a CR at the end of commented line and the auto-comment function adds the # symbol to the next line automatically.

The real problem begins when you try to insert many lines with some of them commented. You end up with each line after the first comment commented, the next comment commented twice so the next lines all commented twice and so on.

The solution:

Add this to your vimrc file:


au FileType * setl fo-=cro

where:

  • au = autocmd
  • FileType * – any file type
  • setl = setlocal
  • fo = formatoptions
  • -=cro disables c, r and o options ( see documentation )

This is a really annoying problem we meet pretty often. You can meet it even if you don’t use Bash. For example, you upload an image named “my lovely cat.jpg” to your website and find no image there. Or it has no thumbnail. This may happen because you are using some program for making thumbnails that is being called from, say, PHP via backtick operator (that is, using unix shell) and that program interprets the file name passed to it as three different command line options: my, lovely and cat.jpg.

Read the article

It’s not easy to remember, so here’s the reference.

var=aaa.bbb.ccc

  % %% # ##
  .*  ${var%.*} => aaa.bbb ${var%%.*} => aaa ${var#.*} => aaa.bbb.ccc (doesn’t work) ${var##.*} => aaa.bbb.ccc (doesn’t work)
  *.  ${var%*.} => aaa.bbb.ccc (doesn’t work) ${var%%*.} => aaa.bbb.ccc (doesn’t work) ${var#*.} => bbb.ccc ${var##*.} => ccc
Read the article

I was doing a simple test of pwgen (handy utility to generate passwords) and since this test was creating a lot of files in a folder I decided to combine it with a test of ls speed vs find speed:

Read the article

For some reason there is no obvious way to enable it (Gnome 2.26.3)

  1. Open GConf editor: Applications -> System Tools -> Configuration Editor
  2. apps -> metacity -> general -> compositing manager (check box there)

That’s all. Enjoy numerous visual effects like shadows, real transparency (underlying windows become visible) etc.

Pretty easy:


mysql -u<username> -p<password> <dbname> -B -e "select * from <tablename>" | sed 's/\t/","/g;s/^/"/;s/$/"/;s/\n//g' > <filename.csv>