Archive

Archive for May, 2011

Reduce the size of a PDF file

Problem
You produce a PDF but it’s too big. How to reduce its size?

Solution

Make a much smaller PDF. Note that images will be quite ugly.

gs -sDEVICE=pdfwrite -dPDFSETTINGS=/screen -dCompatibilityLevel=1.4 -dNOPAUSE -dQUIET -dBATCH -sOutputFile=output.pdf input.pdf

The PDF will be a bit smaller. Images remain in good quality.

gs -sDEVICE=pdfwrite -dPDFSETTINGS=/printer -dCompatibilityLevel=1.4 -dNOPAUSE -dQUIET -dBATCH -sOutputFile=output.pdf input.pdf

For more possible values of PDFSETTINGS, see this page.

Ref.: here.

Categories: bash Tags: , , , ,

Some notes on MySQL

Log in to MySQL as administrator:

mysql -u root -p

Show databases:

mysql> show databases;

Show tables in the database mysql:

mysql> use mysql;
mysql> show tables;

Administration program for the mysqld daemon: mysqladmin.

Accessing phpmyadmin: http://localhost/phpmyadmin.

Another GUI administration program: mysql-admin (note the ‘-‘).

Create a database called “finance”:

$ mysqladmin -u root -p create finance

Create a user who can work on the previously created finance database:

mysql> GRANT select, insert, update, delete, create, drop, index, alter,
create temporary tables, lock tables
ON finance.*
TO 'myuser'@'localhost'
IDENTIFIED BY 'mypass';

Now we have a user called “myuser” with the password “mypass” who can work on the “finance” database from localhost only.

Log in with the newly created user:

$ mysql -u myuser -p
mysql> use finance;
mysql> show tables;
Categories: mysql Tags: ,

Compress with RAR and split into multiple files

May 27, 2011 2 comments

Problem

You have a large file that you want to send to a friend. One possible way is to upload it to your Dropbox folder and when he got it you remove it. If the file is too big, split it into multiple smaller files. If your friend uses Windows, you should compress the file with ZIP or RAR. Here I show you how to do it with RAR.

Solution

rar a -m5 -v10m myarchive movie.avi

It will compress movie.avi and split it into 10 MB chunks (-v10m), using the best compression ratio (-m5). In the case of an AVI file it won’t help much, so here you could use -m0 too, which means no compression at all. The default is -m3 by the way. Output: myarchive.part1.rar, myarchive.part2.rar, etc.

If you prefer the traditional names (myarchive.rar, myarchive.r00, myarchive.r01, …), add the -vn switch too.

Extraction:

rar x myarchive.part1.rar

Credits
This entry is based on this post: http://linux.byexamples.com/archives/226/compress-to-multiple-volume-rar/.

Categories: bash Tags: , , ,

Hadoop

MapReduce and Hadoop have been on my list for some time. I hope I will have some time soon to look at them in detail. Until then, here are some nice tutorials:

Categories: Uncategorized Tags: , ,

Free HTML5 templates

Free HTML5 Templates are just what they sound like: free web templates using valid HTML5 code. Some of the templates may also include CSS3 code as well, but those will be clearly tagged, and because CSS3 is still evolving and is highly dependent upon browser vendor implementations, the CSS may not always validate.

The goal, of course, is to help you use the newest, most modern template code available. As the HTML 5 standard evolves, we’ll evolve with it.

We license our free templates under the Creative Commons Attribution (by) 3.0, which put simply, means you can distribute, remix, tweak, and build upon our work, even commercially, as long as you credit us for the original creation.” (source)

The template portfolio is here.

Categories: web Tags: , ,

Something happened to my laptop keyboard. Or not?

Yesterday evening something strange happened to my laptop keyboard. Some keys on the right side stopped working correctly thus it became unusable.

I switched to console with CTRL+ALT+F1 but the problem remained. I rebooted the machine, it didn’t help. I booted to Windows but it was working fine there. Hmm, so the hardware is OK. But why is it not working under Ubuntu? This morning I switched it on hoping that a night rest might have solved the problem :), but no luck. I have a USB keyboard, so I could use the system with that, but what’s wrong with the original keyboard?

Then I found the problem… Num Lock was pressed by accident :))) Holy crap. I use Midnight Commander a lot and Num Lock is right next to Insert, so I must have pressed it by accident. Strangely, Num Lock remains on after switching off and on the computer.

The important thing is that my keyboard is fine and it works correctly again :)

Categories: ubuntu Tags: ,

randomize-lines

There is a useful command in the randomize-lines package namely “rl“. It reads lines from the standard input and shuffles them. It can also be used to pick a random line from a text file.

Let’s see some examples from the man:

Play a random sound after 4 minutes (perfect for toast):

sleep 240 ; play `find /sounds -name ´*.au´ -print | rl --count=1`

Play the 15 most recent .mp3 files in random order.

ls -c *.mp3 | head -n 15 | rl | xargs --delimiter=´\n´ play

Roll a dice:

seq 6 | rl --count 2

Roll a dice 1000 times and see which number comes up more often:

seq 6 | rl --reselect --count 1000 | sort | uniq -c | sort -n

Shuffle the words of a sentence:

echo -n "The rain in Spain stays mainly in the plain." \
| rl --delimiter=´ ´;echo

Find all movies and play them in random order.

find . -name ´*.avi´ -print0 | rl -0 | xargs -n 1 -0 mplayer

Because -0 is used filenames with spaces (even newlines and other unusual characters) in them work.

Categories: bash Tags: , ,

C-64 programming on PC

Categories: commodore Tags:

Setting file permissions in your Dropbox folder recursively

Problem

I use Dropbox a lot; it’s very useful to have a large folder that is automatically synchronized and made available on all my machines, be it either a Linux or a Windows box.

However, I have problems with file permissions. Unfortunately it’s not treated by Dropbox in a platform-independent way. What I mean is the following: when I log in to Windows, Dropbox “nicely” flattens all my files, i.e. it removes the executable flag from my scripts… When I log back to Linux, all my scripts (*.sh, *.py, etc.) are non-executable. Great! :(

Solution #1 (naive approach)

First I wrote a simple bash script to correct the directory and file permissions:

#!/bin/bash

HERE=$HOME/Dropbox
cd $HERE

find . -type d -print0 | xargs -0 chmod 700
find . -type f -print0 | xargs -0 chmod 600
find . | grep ".py$" | xargs chmod u+x
find . | grep ".sh$" | xargs chmod u+x
chmod u+x $HERE/xmind-portable/XMind_Linux/xmind
chmod u+x $HERE/xmind-portable/XMind_Linux/xmind-bin
chmod 755 $HOME/Dropbox
chmod u+x git.projects/others/upskirt/upskirt

I put it in crontab and called the script once in an hour.

The script gets the job done but it has a great price. In my Dropbox folder I have more than 30,000 files. This script updates all of them with “chmod”. Even if there is no change (for ex. a file had 0600 permissions and we set 0600 again), Dropbox notices the update (!) and synchronizes all the files with its server! Thus, even if the script modified the permissions of few files, all the files will be synchronized. Result: high CPU and RAM usage for several minutes when the script is launched…

Solution #2 (sophisticated approach)

To overcome the previous problem, we need to do the following: look at the permissions of a directory/file and IF its permissions are not good THEN modify the permissions. That is, if permissions are OK, we do not touch the file with “chmod” and thus Dropbox will not take any actions. Dropbox will synchronize only those files whose permissions really changed.

So here is my script, written in Python. Just put it in your Dropbox folder and launch it. By default it’s in “dry” mode, i.e. it only prints the changes without applying them. If the result is OK, set DRY to False. The script also verifies if it’s in the Dropbox folder. Since it changes permissions recursively, you don’t want to run it in your HOME folder :)

#!/usr/bin/env python

# dropbox_permissions.py
# https://ubuntuincident.wordpress.com/2011/05/08/setting-file-permissions-in-your-dropbox-folder-recursively/

import os
import sys
import stat

# dry run, make no changes just show them
DRY = True
#DRY = False

# verify if we are in the Dropbox folder
VERIFY_DROPBOX = True
#VERIFY_DROPBOX = False

ignore_dirs = ('.git', '.svn', '.eric4project', '.ropeproject')
executable_file_extensions = ('.py', '.sh', '.pl')
executable_files_with_relative_path = (
    './xmind-portable/XMind_Linux/xmind',
    './xmind-portable/XMind_Linux/xmind-bin'
)

changes = 0


def chmod_ux(file):
    set_mode_to(file, 0700)


def set_mode_to(file, permissions):
    global changes
    f = file
    mode = get_oct_mode(f)
    if mode != oct(permissions):
        try:
            if DRY:
                print "# chmod {0} {1}".format(oct(permissions), f)
            else:
                os.chmod(f, permissions)
            changes += 1
        except OSError:
            print >>sys.stderr, "# cannot chmod the file {0}".format(f)


def get_oct_mode(entry):
    entry_stat = os.stat(entry)
    mode = oct(entry_stat[stat.ST_MODE] & 0777)
    return mode


def process_dir(directory):
    set_mode_to(directory, 0700)


def process_file(file):
    f = file
    file_name = os.path.split(f)[1]
    file_ext = os.path.splitext(file_name)[1]

    if (file_ext in executable_file_extensions) or (f in executable_files_with_relative_path):
        process_exe_file(f)
    else:
        process_other_file(f)


def process_exe_file(file):
    chmod_ux(file)


def process_other_file(file):
    set_mode_to(file, 0600)


def traverse(directory):
    """Traverse directory recursively. Symlinks are skipped."""
    #content = [os.path.abspath(os.path.join(directory, x)) for x in os.listdir(directory)]
    content = [os.path.join(directory, x) for x in os.listdir(directory)]
    dirs = sorted([x for x in content if os.path.isdir(x)])
    files = sorted([x for x in content if os.path.isfile(x)])

    for d in dirs:
        if os.path.islink(d):
            continue
        dir_name = os.path.split(d)[1]
        if dir_name in ignore_dirs:
            continue
        # else
        process_dir(d)
        traverse(d)
    
    for f in files:
        if os.path.islink(f):
            continue
        # else
        process_file(f)


def verify_dir(directory):
    d = os.path.abspath(directory)
    if 'dropbox' not in d.lower():
        print >>sys.stderr, """
It seems that you are not in the Dropbox folder. If you launch this
script in a wrong folder, it may do more harm than good since it
changes file permissions recursively.
If this is a false alarm and you really want to execute the script
here, disable this verification by setting the variable VERIFY_DROPBOX
to False.
"""
        sys.exit(1)


def main():
    start_dir = "."
    if VERIFY_DROPBOX:
        verify_dir(start_dir)
    traverse(start_dir)
    #chmod_ux(sys.argv[0])
    print "# changes: {0}".format(changes)
    if DRY:
        print "# >>> it was a dry run, no changes were made <<<"

####################

if __name__ == "__main__":
    main()

The up-to-date version of the script is available here, in the dropbox/ folder.

You can safely call it from crontab at every hour, it will not eat up your system resources.

Categories: bash, python Tags: ,

Screenshot and video information with mplayer

Let our test file be “/tmp/test.wmv“.

Getting video info

mplayer '/tmp/test.wmv' -ao null -vo null -frames 1 -identify

It will produce a long output. You can refine the output:

(mplayer '/tmp/test.wmv' -ao null -vo null -frames 1 -identify | grep ID_) 2>/dev/null

Again, it will produce a long output that I omit. The video length is here (given is sec.):

ID_LENGTH=119.93

Another interesting line is the video summary:

(mplayer '/tmp/test.wmv' -ao null -vo null -frames 1 -identify | grep 'VIDEO:') 2>/dev/null

Sample output:

VIDEO:  [WMV3]  320x240  24bpp  1000.000 fps  386.0 kbps (47.1 kbyte/s)

Taking a screenshot at a given time

mplayer '/tmp/test.wmv' -ss '20' -noautosub -frames 1 -ao null -vo png:outdir='/tmp'

Here we take a screenshot from the video at 20 sec. The output file will be /tmp/00000001.png (mplayer gives this name automatically).

Ref.: I found these tricks in this project.

Follow

Get every new post delivered to your Inbox.

Join 72 other followers