Linux/Unix usage tips

This is memo for myself (thus not necessarily self-explanatory for all) about various GNU/Linux and Unix related commands that I have used since around 1999. The tips are in chronological order, which means that the first ones are more likely deprecated than later ones.

To utilise this web-page in plain text format, e.g. with grep or emacs, you might want to remove/convert some HTML tags:
sed -e 's/&#60;/</g' -e 's/&#62;/>/g' -e '/^<pre>$/d' -e '/^<\/pre>$/d' linux.html > linux.txt

Seppo Heikkila, last updated: 2020-08-15

Basic file commands

Command [extra params] Description
man command Shows the manual-page for command.
info command Shows the info-page for command(more detailed than man).
cp [-R] from_where to_where Copy files from place to place.
mv from_where to_where Move files from place to place.
rm [-rf] target_file Remove files
du [-sk] disk usage in kilobytes
swapon [-s] swap usage (summary)
free displays free memory and swap resources
dmidecode Dump DMI (SMBIOS) table to human readable format
cardctl PCMCIA card control utility ( e.g. cardctl ident)
top displays the top CPU processes
ln [-s] real_dir_name link_name make hard or symbolic links to files
locate file_name search for files
mount /dev/device /mnt_place [-o uid=user,gid=group,umask=0007] Sets up(mounts) a file system to /mnt_place directory.
umount /dev/device Unmounts the wanted device.
dmesg print or control the kernel ring buffer.

Adding groups and users

Commands adduser and addgroup will deal with this. The settings are saved into /etc/group. For example group root0 where belong ssheikki and www-data is defined like this:
root0:x:102:ssheikki,www-data
To show the groups where user_name belongs to:
groups user_name

Mounting devices

Mounts are loaded from /etc/fstab in the startup.
For example line(in /etc/fstab)
/dev/hda1 /c vfat gid=root0,uid=www-data,umask=0007 0 0
mounts the /dev/hda1 to /c with given parameters.

Same can be given also from command-line:
mount /dev/hda1 /c -o gid=root0,uid=www-data,umask=0007

To see all partitions, use:
fdisk -l command
If you want for example remount your read filesystem to rewritable, use;:
mount -o remount,rw /dev/hda2

Mouse Settings

Mouse device is /dev/mouse, a symbolic link(ln -s) to the physical device, such as:
/dev/ttyS0 for COM1
/dev/ttyS1 for COM2
/dev/ttyS2 for COM3
/dev/ttyS3 for COM4

Mouse can be configured using gpmconfig which is config for gpm. The settings for standard ps2 mouse are following:
Set the responsiveness (normally not needed) [1]?
> 10
Repeat protocol (enter 'none' to turn repeating off) [raw]?
>
Do you want to add any additional arguments []?
>
Do you want to test this configuration (y/N)? y
gpm -m /dev/psaux -t imps2 -r 10 -Rraw
Finish testing by typing Ctrl-D
Current configuration: -m /dev/psaux -t imps2 -r 10 -Rraw
Device: /dev/psaux
Type: imps2
Responsiveness: 10
Repeat_Type: raw

Runtime information /proc/

The /proc file system is a pseudo-filesystem that stores relevant informations about the kernel(man 5 proc). For example /proc/interrupts shows how many times each IRQ was used and by whom.

Unix has Different Run Levels

Each run level has own configuration files. In debian they are in /etc/rc#.d/ and in redhat they are /etc/rc.d/rc#.d/ (# means the run level number).

Power Off: Run Level 0(/etc/rc0.d/)
Single User: Run Level 1(/etc/rc1.d/)
Multiuser: Run Level 2(/etc/rc2.d/)
Full Multiuser(2 with NFS): Run Level 3(/etc/rc3.d/)
Unused: Run Level 4(/etc/rc4.d/)
X11(graphical): Run Level 5(/etc/rc5.d/)
Reboot: Run Level 6(/etc/rc6.d/)

To change boot behaviour, modify /etc/inittab. The run levels are also described there.

A script starting with S starts a service. One starting with K stops a service. The number after the `S' or `K' specifies the order in which the scripts are run. For example, S25xdm will start before S35xdm.

Stop/Start/Restart scripts

Subdirectory /etc/init.d/ (or in redhat /etc/rc.d/init.d) contains all startup scripts that may be used to stop/start/restart the related process. For example /ec/init.d/gpm stop will stop the mouse and /ec/init.d/gpm start will start it again.

Installing kernel

/usr/src/linux/README tells what to do. First download the latest source. Then everything starts with "make menuconfig". If you don't have ncurses-dev installed please find it from the web or in debian just use "apt-get install ncurses-dev".

As simplest, the following steps should be taken(with 2.4.x kernel):
make menuconfig
make dep
make bzImage
make modules
make modules_install
make install

or using with make-kpkg:
make-kpkg --targets
make-kpkg buildpackage

Things to remember in menuconfig

To select modules for different kernels in /etc/modules

Write following to /etc/modules:
if `kernelversion` == 2.4
  uhci
  emu10k1
  ...
elseif `kernelversion` == 2.6
  uhci-hcd
  snd-emu10k1
  ...
endif

Window managers

To handle logging I recommend; wdm(WINGs Display Manager).
For actual managers you should try;

Test if your port X is open

To resolve net-addresses

Use nmap, host and dig programs.

Show info about PCI cards

lspci, Shows info about your pci cards(replaces /proc/pci)
tune2fs, from ext2 -> ext3 for example.

[1] Linux Administration Guide
http://www.bo.infn.it/alice/alice-doc/mll-doc/linux/admin/lxadmgde.html

To setup a new disk

Partition the disk: cfdisk.
Make it ext3: mke2fs -j /dev/hdXX
Set parameters, turn ext2, etc: tune2fs -m 0 /dev/hda1
Remember to put DMA on: hdparm -c /dev/hdb, hdparm -c 1 /dev/hdb
Good configure for hdparm is for example:
/dev/hda:
 multcount    = 16 (on)
 I/O support  =  1 (32-bit)
 unmaskirq    =  1 (on)
 using_dma    =  1 (on)
 keepsettings =  0 (off)
 nowerr       =  0 (off)
 readonly     =  0 (off)
 readahead    =  8 (on)
 geometry     = 19457/255/63, sectors = 312581808, start = 0
 busstate     =  1 (on)
and acceptable speed for example:
ssheikki:~# hdparm -Tt /dev/hda
/dev/hda:
 Timing buffer-cache reads:   128 MB in  0.43 seconds =297.67 MB/sec
 Timing buffered disk reads:  64 MB in  1.17 seconds = 54.70 MB/sec
If you get "HDIO_SET_DMA failed: Invalid argument", when setting hdparm -d1 /dev/hda then your kernel is missing VIA module. Select it from:
ATA/IDE/MFM/RLL support  --->  
  IDE, ATA and ATAPI Block devices  --->
    <*>     VIA82CXXX chipset support 
For real world tests:
Writing speed:
time dd if=/dev/zero of=test bs=1048576 count=1024
Reading speed:
time dd if=test of=/dev/null bs=1048576 count=1024

In the end you can umount your file system and do fsck(filesystem check):
For ext2 in is done: e2fsck -c -c -C 0 -f -v /dev/hdXX

Helvetin skandit

You can try these and hope that it will work! :)
1. loadkeys fi-latin1
2. add to /etc/locale.gen following
fi_FI ISO-8859-1
fi_FI@euro ISO-8859-15
And the run locale-gen.
3. http://www.arska.org/finnish-howto/html/
4. xorg.conf keyboard should look about like this for Finns:
Section "InputDevice"
        Identifier  "Keyboard0"
        Driver      "keyboard"
        Option      "XkbRules" "xorg"
        Option      "XkbModel" "pc105"           # pc105 is finnish
        Option      "XkbLayout" "fi"
EndSection

/dev/null breaks

rm the old and create new with:
mknod -m 666 /dev/null c 1 3
The break might cause for example sshd fail to start(sshd[756]: fatal: daemon() failed: Success).

Change finger information

Use chfn command.

Setting up libraries

Add library paths in:
/etc/ld.so.conf
See man for the following commands:
ldconfig
ldd program

Gphoto2 & USB settings

You need USB support from kernel(see preferred kernel settings below and/or Linux USB).
You need: libusb
You need: libgphoto2 installed. Remember to see that usb is found by libgphoto2.
Last you need: gphoto2 installed.
How to know that something might work:
less /proc/bus/usb/devices
gphoto2 --list-ports
gphoto2 --auto-detect

Prefered kernel settings for usb:
debian:/usr/src/kernel/linux-2.6.7# grep -i usb .config | grep -v "#"
CONFIG_USB=m
CONFIG_USB_DEVICEFS=y
CONFIG_USB_EHCI_HCD=m
CONFIG_USB_UHCI_HCD=m
CONFIG_USB_HID=m
CONFIG_USB_HIDINPUT=y

Find out who is using some file or socket

fuser -v /resource

Ssh authentication without password

Goto computer FROM where you want to access some computer:
Generate key pares:
ssh-keygen -b 2048 -t dsa
Copy the file to the computer you want to access. If many computers, just append the id_dsa.pub-file to authorized_keys-file.
scp id_dsa.pub user@remotehost:.ssh/authorized_keys
And volaa... do a test:
ssh user@remotehost

Install windows and Linux on different harddrives, Lilo config

Situation example: linux on Master Disk MBR(/dev/hda) and windows on Slave Disk MBR(/dev/hdb). In this situation, linux bootloader(lilo in this example) starts from Master MBR. To add Windows boot you need to add following choice to /etc/lilo.conf.
other=/dev/hdb2
        label="Windows"
        table=/dev/hdb
        map-drive=0x80
                to=0x81
        map-drive=0x81
                to=0x80
It changes the active disk in the boot so Windows will be booted unnoticed.

Where are your cd-roms

To see this, cat:
cat /proc/sys/dev/cdrom/info

Second LoopBack interface/ip for local usage

If you want 193.167.3.129, for example, to point to your own computer, use:
/sbin/ifconfig lo:1 193.167.3.129

Test in local:
ssheikki@jt11-231-2:/$ traceroute 193.167.3.129
traceroute to 193.167.3.129 (193.167.3.129), 30 hops max, 38 byte packets
 1  jt11-231-2 (193.167.3.129)  0.061 ms  0.020 ms  0.014 ms
Test in computer where loopBack adapter isn't used:
ssheikki@jt7-236:~$ traceroute 193.167.3.129
traceroute to 193.167.3.129 (193.167.3.129), 30 hops max, 38 byte packets
 1  trinet-gw (130.233.31.254)  2.303 ms  2.331 ms  3.403 ms
 2  gw-2-v96.hut.fi (130.233.231.19)  0.899 ms  0.634 ms  0.681 ms
 3  u35211.win.hut.fi (193.167.3.129)  0.833 ms  4.314 ms  0.591 ms

Howto setup http over SSL(https)

creating the certificate and config apache
securing the actual webpages

Howto tunnel Java RMI through HTTP

  • Install Apache 2.x to port 80.
  • Install Tomcat 4.x to port 8080. For windows, use exe-installer.
  • Install Tomcat Web Server Connectors, i.e. get mod_jk_1.2.x.dll(in linux mod_jk_1.2.x.so).
  • Make sure you have following lines in Apache httpd.conf:
    LoadModule proxy_module modules/mod_proxy.so
    LoadModule proxy_connect_module modules/mod_proxy_connect.so
    LoadModule proxy_http_module modules/mod_proxy_http.so
    LoadModule rewrite_module modules/mod_rewrite.so
    
    Options Indexes FollowSymLinks ExecCGI
    
    
        AllowOverride None
        Options +ExecCGI
        Order allow,deny
        Allow from all
    
    
    RewriteEngine on
    RewriteLog C:/tomcat/rewrite.log
    RewriteLogLevel 9
    RewriteRule ^/cgi-bin/java\-rmi\.cgi$ http://localhost/examples/servlet/ServletHandler    [P]
    
    
    LoadModule jk_module C:/Winnt/system32/mod_jk_1.2.5_2.0.47.dll
     
    
    JkWorkersFile "C:/tomcat/conf/workers.properties"
    JkLogFile "C:/tomcat/logs/mod_jk.log" 
    
    JkLogLevel info 
    
    JkMount /manager ajp13
    JkMount /manager/* ajp13 
    
    JkMount /examples ajp13
    JkMount /examples/* ajp13 
    
    JkMount /tomcat-docs ajp13
    JkMount /tomcat-docs/* ajp13 
    
    JkMount /webdav ajp13
    JkMount /webdav/* ajp13 
    
    JkMount /*.jsp ajp13 
    
  • tomcat/conf/workers.properties should contain:
    workers.tomcat_home=C:/tomcat
    workers.java_home=C:\j2sdk1.4.2_01
    ps=\ 
    
    worker.list=ajp13, ajp14
    worker.ajp13.port=8009
    worker.ajp13.host=localhost
    worker.ajp13.type=ajp13
    worker.ajp13.lbfactor=1 
    
    worker.loadbalancer.type=lb
    worker.loadbalancer.balanced_workers=ajp13
    worker.inprocess.type=jni
    worker.inprocess.class_path=$(workers.tomcat_home)$(ps)server$(ps)lib$(ps)catalina.jar
    worker.inprocess.cmd_line=start
    worker.inprocess.jvm_lib=$(workers.java_home)$(ps)jre$(ps)bin$(ps)server$(ps)jvm.dll
    worker.inprocess.stdout=$(workers.tomcat_home)$(ps)logs$(ps)inprocess.stdout
    worker.inprocess.stderr=$(workers.tomcat_home)$(ps)logs$(ps)inprocess.stderr 
    
  • Tadaa... you might want to test with: rmiservlethandler.zip.
  • My iteration time for this easy setup was 16h. :I

    Image and video screen capture in Linux

    The software for this is xvidcap.
    To capture in avi format for example:
    xvidcap --file grap.avi --time 100
    Brilliant! :)

    Making thumbnails images

    Use convert:
    convert -resize 15x15% original.gif original_thumb.gif

    It might be usefull to use script:
     for image in *.jpg
       do
       echo "Processing: $image ..."
       convert  -resize 15x15% $image thumbs/$image
       echo "done."
     done
    

    Create animated gifs

    Gifmerge is neat and simple:
    ./gifmerge -l0 -10 animate_this/*.gif > anim.gif

    Sometimes you might to need a script, that selects non-duplicate images and converts them to be in right format and then creates the animation:
    mkdir __tmp
     
    for image in *.gif
      do
      if [ $image_prev ] ; then
         diffout="`diff $image_prev $image | wc -l`"
         if [ $diffout = "0" ] ; then
            echo "Identical images: $image_prev $image, not processed."
         else
            convert  -resize 100x100% $image __tmp/$image
            echo "Image $image processed."
         fi
      else
        convert  -resize 100x100% $image __tmp/$image
        echo "Image $image processed."
      fi
      image_prev=$image
    done
    
    ./gifmerge -l0 -10 __tmp/*.gif > anim.gif
     
    rm -rf __tmp
     
    echo "Output should be here:"
    ls -laF anim.gif
    echo "done."
    

    Shell scripting

    http://vertigo.hsrl.rutgers.edu/ug/shell_help.html

    DVD players

    Install ogle and ogle-gui. Remeber that you also need libdvdcss for all movies to be readable.

    Example run:
    ogle -u gui /dev/hdc

    Howto install foreign packages(rpm, etc.) in debian

    There is program called alien, which converts packages to debian packages which can be then easily installed:
    alien - Convert or install an alien binary package

    alien --to-deb cool-program.rpm
    dpkg -i cool-program.deb

    Howto merge and edit avi-files

    Install avidemux or avimerge.
    Note: Avimerge is easier but has problems with audio and etc.

    Video editors

    avidemux - best with avi files, at least, maybe best in others too.
    cinelerra

    Clock synchronization with NTP

    Network Time Protocol debian packages: ntp, ntp-simple.
    Servers: ntp1.hut.fi, ntp1.funet.fi
    List ntp servers:
    ntpq -p

    Howto capture stream data:

    - to capture rm-stream into wav:
    vsound -t -v -f output.wav RealPlayer8/realplay input.rm
    vsound: digitally record output of an OSS audio program.
    - rip radio streams to mp3 files: streamripper URL

    Howto convert sound into mp3/ogg

    - audacity - Graphical cross-platform audio editor.
    Encodes wav to mp3/ogg and lots of more. And use through very usable GUI! Brilliant! Myös Suomen kielen tuki löytyy!

    Timing commands

  • at: input time and wanted commands to execute.
  • crontab input.file: crontab -l list the commands to be executed.

    Where the executed program is on the path and it's man page

    whereis

    Example, where is whereis program:
    root@debian:/# whereis whereis
    whereis: /usr/bin/whereis /usr/share/man/man1/whereis.1.gz
    

    Search files recursively for wanted string, windows and linux

    Linux:
    grep -r search_string *
    Windows(not in XP):
    search search_string

    Wget and for example

    for((i=9201;9300>i;i++)); do wget http://osoite.org/292_$i.jpg ;done

    Converting video formats, e.g. vro to avi/mpeg/etc

    Use memcoder, which is part of the mplayer:
    mencoder source.vro -o output.avi -ovc lavc -lavcopts vcodec=mpeg4:vhq:vbitrate=1800 -oac copy
    Notes: -ovc defines video codec.
    Notes 2: msmpeg4 codec works in Windows

    Converting mov -> avi

    mencoder *.MOV -o koeX.avi -srate 8000 -ovc lavc -lavcopts vcodec=mpeg4:vhq:vbitrate=1800 -oac mp3lame -lameopts vbr=3:br=32

    Update alternative packet

    update-alternatives --config automake

    Display network information

    netstat -a
    Print network connections, routing tables, interface
    statistics, masquerade connections, and multicast memberships.

    Executing X programs as root

    Install sux. Log in as normal user who is allowed to use X programs. Write sux -, behaves just like su but transfers $DISPLAY and the X cookies.

    Securing computer for local-hack

    Disable floppy boot and add-password for lilo: security + howto.

    More sequrity issues: ulimit.

    Bluetooth installation

    Pretty good tutorial is here.

    But basically it's like this:

    Execute images with qemu

    The QEMU System emulator simulates a complete PC!

    Update dynamic libraries

    Configure dynamic linker run-time bindings:
    ldconfig

    Security tools

    Nessus; free, powerful, up-to-date and easy to use remote security scanner.
    tiger; report system security vulnerabilities.

    Neural Network implementations

    Fast Artificial Neural Network Library implements multilayer artificial neural networks in C with support for both fully connected and sparsely connected networks:
    http://fann.sourceforge.net/

    USB hard disk

    - Check that disk is detected, e.g.: less /proc/bus/usb/devices
    - Partition the disk, e.g.: cfdisk /dev/sda
    - Format the partitions, e.g.: mke2fs -j /dev/sdaX
    - Test speed(~27MB/sec) e.g.: hdparm -tT /dev/sda6
    - Mount and enjoy, e.g.: mount -t ext3 /dev/sda6 /usbhd

    Samba

    smbstatus, smbclient, smbmount.

    CVS-howto

    set up: cvs -d CVSROOT init
    import: cvs -dCVSROOT import projektinnimi vendor_tag release_tag
    e.g. cvs -d/private/cvs_repository import peacebot TKK start
    set CVSROOT: export CVSROOT=:ext:ssheikki@kosh.hut.fi:/u/opi/90/ssheikki/cvs_repository

    Benchmarking computers

    LMbench, Tools for Performance Analysis.

    Open connections

    Show all open connections: netstat -a
    Show port numbers: netstat -n -a
    Who is using port: fuser -n tcp
    Verbose who is using port: fuser -n tcp -v <PORT>

    DNS name server

    apt-get install bind
    To create for exaple alias for jt11-117-3.tky.hut.fi
    master zone tky.hut.fi
    and the name alias jt11-117-3
    
    For easy configuration, please install webmin.
    webmin-bind, webmin-apache, webmin-samba, etc...
    Note that webmin starts it's own http server:
    https://127.0.0.1:10000/

    Analyze X-window connections

    xdpyinfo - display information utility for X
    xev - print contents of X events
    xwininfo - window information utility for X

    Partitioning disk in linux so that windows understands it

    Partion like this,
       Device Boot      Start         End      Blocks   Id  System
    /dev/sdb1               1        1824    14651248+   c  W95 FAT32 (LBA)
    /dev/sdb2            4256       30401   210017745    5  Extended
    /dev/sdb3            1825        4255    19527007+   c  W95 FAT32 (LBA)
    /dev/sdb5   *        4256        5471     9767488+  83  Linux
    /dev/sdb6            5472       30401   200250193+  83  Linux
    
    Now evetyhing is ok but the Extended type is wrong. Let's change it with fdisk.
    Command (m for help): t
    Partition number (1-6): 2
    Hex code (type L to list codes): L
    Hex code (type L to list codes): f
    Command (m for help): w
    
    Now the table looks like this:
       Device Boot      Start         End      Blocks   Id  System
    /dev/sdb1               1        1824    14651248+   c  W95 FAT32 (LBA)
    /dev/sdb2            4256       30401   210017745    f  W95 Ext'd (LBA)
    /dev/sdb3            1825        4255    19527007+   c  W95 FAT32 (LBA)
    /dev/sdb5   *        4256        5471     9767488+  83  Linux
    /dev/sdb6            5472       30401   200250193+  83  Linux
    

    Backup whole mysql-database

    Dump all databases with root access:
    mysqldump --all-databases -p -u root | gzip -c > mysql_dump_archive.sql.gz

    To restore the databases, run the console output of gzip through mysql:
    gzip -cd mysql_dump_archive.sql.gz | mysql -u root -p

    Another option is to log as a root into a mysql database:
    mysql -u root -p
    If you do not remember your password, you can reset it using root shell:
    /usr/bin/mysqladmin -u root password 'new_password'
    and then execute
    source mysql_dump_archive.sql;

    Upgrading mysql database from 1.5 to 1.7.1 (Ubuntu)

    Step1:
    Take backup of the mysql database and save AdminSettings.php and LocalSettings.php files. Install using aptitude the new mediawiki version (do not configure it!!, just install, i.e. aptitude install mediawiki). Copy AdminSettings.php and LocalSettings.php to /var/lib/mediawiki1.7 directory.

    Step2:
    Sign in the mysql database (mysql -u root -p) and rename your old table to text table. Like this:
    usedb wikidb;
    rename table sm_old to sm_text;
    

    Step3:
    Go to the /usr/share/mediawiki1.7/maintenance directory and command "php upgrade.php". All should go happily and last print is "Done.". Then command "php refreshLinks.php" to finish. Now the database should work again as before (or better).

    Backuping the mediawiki

    Make a static html page backup with:
    php /usr/share/mediawiki1.7/maintenance/dumpHTML.php -d /destination/dir
    Make an xml dump of the database:
    php dumpBackup.php --full > dumpBackup.xml
    Dump mysql databases:
    mysqldump --all-databases -p -u root | gzip -c > mysql_dump_archive.sql.gz

    GANTT chart - project management tool

    planner

    Irssi

    /set term_charset ISO-8859-15
    /set term_force_colors ON

    Sftp/scp connection with chroot

    You can restrict the user to his home directory:
    apt-get install scponly
    Check that /usr/sbin/scponlyc has suid:
    chmod 4755 scponlyc

    Using rdesktop

    Opening a connection with 24bit 80% windows size and with local disk linked to the remote computer:
    rdesktop -a 24 -g 80% -r disk:yebbey=/home/ssheikki/ rondo.tkk.fi

    Opening a connection with 24bit fullscreen and with local disk linked to the remote computer:
    rdesktop -a 24 -f -r disk:yebbey=/home/ssheikki/ rondo.tkk.fi

    Connecting a computer behind firewall:
    If you have a ssh server that you can connect at TKK, just make a ssh tunnel to that computer like this:
    ssh -L 7777:rondo.tkk.fi:3389 username@autlab
    Now you can open remote desktop just by connecting:
    rdesktop -a 24 -f -r disk:localdisk=/home/username/ localhost:7777

    SVN (Subversion) howto

    Checkout:
    svn checkout http://repository.address.com/path

    Status of local files compared to the svn repository:
    svn status [files/dirs]

    Update:
    svn commit [files/dirs]

    Commit:
    svn commit [files/dirs]

    Resolve (my files are good, update them to repository):
    svn resolved [files/dirs]

    Revert (my files are crap, get original ones from repository):
    svn revert [files/dirs]

    Check what is the status of files in current directory:
    svn -v status

    swheikki
    ruuvi.helsinki.fi
    melkinpaasi.cs.helsinki.fi

    subversion howto with https

    List project in repository:
    svn --username ssheikki list https://localhost/svn/

    Import a directory (svnhowto) to repository as svnhowto:
    svn --username ssheikki import svnhowto https://localhost/svn/svnhowto -m "initial import"

    Checkout all from repository :
    svn --username ssheikki checkout https://localhost/svn

    Commit:
    svn commit

    Delete:
    svn delete svnhowto

    subversion howto with ssh

    Same commands as above but you need to give path in the server:
    export SVN_SSH="ssh -l ssheikki"
    svn checkout svn+ssh://aut40/svn_repository/project_name

    subversion howto with local repository

    svn co file:///svn_repository/project_name

    Multiple ssh connections via one ip

    Add in /etc/ssh/ssh_config or in ~/.ssh/config
    Host aut40
            Hostname        yourserver.ftl
            Port            49490
            HostKeyAlias    aut40
            CheckHostIP no
    Host lap
            Hostname        yourserver.ftl
            Port            49491
            HostKeyAlias    lap
            CheckHostIP no
    

    Pasword change with first visit

    After creating the username, set the password to be expired. It requires user the change it when he tries to log in:
    passwd -e

    Howto backup your hard disk

    dump and restore packages

    Tar exclude directory

    tar -cvfX tar-name.tar exclude-file dirtobetarred/

    Find file with full path and date

    find . | grep McCtrl.c > /tmp/lsgreptmp && ll `cat /tmp/lsgreptmp`

    Kernel magic instructions

    List instructions, helps if computer is jammed:
    Alt + Prnt Screen + h

    Display all IRQ:s

    cat /proc/interrupts

    Design programs (open source)

    * Inkscape (http://www.inkscape.org/), Open Source vector graphics editor, with capabilities similar to Illustrator, Freehand, CorelDraw, or Xara X.
    * Dia (http://dia-installer.sourceforge.net/), diagram creation program, inspired by the commercial Windows program 'Visio'.
    * GIMP (http://www.gimp.org/), GNU Image Manipulation Program, for such tasks as photo retouching, image composition and image authoring.

    Programming editors (open source)

    # Code::blocks (http://www.codeblocks.org/) (Windows)
    # devcpp (http://www.bloodshed.net/devcpp.html) (Windows)

    Tell command that parameters end

    Problem, e.g. howto remove --bzip2 file. Answer:
    rm -- "--bzip2"

    Latex

    xdvi - display dvi files
    Create a one big html page from the tex document:
    latex2html -split 0 thesis.tex

    Mplayer and DVD subtitles

    mplayer -sid ID DVDFILE.VIDEO

    Create ssh-tunnel

    Tunnel FROM remote.iki.fi port 10024 TO localhost port 22:
    ssh -N -R 10024:localhost:22 -2 ssheikki@remote.iki.fi

    Ssh forward in case GatewayPorts off

    Computer behind firewall:
    ssh -v -N -R 49490:localhost:22 -2 ssheikki@asrobo.hut.fi
    Computer being forwarder:
    mkfifo in out
    cat out | nc -l -p49491 > in
    cat in | nc localhost 49490 > out

    Dave port:
    ssh -N -L 81:localhost:81 -2 ssheikki@voittoputki.dyndns.org

    CVS HOWTO

    Create a CVS repository where your projects are saved:
    cvs -d /absolut/path/of/your/repository init

    Put your project to the CVS repository:
    cd /directory/where/your/projects/is
    cvs -d /absolut/path/of/your/repository import -m "Name of your Project" modulename_of_your_project vendor_name version_1_0_0

    Get the stuff from CVS:
    cd /new/working/directory/of/you/project
    cvs -d /absolut/path/of/your/repository co modulename_of_your_project

    Update, add and Commit cvs:
    cvs -d /absolut/path/of/your/repository update
    cvs -d /absolut/path/of/your/repository add name_of_file_tobe_added
    cvs -d /absolut/path/of/your/repository commit

    WLAN howto

    Enable internal driver, e.g. for Acer Aspire 5021WLMi:
    1) Download and install acer_acpi:
    make acer_acpi.ko # How to compile the driver for 2.6 kernel
    modprobe acer_acpi
    echo "enabled: 1" >/proc/acpi/acer/wireless
    Install wlan driver, e.g. for Broadcom BCM5700:
    modprobe ndiswrapper
    ndiswrapper -i bcmwl5.inf

    Scan for network interfaces:
    iwlist wlan0 scan
    Connect wlan0 into a network node if found:
    iwconfig wlan0 essid ESSID_OF_FOUND_NETWORK
    ifconfig wlan0 up
    Get connection settings:
    dhclient wlan0

    PPPOE howto

    First time

    Make sure PPP is enabled in the kernel!
    Configure pppoe:
    pppoeconf

    After first time

    Make sure that server is started:
    /etc/init.d/ppp start
    Start pppoe connection:
    pon dsl-provider

    Continue http-download

    wget -c file_cancelled.zip

    Download one page with all it is inline contents, e.g. images

    Download all photos on the page:
    wget -H -p -nd -Ajpg http://www.address.com/

    Emulate 32bit, chroot

    chroot /var/chroot/sid-ia32

    Sending attachment files in linux

    mutt -s "Test" -a Attachment.tar.bz2 address@mail.com < body_message.txt

    Compiling kernel in Debian with make-kpkg

    Configure kernel:
    make menuconfig
    Compile kernel into debian package:
    make-kpkg buildpackage
    If this fails for some reason, you might want to do a clean:
    make-kpkg clean

    Compiling kernel-modules in Debian with make-kpkg

    Copy kernel-module codes into location defined by MODULE_LOC (default is /usr/src/modules). Build modules:
    make-kpkg modules

    Open remote desktop connection

    rdesktop -k fi -d universe -u firstname.lastname virgo.isunet.edu

    Partition a USB stick with messed partition table

    Create new partition and ignore the one used:
    cfdisk -z /dev/sdb

    Format file system for windows (mkfs.vfat is part of dosfstools package)
    mkfs.vfat /dev/sdb1

    Howto install/update debian packages offline

    Update your package list apt-get --print-uris update | sed s/\'//g
    Download the packes e.g. with wget or apt-zip script. Use bunzip2 or gunzip to extract all source and packages files.
    rename 's/Sources$/Sources.bz2/' *Sources && bunzip2 *.bz2
    rename 's/Packages$/Packages.bz2/' *Packages && bunzip2 *.bz2
    Copy the downloaded files to /var/lib/apt/lists/ and update the package list by running
    apt-get check

    Get packages apt-move and apt-zip:
    apt-get --print-uris install apt-zip
    apt-get --print-uris install apt-move
    and copy them to /var/cache/apt/archives/. Then you can just use apt-get install to install the packages.

    Generate a package download script
    apt-zip-list -m /usbstick -a upgrade
    To do the same in random directory, use the --skip-mount parameter. In UNIX/Linux, run the script to get packages. If you have to use a windows machine, see below.
    Download in windows
    Download unix tools package, e.g. UnxUtils.

    In case proxy servers have to be used, set http_proxy and ftp_proxy.
    export http_proxy=http://proxy.company.com:portnumber/
    export ftp_proxy=http://proxy.company.com:portnumber/

    Modify the script to work in windows:
    - separate parameters
    p1 = `echo $1 | gawk '{print $1}'`
    p2 = `echo $1 | gawk '{print $2}'`
    etc.
    - add wget proxy username and password
    wget --proxy-user=USERNAME --proxy-passwd=PASSWD -t3 -nv -O $2 $1
    Install downloaded packages apt-zip-inst does not seem to work so you can copy the packages directly to /var/cache/apt/archives/. Then just install the packages by running
    apt-get upgrade
    Install new individual packages Get urls of your_package update urls:
    apt-get -y --print-uris install your_package | grep deb | cut -f1 "-d " | sed s/\'//g
    Download your package, copy it to /var/cache/apt/archives/ and install it normally using apt-get install
    apt-get install your_package

    Autofs removable devices

    Get autofs packet:
    apt-get install autofs

    Create a /etc/auto.master file with this content
    /var/autofs/removable /etc/auto.removable --timeout=2
    The timeout defines when you can pull the device out.

    Create auto.removable file
    usbstick        -fstype=vfat,rw,gid=100,umask=002       :/dev/sda1
    cdrom           -fstype=iso9660,ro,sync,nodev,nosuid    :/dev/hdc
    
    Create symbol links to this paths
    ln -s /var/autofs/removable/usbstick /usbstick
    ln -s /var/autofs/removable/cdrom /cdrom

    Restart autofs:
    /etc/init.d/autofs

    Use the device, e.g.
    ln -laF /usbstick
    You can pull the device out after "timeout" seconds has passed.

    Connect to vnc server

    For example realvnc compatible is
    vncviewer address:display

    Add apt-get pgp-key

    After getting verification error while apt-get update, take the last 8 digits of the key and
    gpg --keyserver pgp.mit.edu --recv-keys 2D230C5F
    gpg --armor --export 2D230C5F | apt-key add -

    Who or what is using file

    fuser used_file_or_device

    Transfer file using serial cable

    Send file:
    cat file > /dev/ttyS0
    Read file:
    cat /dev/ttyS0

    How can you see contents of debian package

    dpkg --contents package.deb

    Howto encrypt yout harddisk

    Make sure that you have these modules in your kernel:
    dm_mod, dm_crypt, sha512, aes

    Format a device as luksDevice which is standard for hard disk encryption (Linux Unified Key Setup). Enter password when asked:
    cryptsetup -c aes-cbc-essiv:sha256 luksFormat /dev/sda1

    Create mapping to the drive:
    cryptsetup luksOpen /dev/sda1 sda1

    Now you can access your device in address /dev/mapper/sda1. You can e.g. format it with mkfs.ext2 and mount it.

    Encrypt with truecrypt

    Good encryption algorithms:
    Hash algorithm: Whirlpool
    Encryption algorithm: AES

    Create a ext2 partition on the disk:
    truecrypt -v -c /dev/sda1
    truecrypt -N 1 /dev/sda1
    mkfs.ext2 /dev/mapper/truecrypt1
    mkfs.ext2 mkfs.ext2 -m0 -T largefile4 /dev/mapper/truecrypt1 # for large files, inode size 4MB instead of 8kB
    tune2fs -l /dev/mapper/truecrypt1 # Check what was done, e.g. inode size on 128B

    To mount into directory mnt
    truecrypt /dev/sda1 mnt
    To unmount directory mnt
    truecrypt -d sda1

    Change keyboard variant

    Change X-keyboard layout
    xkeycaps
    Change X settings on the fly
    setxkbmap -layout fi -variant nodeadkeys

    Download files from link page

    wget -O - http://www.linkpage.com/ | wget -i -

    Encode CD to ogg/mp3

    Install mp3c package and rip+encode with the menu interface. Could not be easier!

    Webcam Logitech for Notebooks Pro (ID 046d:08cb), 2006/2007 model

    Kernel module:
    uvcvideo
    Programs:
    lucview
    ekiga

    Webcam Logitech Quickam Connect (ID 046d:08d9)

    Kernel module:
    gspca
    Programs:
    gqcam -v /dev/video1
    xawtv -geometry 640x480
    ekiga

    Subversion on localhost or over ssh

    Create the subversion repository to localhost:
    svnadmin create /var/svn/my_project

    On localhost start the subversion daemon:
    svnserve -d

    Checkout the project:
    svn co svn://localhost/var/svn/my_project

    In order to be able to add and commit stuff, you have to edit the /var/svn/my_project/conf/svnserve.conf and maybe add "anon-access = write" or create password database.

    To use the project in remote computer, run:
    svn co svn+ssh://server_address/var/svn/my_project
    To connect to a server with different port than 22, e.g. this in .ssh/config helps:
    Host lap
            Hostname        asrobo.hut.fi
            Port            49491
            HostKeyAlias    lap
            CheckHostIP no
    

    Update sources.list packages

    Deciding which mirror is the best to include in the sources.list file:
    netselect, netselect-apt
    Debian worldwide mirror sites:
    http://www.debian.org/mirror/mirrors_full
    Generate fastest sources.list selection for sid:
    netselect-apt sid

    Howto find out which package a given file belongs to

    dpkg -S stdio.h
    and/or
    apt-file search filename
    To see all the files that package contains:
    apt-file list packagename

    List installed packages

    COLUMNS=132 dpkg -l

    Install packages on demand

    auto-apt run ./configure

    Compile deb package sources

    Get package:
    apt-get source packagename
    Compile package:
    apt-get -b source packagename
    Install then package:
    dpkg -i file.deb

    Reference tutorial: [1].

    Find out what traffic is hammering your computer

    See ports and bytes with
    iptraf
    Iptraf e.g. in the open WLAN network shows all the traffic so it might be necessary to add filters e.g. to show only traffic to/from your computer. Watch the detailed traffic with
    wireshark.

    Is your DNS lookup not working

    It is configured in:
    /etc/resolv.conf
    You might need to fix that for chroot:ed versions especially.

    Firefox to work in chroot

    First install some sound packages and flash:
    apt-get install alsa-base alsa-oss alsa-utils flashplugin-nonfree
    Then make sure that your /etc/iceweasel/iceweaselrc file has either line ICEWEASEL_DSP="auto" or ICEWEASEL_DSP="aoss". Then it should work.

    Configure firefox options

    Write about:config to the address bar and be happy.

    Firefox save as... not working

    Try to delete this file from your local firefox settings.
    downloads.rdf

    Lossless jpeg manipulation

    apt-get install libjpeg-progs
    For example rotation:
    jpegtran -rot 90 -trim foo.jpg > foo90.jpg

    Compare directories

    To see what files differ in the given directories, command:
    diff -r --brief dir1 dir2

    Take screenshot of the desktop

    After installing ImageMagick:
    import screenshot.jpg

    Check last logins to the system

    As a root:
    last

    Controlling X-server connection rights

    xhost - server access control program for X
    Grant rights to all hosts:
    xhost +

    Howto configure network to work manually

    You need to know IP, MASK, GATEWAY, DNS, HOSTNAME and DOMAIN:
    ifconfig eth0 IP netmask MASK up
    route add -net default gw GATEWAY netmask 0.0.0.0 eth0
    hostname HOSTNAME
    echo ``domain DOMAIN'' >/etc/resolv.conf
    echo "nameserver DNS" >>/etc/resolv.conf
    

    Tools to check ethernet speed

    mii-tool, ethtool, iperf
    :~# mii-tool -v eth0
    eth0: negotiated 100baseTx-FD flow-control, link ok
      product info: vendor 00:07:32, model 17 rev 3
      basic mode:   autonegotiation enabled
      basic status: autonegotiation complete, link ok
      capabilities: 100baseTx-FD 100baseTx-HD 10baseT-FD 10baseT-HD
      advertising:  100baseTx-FD 100baseTx-HD 10baseT-FD 10baseT-HD flow-control
      link partner: 100baseTx-FD 100baseTx-HD 10baseT-FD 10baseT-HD flow-control
    
    :~# ethtool eth0
    Settings for eth0:
            Supported ports: [ TP ]
            Supported link modes:   10baseT/Half 10baseT/Full 
                                    100baseT/Half 100baseT/Full 
                                    1000baseT/Full 
            Supports auto-negotiation: Yes
            Advertised link modes:  10baseT/Half 10baseT/Full 
                                    100baseT/Half 100baseT/Full 
                                    1000baseT/Full 
            Advertised auto-negotiation: Yes
            Speed: 100Mb/s
            Duplex: Full
            Port: Twisted Pair
            PHYAD: 0
            Transceiver: internal
            Auto-negotiation: on
            Supports Wake-on: pumbg
            Wake-on: g
            Current message level: 0x00000033 (51)
            Link detected: yes
    
    ipref measures the traffic between two comptures. Other computer is server and the other computer is client.

    To find out what libraries program needs

    Check by running:
    ldd programname
    To add a new dynamic library:
    ldconfig -n /path/of/new/lib
    For more info, man ld.so

    Which user I am and which groups do I belong

    Use this command:
    id

    Creating a CD

    AUDIO:
    To put on an audio CD all the *.wav files which are in the current directory:
    cdrecord dev=x,y,z -pad speed=0 -audio *.wav

    DATA:
    mkisofs -o cd.iso -J -R /tmp/mycd
    cdrecord -scanbus
    cdrecord dev=x,y,z -multi speed=0 -data cd.iso
    

    CD to wav

    Whole CD:
    cdparanoia -B
    Just one track:
    cdparanoia -w track_number file.wav

    wav to mp3 or ogg

    lame file.wav file.mp3
    oggenc file.wav -o file.ogg

    Split a file to several floppies

    Split:
    split -b1m file
    Reassemble
    cat x* >file

    Converting text files from DOS, Windows, Mac...

    Adopted from: http://www.pallier.org/ressources/linux_howtos/linux_howto.html
    Texts files under unix, mac, dos and windows use differents codes for end-of-lines and accentuated characters. Linux typically uses the Latin-1 character encoding. The ``recode'' program converts from one format to the other (info recode).
    To convert file.txt coming
    from a Macintosh: recode mac..latin1 file.txt
    To convert from DOS codepage 850 to unix latin1:
    recode 850..latin1 file.txt
    To just remove ^ M:
    recode l1/crlf..l1 file.txt
    To convert from Windows codepage 1250 to unix latin 1 (iso-8859-1): 
    recode ms-ee..l1 file.txt
    

    Printing

    To put the 'file.ps' into the printing queue of the printer known as 'printername':
    lpr -P printername file.ps

    The printers can be attached to the local system via a parallel, serial, or USB port, or available over the network. To get a list of available printers:
    lpstat -p -d

    To check the status of all printers:

    lpstat -a

    To remove a printing job:
    lprm job-id

    Vector graphics

    Maybe use 'xfig' to construct complex graphics. Yet the
    ergonomy of xfig is very different from what users expect nowadays.
    You can try 'sketch', 'dia' or OpenOffice draw. Or inkscape.
    For mathematical plots, bar plots..., I use the data analysis package
    R. For simple things, gnuplot may also help.
    

    Scan for badblocks

    This is destructive, so do it only for empty partitions:
    badblocks -v -w -p 3 -o /root/badblocks.list.txt /dev/sda1

    md5 check file for files in directory

    To create md5 check file, execute:
    find . -type f 2>/dev/null -exec md5sum {} \; >test.md5
    Check the directory:
    md5sum -c ../picsall.test.md5
    source: http://www.brunolinux.com/

    Need a secure kernel

    http://www.grsecurity.net/

    Ping HW address

    iputils-arping
    arping
    Usage example:
    ~# arping 00:0D:F0:14:B8:22
    ARPING 00:0D:F0:14:B8:22
    60 bytes from 192.168.1.1 (00:0d:f0:14:b8:22): icmp_seq=0 time=434.160 usec
    60 bytes from 192.168.1.1 (00:0d:f0:14:b8:22): icmp_seq=1 time=425.100 usec
    

    WLAN WEP in Linux

    iwconfig wlan0 essid myHost
    iwconfig wlan0 key s:passphrase
    iwconfig wlan0 up
    dhclient wlan0

    And all should work.

    WLAN WPA in linux

    WPA is more secure than WEP and should be prefered.
    Install wpasupplicant package:
    wpasupplicant - Client support for WPA and WPA2 (IEEE 802.11i)

    Manual configuration:
    1) Generate passphrase:
    wpa_passphrase myHost myPassPhrase
    network={
            ssid="myHost"
            #psk="myPassPhrase"
            psk=0d0800c35ab28ba9ed417863ade1ddc3ec38b9490ed633f99081cae4ac65c39e
    }
    
    Insert it into /etc/wpa_supplicant/wpa_supplicant.conf. 2) Run these commands:
    iwconfig wlan0 essid YebbeY
    ifup wlan0
    wpa_supplicant -iwlan0 -c/etc/wpa_supplicant/wpa_supplicant.conf
    3) Connection is established so get a IP address:
    dhclient wlan0

    Automatic configuration:
    1) Add these to /etc/network/interfaces
    auto wlan0
    iface wlan0 inet dhcp
    up wpa_supplicant -iwlan0 -c/etc/wpa_supplicant/wpa_supplicant.conf -Bw
    down killall wpa_supplic
    

    Which distribution are you running?

    lsb_release -a

    Logical volume management

    Check lvm.

    Check that all files are unique, no duplicates

    fdupes -r /dir

    Check WLAN signal strength or quality

    wavemon

    Update GRUB menu

    update-grub

    Open remote desktop connection

    rdesktop -f -a 24 -r disk:homedir=/home/ssheikki rondo.tkk.fi

    Remove default gw from eth0 to enable connection through wlan0

    List routes:
    route -n
    Delete route that you do not want to use:
    route del default gw 130.233.120.254

    Howto grip owner of other user screen

    Check what is your console:
    w
    Change full rights to the console:
    chmod 777 /dev/pts/32
    Change to the user with su:
    su username
    Get the screen:
    screen -DDRR

    Howto mount disk image with partitions

    Check the number of cylinders from the original disk (e.g. with cfdisk) and check the image properities:
    fdisk -l -u -C <#cylinders> dd.image

    Mount the image with right byte-offset:
    mount -o loop,offset=<start*units> dd.image mnt

    Howto monitor ip-traffic

    iptraf

    Adding firewall (or anyother script) to start automatically

    ln -s ../init.d/connect_router rc2.d/S20connect_router
    Do the same for levels 2-5. S means Start so you might want to have also 0-1 and 6 run levels a script with K20connect_router (to Kill).

    This is of course wrong way to do it. How it should be done:
    update-rc.d connect_router defaults
    update-rc.d connect_router start 99 2 3 4 5 . stop 99 0 1 6 .
    To test the script, you can use:
    invoke-rc.d

    Playing with network interfaces

    apt-cache show netenv

    How to change default route? In the ifconfig there is defined what to do before and after interface is put down, e.g. postdown etc.

    Search files from packages

    How to findout in which package file belongs to:
    dpkg -S filename

    How to findout which files belong to a packet:
    dpkg -L packetname

    Usb2serial configuration

    apt-get install usrp
    To test:
    cat /dev/ttyUSB0

    aclocal: not found

    Install automake.
    autoconf -- to get autoheader,autoconf
    libtool -- to get libtool, libtoolize 
    

    Matlab is missing a function

    file exchange toolbox: fxsearch

    Howto find out what application is using your port

    Who is using port XXX:
    fuser -n tcp XXX
    List all ports being used:
    netstat -nlp

    IRC in ipv6 with irssi

    Set the ipv6 hostname that you have:
    /set hostname pikku.giraf.fi
    Connect to the server that supports ipv6:
    /server -6 irc.cc.tut.fi

    SMART IDE hard disk info

    aptitude install smartmontools
    Show device info (does not work for USB disks):
    smartctl -a /dev/sda

    Joomla transfer to new server

    1) Download latest stable version of joomla and install it
    2) Load the mysqldump of your old site to the new database.
    3) Copy files that you are missing from the old system (modules/mod_header_image*, images/*, templates/?*)

    Installing kernel from ubuntu/debian package

    Install kernel:
    apt-get source linux-image-2.6.20-16-386
    Install sources (needed e.g. to compile truecrypt):
    apt-get source linux-image-2.6.20-16-386

    Hostname change

    Edit
    /etc/hostname
    and set
    hostname [new_hostname]

    Inventory computers

    Install these packages:
    aptitude install dmidecode libio-zlib-perl libxml-simple-perl libnet-ip-perl libnet-ssleay-perl libwww-perl
    Install:
    OCSNG_LINUX_AGENT_1.01_with_require.tar.gz
    http://www.ocsinventory-ng.org/

    Look strings inside binary file (e.g. jpg)

    strings kuva.jpg | less

    charset data for locale

    luit searches locales from:
    /usr/lib/X11/locale
    but they are actually located in:
    /usr/share/X11/locale

    xterm encoding

    xterm -en iso-8859-15

    change sender in pine

    Select SETUP and C (Config). Customize the line:
    customized-hdrs=From: email.address@somewhere.the

    Check web sites links validity

    linkchecker --no-warnings http://www.spacemaster.se/

    Adding crontab a new time

    Crontab has five fields to set the time:
    field           allowed values
    -----		--------------
    minute		0-59
    hour		0-23
    day of month	1-31
    month	        1-12 (or names, see below)
    day of week	0-7 (0 or 7 is Sun, or use names)
    
    To add a crontab for a user, write "crontab -e":
    1 1 * * Fri komento

    Add a library path

    If you miss a library when starting a program, find it with locate (e.g. locate libode.so):
    export LD_LIBRARY_PATH=/usr/local/lib

    Module assist usage

    Go to the directory where your kernel module is (e.g. fglrx) and write:
    sudo module-assistant prepare
    sudo module-assistant update
    sudo module-assistant build fglrx
    sudo module-assistant install fglrx
    sudo depmod -a
    

    Usefull regexps

    Match start of the line == ^, e.g.:
    grep "^From " 2007_09_27_sm | wc
    More:
    http://www.regular-expressions.info/reference.html

    Make password in linux

    makepasswd --char=10

    You program can not file binary, headers or libs

    Your header files are not in the path (CPATH):
    E.g if you use different version of ODE libarary:
    export CPATH=/usr/share/ode-0.9double/include:$CPATH

    Your binaries are not in the path:
    export PATH=/usr/local/bin:$PATH
    Your libraries are not in the path while linking your code:
    export LIBRARY_PATH=/usr/local/lib:$LIBRARY_PATH
    Your libraries are not in the path while executing your code:
    export LD_LIBRARY_PATH=/usr/local/lib:$LD_LIBRARY_PATH

    Define environment variables with script

    source ./scriptname

    Monitor the network traffic

    wireshark, ethereal

    Changing bootable disk with grub

    Install grub to the new disk (/dev/hdb, grub partition mounted at /mnt/newdisk):
    grub-install --root-directory=/mnt/newdisk/ /dev/hdb
    Change /etc/fstab and /boot/grub/menu.lst to match the new configuration.

    Harddisk volume id (UUID)

    vol_id /dev/hda1

    ext2 to ext3

    tune2fs -j /dev/hdx1
    Change /etc/fstab and reboot.

    VMware in Linux

    1. Download WMware Server, vmware.
    2. Install it (requires kernel headers).
    3. Run it with command: vmware.

    Recover data from a broken ext2/ext3 hard disk

    1. Install ddrescue and testdisk tools
    2. Read data with all offsets with ddrescue
    3. Uset testdisk to find superblock backups from the disk (your primary superblock in the start of the partition is probably broken and you can not mount the partition)
    4. Do with 'losetup' tools a 'loopback device' of the partition image with disk offset that disregards the partition table
    * losetup -o 32256 /dev/loop0 hdc.ddrescue.bin
    5. Use a found superblock backup (found with testdisk) and filesystem repair to fix the disk
    * fsck.ext3 -b 32768 -B 4096 /dev/loop0
    6. After checking, mount the loopback partition and copy your data to safety.

    Sign and crypt your emails

    Install firegpg and kgpg.

    Generate comics web page

    dailystrips - view web comic strips more conveniently

    Using dump/restore

    First edit /etc/fstab to include "1" to the places that you want to backup. Otherwise keep/set to zero the column dump.
    Create a zero level dump first:
    dump 0uf dump_level0.dump /
    You can list the contents of the dump:
    rrestore -tf 2007_08_29_yebbey_l0.dump

    Modify SQL table

    Check current settings:
    describe events;

    Dump/restore one mesql database

    Dump:
    mysqldump [databasename] -p -u root | gzip -c > mysql_dump.gz
    Restore:
    gzip -cd mysql_dump.gz | mysql -u root -p

    Add a new user for mysql table

    Add without DROP/CREATE priviledges:
    grant INSERT,DELETE,UPDATE,SELECT on mydatabasename.* to myusername@localhost;
    Add also password:
    set password for myusername@localhost = password('mysecretpassword');

    Dump just data/structure

    Just structure:
    mysqldump -d --compact -u root -p yourdatabase > dump
    Just data:
    mysqldump -p -u root yourdatabase --no-create-info > dump
    Just data, one insert per row:
    mysqldump -p -u root yourdatabase --extended-insert=FALSE --no-create-info > dump

    Get more info about a file

    Display file or file system status:
    stat filename.end
    Determine file type:
    file filename.end

    Make a php upload page

    Configure /etc/php5/apache2/php.ini:
    1. upload_max_filesize
    2. max_execution_time
    3. max_input_time
    4. post_max_size

    Pairing cellphone and laptop with bluetooth

    hcitool scan
    mknod /dev/rfcomm0 c 216 0
    rfcomm bind 0 1
    passkey-agent --default /etc/bluetooth/feed-bin.sh
    You can try the connection with e.g. konqueror, write:
    bluetooth:///

    Send a sms from a command line

    gnokii --sendsms +358504137550 < message.txt

    Document search engine

    Creates a google type of document search engine, includes pdf:s, docs and text files for example.
    SWISH

    Make password

    makepasswd

    Multipointer operating system

    Use mpx live cd and you will have operating system that supports e.g. two mouse pointers.
    http://wearables.unisa.edu.au/mpx/
    http://wearables.unisa.edu.au/mpx/?q=node/51
    Boots up usually directly and also programs which are not designed for multipointing work at least adequately.

    Load kernel module with specific parameters

    For example acer_acpi module with wireless=1 parameter.
    echo "options acer_acpi wireless=1" > /etc/modprobe.d/acer_acpi

    Automatic wireless wep network setup

    auto wlan0
    iface wlan0 inet dhcp
    wireless-essid <youressid>
    wireless-key s:<yourwepkey> open
    

    Easy to use firewall using iptables

    Uncomplicated FireWall:
    ufw enable
    ufw logging on
    sudo ufw allow 53/tcp
    sudo ufw allow ssh
    

    Continue partially send scp file

    rsync --partial file.tar.bz2 user@machine.com:

    vmware in ubuntu hardy heron howto

    Packages required:
    aptitude install build-essential linux-kernel-devel linux-headers-generic xinetd
    Run install (with normal settings):
    ./vmware-install.pl
    Copy few files to right places:
    cp /lib/libgcc_s.so.1 /usr/lib/vmware/lib/libgcc_s.so.1
    cp /usr/lib/libpng12.so.0 /usr/lib/vmware/lib/libpng12.so.0
    Locate your virtual machine and edit the vmx file in this folder and add the following line:
    svga.maxWidth = "1280"
    svga.maxHeight = "800"
    
    You can also try if above does not work:
    sudo gedit ~/vmware/preferences
    - Now where it says:
    pref.autoFitFullScreen = "fitHostToGuest"
    change it to:
    pref.autoFitFullScreen = "fitGuestToHost"
    

    vmware USB enable

    Edit:
    /etc/init.d/mountdevsubfs.sh
    Uncomment lines 42-45 starting with #mkdir p....

    Remove duplicate lines

    Remove duplicate acronyms (-f == ignore case)
    sort -f acronyms_unsorted.tex | uniq > acronyms.tex
    Display duplicate acronyms:
    sort acronyms.tex | uniq -dc

    Install fglrx in ubuntu

    Install restricted modules:
    aptitude install linux-restricted-modules-`uname -r`
    sudo dpkg-reconfigure -phigh linux-restricted-modules-`uname -r`
    sudo insmod /lib/modules/`uname -r`/volatile/fglrx.ko
    

    Boot without password

    At grub-boot loader screen. Select Recovery mode the version of the kernel that you wish to boot and type e for edit. Select the line that starts with kernel and type e to edit the line.

    Go to the end of the line and type init=/bin/bash as a separate one word (press the spacebar and then type init=/bin/bash). Press enter key to exit edit mode.

    Play encrypted dvds

    locate install-css.sh
    sudo install-css.sh
    with vlc, choose disc and dvd and volaa.

    Save your sound mixer settings in reboot

    aptitude install alsa-utils
    alsactl store
    Automatic version:
    # load/unload the volume settings on startup/shutdown
       install snd-driver-name /sbin/modprobe \
        --ignore-install snd-driver-name;/usr/sbin/alsactl restore
       remove snd-driver-name /usr/sbin/alsactl \
        store;/sbin/modprobe --ignore-remove -r snd-driver-name
    

    for loop in bash

    for i in `seq 1 9`;do wget http://www.picsurl.com/file$i.jpg;done

    Check disk health status

    smartctl -H /dev/hda
    smartctl -l error /dev/hda,
    

    Capture video with sound

    Setup the mixer settings e.g. using gnome-alsamixer. Make sure that "Mix" and "Capture, Rec" are selected.
    recordmydesktop -x 100 -y 100 -width 300 -height 300 -o output.ogg
    End the recording by Ctrl+C.
    3D accelerated parts require that --full-shots parameter is used.
    recordmydesktop -x 150 -y 120 -width 700 -height 500 -fps 30 --full-shots -o output.ogg

    Make windows compatible avi

    Does not work as PDF embedded movie:
    mencoder input.avi -o out.avi -ovc lavc -lavcopts vcodec=msmpeg4v2 -oac copy

    Compare two md5sum files

    Return the lines that are mentioned in first file but not in second.
    sort a >aa; sort b >bb; diff aa bb | sed -n '/^
    Don't care if file is located in different directory:
    sort a | sed -n 's/\/.*\///p' > aa;sort b | sed -n 's/\/.*\///p' > bb;
    diff aa bb | sed -n '/^ Also this works instead of 's/\/.*\///p':
    's#\.\?/.*/##'

    Compare just the md5sums:
    sort a | sed -n 's/ .*//p' > aa;sort b | sed -n 's/ .*//p' > bb;
    diff aa bb | sed -n '/^Match lines starting with k and print sed -n '/^k/p'

    Add space before string

    ls -1 | sed -n 's/^./ \0/p'

    Kill all processes accessing a mounted partition

    fuser -km /home/ssheikki/mnt

    Debugging functions in matlab/octave

    Add a breakpoint in function "test.m" in line 28:
    dbstop test 28
    Note! Returns the line where the breakpoint was actually set.
    Go to the next line in debug mode:
    dbnext

    Download all packages installed to your system

    dpkg --get-selections | sed -n 's/..install$//p' | perl -pi -e 's/^\s*(\S.*\S)[ \t]*$/$1 /' | perl -p -e 's/\n//' > packages
    Edit aptitude download in the start of the file of packages.
    sh packages

    Replace space with underline in filenames

    rename "s/ /_/g" *

    Add prefix in the front of the name

    rename 's/(.+)/prefix-$1/' *.jpg

    Remove prefix from the front of the name

    for f in prefix-*; do mv "$f" "${f#prefix-}"; done

    Run firefox, etc in 32bit mode

    dchroot -c ia32 -d firefox
    dchroot -c ia32 -d gmplayer

    Sort according to second column

    sort +1 -2 filename.txt

    Install text to speech system

    aptitude install festival
    echo "Speech to text. It works?" | festival --tts
    You might want to install some festvox-* packages:
    echo "Kuka mina olen?" | festival --tts --language spanish
    echo "Kuka mina olen?" | festival --tts --language finnish

    Get info about your soundcard

    Cards recognized:
    aplay -l
    asoundconf list
    Default clock etc. parameters:
    modinfo module_name, e.g. modinfo snd_atiixp

    Compile kernel with initrd

    make-kpkg --initrd --append-to-version=-custom kernel_image kernel_headers
    See also:
    update-initramfs

    Manage your iPOD in linux

    gtkpod

    Run oss designed program using alsa

    aoss program_name

    arping in debian/ubuntu

    aptitude install arping
    arping 00:0a:e4:e1:04:b9

    Capture mms stream from net

    mplayer mms://example.con/example12a.wmv -dumpstream -dumpfile example12a.wmv

    Remove pages from pdf files

    aptitude install pdftk
    Remove pages 1,2 and 10-14:
    pdftk input.pdf cat 3-9 15-end output output.pdf

    Combine pages from several pdfs into one

    pdftk A=input1.pdf B=input2.pdf cat B1-2 A3-end output combined.pdf

    Compare contents of two directories

    To see differencies in the contents of two directories:
    diff -rq dirA dirB
    Note, the comparison is recursive, so also contents of subdirectories are compared.

    Howto install display drivers

    Get envy.deb
    Install it... and run "envy -t".

    Config alsa

    You might get error like this:
    alsamixer: function snd_ctl_open failed for default: No such device
    alsaconf

    Compile programs faster

    http://ccache.samba.org/

    Display planet as background

    xplanet -latitude 40

    Install Nvidia/ati drivers

    aptitude install envyng-core
    envyng -t

    Trace what system calls a program does

    strace -eopen alsamixer

    Update PCI ID list

    update-pciids

    Missing initram causes kernel panic

    Notice something like VFS mount failed
    update-initramfs -u -k 2.6.27.1

    Change default audio card

    asoundconf-gtk

    Show progress of suspended dd

    kill -SIGUSR1 %1

    Protect yourserver from attacks

    Software:
    - aide (files)
    - tiger (services)
    - rootkit (rootkits)
    

    Loading irssi scripts

    /script load scriptassist
    /scriptassist install morse
    etc.

    Install additional packages to debian-stable - backports

    Add this line to /etc/apt/sources.list:
    deb http://www.backports.org/debian etch-backports main contrib non-free

    John the Ripper

    Check that passwords are strong:
    unshadow /etc/passwd /etc/shadow > johnfile
    john johnfile

    Password strength check in login

    aptitude install libpam-cracklib
    Add to the services you want in /etc/pam.d/(e.g. common-password, ssh and login) this:
    # Be sure to install libpam-cracklib first or you will not be able to log in
    password required         pam_cracklib.so retry=3 minlen=10 difok=3
    password required         pam_unix.so use_authtok nullok md5 remember=3
    

    Convert dos/windows files Unix format

    Basically change the carriage return:
    aptitude install tofrodos
    dos2unix/fromdos and unix2dos/todos commands.
    dos2unix makefile.sh

    Prevent hacker to bombing your computer

    aptitude install fail2ban
    OR
    aptitude install denyhosts
    "Monitors log files (e.g. /var/log/auth.log, /var/log/apache/access.log) and temporarily or persistently bans failure-prone addresses."
    In jail.conf, change:
    # Default action to take: ban & send an e-mail with whois report 
    # and relevant log lines to the destemail.
    action = iptables[name=%(__name__)s, port=%(port)s]
             mail-whois-lines[name=%(__name__)s, dest=%(destemail)s, logpath=%(logpath)s]
    
    and
    [ssh]
    enabled = true
    port    = ssh
    filter  = sshd
    logpath  = /var/log/auth.log
    maxretry = 12
    bantime = 3600
    findtime = 600
    ignoreip = 130.233.120.0/24
    

    You wanna track bugs?

    Install mantis:
    http://www.mantisbt.org/

    Want to send email outside in Linux

    Configure global internet delivery:
    dpkg-reconfigure exim4-config
    Test e.g. with:
    mail -v -s Test name@youremail.com

    Make a package from source code that you can install/deinstall

    aptitude install checkinstall
    checkinstall make install

    Print two(or more) pages per sheet pdf

    psnup -2 koe.ps > output.ps

    Convert audio files (e.g. wav) and adding effects with sox

    aptitude install libsox-fmt-all
    Play file with effect:
    play input.wav chorus 0.5 0.9 50 0.4 0.25 2 -t 60 0.32 0.4 2.3 -t 40 0.3 0.3 1.3 -s Convert file:
    sox input.wav output.wav chorus 0.5 0.9 50 0.4 0.25 2 -t 60 0.32 0.4 2.3 -t 40 0.3 0.3 1.3 -s

    Building and testing software projects automatically

    Hudson, versionin system and cron compatible java based open-source software:
    https://hudson.dev.java.net/

    Creating test cases and plans

    Test Case Management tool:
    http://testlink.org/

    Skype etc., howto install

    Check Medibuntu:
    https://help.ubuntu.com/community/Medibuntu

    Configure nvidia xorg.conf

    gksu nvidia-settings

    Wireless network-manager in Linux

    network-manger and cnetworkmanager.

    Use calibrated joystick

    aptitude install joystick
    Calibrate:
    jscal -c /dev/input/js0

    Festival is not working

    If festival returns:
    Linux: can't open /dev/dsp
    Add in the end of /usr/share/festival/festival.scm
    (Parameter.set 'Audio_Method 'Audio_Command)
    (Parameter.set 'Audio_Command "aplay -q -c 1 -t raw -f s16 -r $SR $FILE")
    

    Print status of files in cvs like with svn status

    alias cvsstatus="cvs status * 2>&1 | grep Status: 2>&1 | grep -v \"Up-to-date\" 2>&1 | grep -v \"cvs add\""

    Check user idle time in irssi

    wii the_username

    Play videos with multithreaded mplayer

    alias mplayer='mplayer -lavdopts threads=2 -vo gl2'
    and play normally with mplayer.

    See what is happening in system level on program

    strace - trace system calls and signals
    Example:
    strace whois tkk.fi
    strace whois tkk.fi 2>&1 | grep send

    Recursive ftp upload

    aptitude install ncftp
    mput -R *

    LAN over bluetooth rfcomm/PPP

    Add in /etc/ppp/peers/dun this:
    115200
    proxyarp
    192.168.168.1:192.168.168.50
    local
    ms-dns 130.233.224.1
    noauth
    debug
    ktune
    
    Start the service daemon:
    dund --listen --persist --msdun call dun
    Note! You dont need to even create bond with the device.

    Load additional programs in IRSSI

    /load ~/irssi/scriptassist.pl
    /scriptassist search [program_name]

    Use bluetooth headset in Linux

    Insert this in .asoundrc file:
    pcm.!default {
            type bluetooth
            device "00:0D:44:0A:6D:C3"
            profile "auto"
    }
    

    Stop/continue, abort running process

    Stop:
    kill -STOP <PIDNUM>
    Continue:
    kill -CONT <PIDNUM>
    Abort:
    kill -ABRT <PIDNUM>
    For more commans, see "man kill".
    

    Netcat echo server

    Server that echoes back all what it receives:
    mkfifo responsepipe && cat responsepipe | nc -l -p 5000 > responsepipe
    Print also what received:
    cat responsepipe | nc -l -p 5000 | tee responsepipe
    Print and log what also received:
    cat responsepipe | nc -l -p 5000 | tee responsepipe responses.log

    Save joystick calibration

    After calibration, get settings:
    jscal -p /dev/input/js0
    Then write settings later:
    jscal -s 6,1,1,126,126,12484989,9256113,1,0,118,118,11422437,6547006,1,0,130,131,6972137,7894920,1,0,108,108,7254791,7354172,1,0,0,0,5

    Dynamic (execution) and static (linking) library paths

    Dynamic library path are managed using ldconfig or LD_LIBRARY_PATH variable.
    Static libraries are managed using -L compiler flag.

    Show RS232 port settings

    stty -a -F /dev/ttyUSB0
    Note! Does not seem to show RTS bit state.

    Strip libraries for release

    You can strip only debug info:
    strip --strip-debug libExample.a

    Request web page with random address from wanted ip using nc

    nc 130.233.125.68 80
    GET /yourstuffmorehere HTTP/1.1
    Host: spacemaster.eu
    

    Ad-hoc mode in client computer

    iwconfig wlan0 essid SERVER_ESSID mode Ad-hoc
    ifconfig wlan0 192.168.0.11 netmask 255.255.255.0 up
    route add -net default gw SERVER_IP netmask 0.0.0.0 wlan0

    (Re)Install grub2

    Mount your linux partition to /mnt and:
    sudo mount --bind /dev /mnt/dev
    sudo mount --bind /proc /mnt/proc
    sudo mount --bind /sys /mnt/sys
    dpkg-reconfigure grub-pc
    Unmount everything and reboot.

    Add manually Windows to grub2

    Edit /etc/grub.d/40_custom and add:
    menuentry "Windows" {
    insmod ntfs
    set root=(hd0,1)
    search --no-floppy --fs-uuid --set YOUR_UUID_FROM_DEV_DISK_BY_UUID
    chainloader +1
    }
    

    Taking time with c/c++

    #include <sys/time.h>
    struct timespec start, stop;
    clock_gettime(CLOCK_REALTIME,&start);
    clock_gettime(CLOCK_REALTIME,&stop);
    double duration= (stop.tv_sec - start.tv_sec) + (double)(stop.tv_nsec - start.tv_nsec) / 1000000000.0;
    printf("time elapsed = %.9f s", duration);
    g++ -lm -Wall -lrt main.cpp
    

    Piper errors or stderr e.g. to grep

    yourprogram 2>&1 | grep -v "warning that and that"

    x264 video encoding in Linux

    Install ffmpeg:
    http://ubuntuforums.org/showthread.php?t=786095
    Encode eg. p3140032.avi to p3140032.mp4:
    ffmpeg -i p3140032.avi -pass 1 -vcodec libx264 -vpre fast_firstpass -b 512k -bt 512k -threads 0 -f rawvideo -an -y /dev/null
    ffmpeg -i p3140032.avi -pass 2 -acodec libfaac -ac 2 -vcodec libx264 -vpre fast -threads 0 output.mp4

    Find out information about video file

    file
    avinfo
    ogminfo, MP4info, mkvinfo (or mkvmerge with the "-i" switch)

    Check what formats and codecs ffmpeg supports

    ffmpeg -formats
    Top choises:
     DE avi             AVI format
     DE matroska        Matroska file format
      E mp4             MP4 format
    
    ffmpeg -codecs
    Top choises:
     D V D  h264            H.264 / AVC / MPEG-4 AVC / MPEG-4 part 10
      EA    libfaac         libfaac AAC (Advanced Audio Codec)
      EA    libmp3lame      libmp3lame MP3 (MPEG audio layer 3)
      EV    libx264         libx264 H.264 / AVC / MPEG-4 AVC / MPEG-4 part 10
     DEV D  mjpeg           MJPEG (Motion JPEG)
     DEVSDT mpeg1video      MPEG-1 video
     DEVSD  msmpeg4         MPEG-4 part 2 Microsoft variant version 3
     DEV D  qtrle           QuickTime Animation (RLE) video
     DEA    vorbis          Vorbis
     DEVSD  wmv2            Windows Media Video 8
    

    Digital camera video encoding howto (H.264/MPEG-4 AVC + Vorbis ogg)

    ffmpeg -y -i $inputvid -b 1.5M -bt 4M -vcodec libx264 -pass 1 -vpre fastfirstpass -an $inputvid.mkv
    ffmpeg -y -i $inputvid -b 1.5M -bt 4M -vcodec libx264 -pass 2 -vpre hq -acodec vorbis -aq 60 -ac 2 $inputvid.mkv

    Start several programs into one screen

    Lets assume there is test.sh script with takes output file as parameter:
    screen -d -m -t test bash test.sh out1.txt
    screen -X screen -t test2 bash test.sh out2.txt -p 1
    screen -X screen -t test3 bash test.sh out3.txt -p 2
    OR
    In file run_screens:
    screen 1 bash test.sh out1.txt
    screen 2 bash test.sh out1.txt
    screen -d -m -c run_screens

    Count the lines of code and estimate price

    sloccount ~/project_name

    Get previous version of file for Subversion

    If current version of the file is 7919:
    svn update file.hpp -r 7918

    Configure cups printers

    Go to:
    http://localhost:631/

    Resize pager window in enlightement

    Press middle mouse button and alt on the pager window. Then just move the mouse to resize the window.

    Serial/RS232 communication in Linux

    cutecom

    Adjust wrong eps image bounding box

    epsffit

    Change the default wordlist e.g. for ispell

    sudo select-default-wordlist
    and ispellconfig
    In emacs: M-x ispell-change-dictionnary
    Then M+x and ispell should work for spell checking in emacs with wanted language.
    Make sure that you have the correct i-dictionaries installed, e.g. ibritish.

    Upgrade ubuntu to new version

    do-release-upgrade

    Disable network-manager and do things by hand

    /etc/init.d/network-manager stop
    ifup wlan0

    Compressing video to mpeg4 with default ubuntu codec

    ffmpeg -ss 13 -t 5 -y -i $input.avi -vcodec mpeg4 -b 1.5M -bt 4M -r 25 -ar 44100 output.mkv
    You might have audio sync problems, then follow this:
    http://www.kkoncepts.net/node/69
    So basically above would be:
    ffmpeg -ss 13 -t 187 -y -i $input.avi -itsoffset -14 -i $input.avi -map 1:0 -map 0:1 -b 1.5M -bt 4M -r 30 -ar 44100 $output.avi

    How to convert jpg, etc files to eps to be used in latex

    convert input.jpg output.eps2
    or
    http://www.pdflib.com/download/free-software/jpeg2ps/

    Join or append avi/video files in Linux

    mencoder -oac copy -ovc copy -o output.avi input1.avi input2.avi

    grep recursively text files only

    grep -R search_string --include '*.txt' *

    Check with arping if server is up or down

    arping -I wlan0 ipaddress.net
    Unicast reply from 192.168.100.13 [00:22:FA:0C:69:E0] 51.373ms
    Unicast reply from 192.168.100.15 [C8:97:9F:C3:85:84] 133.760ms

    Extract images from PDF document

    pdfimages -j foo.pdf images

    Flip your upside down web camera image

    Do these:
    echo -e "\n# libv4l PPA\ndeb http://ppa.launchpad.net/libv4l/ppa/ubuntu `lsb_release -c | awk '{print $2}'` main" | sudo tee -a /etc/apt/sources.list
    sudo apt-key adv --recv-keys --keyserver keyserver.ubuntu.com C3FFB4AA
    sudo aptitude update
    sudo aptitude install libv4l-0
    LD_PRELOAD=/usr/lib/libv4l/v4l1compat.so application_name
    

    Password for '(null)' GNOME keyring: failure when using svn

    Do:
    killall gnome-keyring-daemon
    rm -rf~/.gnome2/keyrings

    Find out which program is using your soundcard

    lsof | grep snd

    Show which version was edited and by whom in svn

    svn annotate filename.cpp

    List lines that are common in two files

    Sort the two files and then run:
    comm -1 -2 sorted_file1 sorted_file2

    Set default file mask for new files

    Set mask 755 for new files (777-022=755):
    umask 0022

    Take a screenshot with mplayer

    mplayer -vf screenshot yourvideo.avi
    Then press just "s" whenever you want a screenshot.
    Remember that with "." you can go frame by frame forward.

    Profile your program, check which part is taking the power

    compile with -pg switch when using gcc.
    Add also debugging symbols (-g) and remove optimization (-O3) etc.
    It creates a gmon.out file which contains profiling info.
    The gmon.out can be examined with gprof or kprof tools.
    Check also gcc parameter gcov and program with same name. Used for code coverage (to see what parts have been used).
    Note! gprof does not support threads!
    For that purpose, might be good to check profiler.

    Howto turn webcamera upside down

    LD_PRELOAD=/usr/lib/libv4l/v4l2convert.so firefox
    See also settings in:
    LD_PRELOAD=/usr/lib/libv4l/v4l2convert.so v4l2ucp
    v4l2ctrl

    How to setup a free video conferecing system

    For this you need a server that has three ports visible to the internet.
    Then check http://www.bigbluebutton.org/ and add repositories to your ubuntu and aptitude install. In 15 minutes you are good to go. The people joining the conference need only web browser to be available.

    Linux ext3 filesystem format options

    If we have 1TB disk and we use "mkfs.ext3 -m0 -T largefile /dev/sda1" to create it, we get:
    Block size 4kB, inode size 256B, and bytes-per-inode (-i) parameter ~1MB.
    /dev/sda1 976457320 204568 976252752 1% /home/ssheikki/mnt
    /dev/sda1 954k 11 954k 1% /home/ssheikki/mnt
    Instead, if we use "mkfs.ext3 -m0 /dev/sda1" to create it, we get:
    Block size 4kB, inode size 256B, and bytes-per-inode (-i) parameter ~16kB.
    /dev/sda1 961432072 204568 961227504 1% /home/ssheikki/mnt
    /dev/sda1 62M 11 62M 1% /home/ssheikki/mnt
    Difference is ~15GB, which is 1.5% of the disk space.
    Block size is the smallest block that can be taken my any file in the file system.
    Bytes-per-inode will in the end tell how many inodes will be created. So in order for your inodes to be enough, your average filesize should be this size.
    Inode size tells size of one inode. You can calculate the space that your inodes take with this formula (=disk size/bytes-per-inode*inode size).

    Check inode size of partition

    sudo tune2fs -l /dev/sda1 | grep Inode

    Ext2IFS compatible ext2/ext3 filesystem

    To get compatible filesystem with Ext2IFS, use:
    mkfs.ext3 -m0 -T largefile -I 128 /dev/sda1
    You will get:
    bytes-per-inode:          1MB
    Block size:               4096
    Inode size:               128
    
    Note that http://www.ext2fsd.com supports the larger 256 inode size!

    Configure firefox settings

    write in address bar about:config

    Check if some nameserver can direct your domain

    dig @a.ns.joker.com www.nokiaplanv.com

    Convert black and white bitmaps to vector graphics

    potrace

    Convert file to utf8 or latin1

    iconv -t ISO8859-1 -f UTF-8 parties.sql > parties2.sql

    Howto determine charset encoding

    file -i parties.sql
    enca -L none parties.sql

    Show processes that are using http 80

    sudo fuser -n tcp -v 80

    Learn about printf syntax

    man 3 printf
    printf("%*d", width, num);

    zenity --info --text="wat"

    Make popups in Linux GUI (KDE?)

    Rescue a partition that has bad sectors

    There is two programs dd_rescue and ddrescue. It seems that ddrescue was able to recover all but 8kB and dd_rescue all but 127kB. Install ddrescue like this:
    aptitude install gddrescue
    and run it like this:
    sudo ddrescue -v -r -1 /dev/sdb1 sdb1.ddrescue /home/ssheikki/ddrescue_log.txt

    Encfs howto

    aptitude install encfs fuse-utils
    modprobe fuse
    adduser ssheikki fuse
    encfs /home/ssheikki/encfs /home/ssheikki/mnt
    fusermount -u /home/ssheikki/mnt

    Adding subtitles to a video

    aptitude install gnome-subtitles
    Create test.srts omething like this:
    1
    00:00:00,000 --> 00:02:57,040
                                3x
    
    mencoder -sub test.srt -subpos 0 -subfont-text-scale 7 -ovc lavc -lavcopts vcodec=mpeg4:vbitrate=800:vhq -oac copy -o test2.avi test.avi
    If you want to hardcode two subtitles, use first a raw avi and later encode:
    sudo mencoder -sub test.srt -subpos 0 -subfont-text-scale 7 -ovc raw -vf format=yuy2 -oac copy -o mnt/test2.avi test.avi
    sudo mencoder -sub test2.srt -subpos 90 -ovc lavc -lavcopts vcodec=mpeg4:vbitrate=800 -oac copy -o test3.avi mnt/test2.avi

    Add character on start of each line

    If you want to add "> " to start of each line:
    sed -i -e 's_.*_> &_' file_to_be_processed.txt

    List files that were recently modified

    In last 30mins:
    find / -mmin -30

    Delete files that have size "1"

    Check first that all files are correct with this (note, shows all files if no hits found... behaviour of rm is different of course):
    find . -maxdepth "1" -type "f" -size "1" -print | xargs ls
    Then delete with this:
    find . -maxdepth "1" -type "f" -size "1" -print | xargs rm

    Extract audio from a video

    mplayer VIDEO.MTS -vc null -vo null -ao pcm:fast:file=output.wav

    Go in emacs to certain line

    Esc+x goto-line
    Esc-g g

    Add column to mysql table

    ALTER TABLE event_referer ADD COLUMN emailcrons tinyint NOT NULL DEFAULT '0' AFTER email;
    Result might be something like this (viewed with describe event_referer):
    +-------------+---------------------------+------+-----+---------------------+-----------------------------+
    | Field       | Type                      | Null | Key | Default             | Extra                       |
    +-------------+---------------------------+------+-----+---------------------+-----------------------------+
    | emailcrons  | tinyint(4)                | NO   |     | 0                   |                             |
    +-------------+---------------------------+------+-----+---------------------+-----------------------------+
    
    If somethings goes wrong, use DROP COLUMN:
    ALTER TABLE event_referer DROP COLUMN emailcrons;
    Note that size after tinyint is not "real size" but "mental reminder of how big the field should be":
    ALTER TABLE event_referer ADD COLUMN showanswers tinyint(3) DEFAULT '0';

    Check account password and other expiration details

    chage -l ssheikki

    Mp4 to avi conversion with ffmpeg

    ffmpeg -i ROB-11-002_video1.mp4 -vcodec mpeg4 -acodec ac3 -ar 48000 -ab 192k -ac 6 -sameq output.avi

    Resize ps/pdf document

    ps2pdf -sPAPERSIZE=a4 document.ps document.pdf

    Get own git repository

    git clone ssh://ssheikki@lakka.kapsi.fi/~/sites/ssheikki.kapsi.fi/secure-www/mielipiteet
    To use a short name:
    git remote add giraffi ssh://ssheikki@giraf.fi//var/www/mielipiteet.fi/html/mielipiteet_git
    git fetch giraffi
    git merge giraffi/master

    jpegs to avi

    sudo mencoder "mf://*.JPG" -mf fps=10 -o test.avi -ovc lavc -lavcopts vcodec=msmpeg4v2:vbitrate=800

    pine: How do I change my 'From:' line?

    From Pine's [M]AIN MENU, choose [S]etup, then [C]onfig. Move down to the customized-hdrs option. Press "A Add Value". Use the format:
    From: "My Real Name" <myusername@domain.fi>

    List size of all files (also hidden) in directory, but not ..

    du -sk .[!.]* *| sort -n

    Maven tips

    Build javadocs:
    mvn javadoc:javadoc OR mvn clean site

    Jpg/image to A4 pdf

    convert passport.jpg passport.pdf
    acoread passport.pdf
    Then just print the pdf as A4 into file.

    Extract ip address from ifconfig command

    ifconfig wlan0 | grep "inet addr:" | sed -n 's/.*addr:\([0-9.]*\) .*/\1/p'

    Encrypt/decrypt file

    openssl enc -md sha -bf-cfb -e -in tunnusluvut.png.enc -out tunnusluvut.png
    openssl enc -md sha -bf-cfb -d -in tunnusluvut.png.enc -out tunnusluvut.png

    Find open ports or what ports are process is using

    lsof | grep TCP

    Viewing CR2 files in linux

    f-spot view
    nautilus
    aptitude install gnome-raw-thumbnailer

    Rescale your video

    mencoder GOPR2132.MP4 -o out2.avi -ss 341 -endpos 30 -ovc lavc -lavcopts vcodec=msmpeg4v2 -vf scale=640:480 -oac copy

    svn admin dump/load

    Recommended way to serialise svn and transfer it to another place.
    bzcat ../ssheikki-svndump.bz2 | svnadmin load jeppe

    Howto create bootable USB disk

    Download image and config it to usb disk with:
    usb-creator-gtk

    Eclipse and Vaadin

    1) If something is screwed up
    * Select your project and press right key
    * Maven -> Update Project Configuration
    2) If you want to get maven project from svn
    * Select import in eclipse
    * write maven and select "existing maven projects"
    

    Host your nameservers with minimal fuss

    aptitude install maradns

    Check that your c-code does not have problems

    aptitude show splint

    List all files that are symbolic links

    find . -type l -print
    and delete them
    find . -type l -exec ls '{}' \;

    Repeat same command every X seconds

    watch -n 0.1 ls -laF /tmp

    Torrent client that supports magnet links

    qBittorrent (at least v.2.2.5)

    Interlaced 50fps HD HTS to avi or mp4

    Install latests ffmpeg:
    http://ubuntuforums.org/showpost.php?p=9868359&postcount=1289
    Requires to compile x264 also... and probably many others.

    With ffmpeg that supports filters:
    ffmpeg -i in.MTS -vf yadif=1 -acodec ac3 -ab 192k -vcodec mpeg4 -f mp4 -y -sameq out.mp4

    With x264 (which is the best option):
    ffmpeg -threads 2 -i input.MTS -vcodec libx264 -filter:v yadif -b 3000k -cmp +chroma -g 250 -partitions +parti4x4+partp8x8+partb8x8 -i_qfactor 0.71 -keyint_min 25 -b_strategy 1 -flags +loop -acodec libmp3lame -ar 48000 -ab 128k -ac 2 output.avi
    With more high quality presets:
    ffmpeg -threads 2 -i 00001.MTS -vcodec libx264 -filter:v yadif -b 3000k -cmp +chroma -g 250 -partitions +parti4x4+partp8x8+partb8x8 -i_qfactor 0.71 -keyint_min 25 -b_strategy 1 -flags +loop -preset slow -crf 22 -acodec libmp3lame -ar 48000 -ab 128k -ac 2 outputfile.avi

    Play interlaced video with mplayer

    mplayer -vf pp=lb 00003.MTS

    Make DVD iso file

    mkisofs -o dvd.iso -r -J -V "Name of the DVD" -publisher "Name Lastname" -graft-points "video.avi=/path/of/first.avi" "pictures=/path/of/pictures/" "README=/path/of/README"

    Burn a DVD without making iso image first (works in Linux and windows, but not with Mac?)

    growisofs -dvd-compat -speed=1 -Z /dev/dvd -r -J -V "Name of the DVD" -publisher "Name Lastname" -graft-points "video.avi=/path/of/first.avi" "pictures=/path/of/pictures/" "README=/path/of/README"

    Commit files to git repository

    Commit just a specific file:
    git add changed_file.txt
    git commit -m "Small improvements."
    Commit all changed tracked file:
    git commit -m "Small improvements." -a
    Check where to fetch/push
    git remote -v

    Get DPI resolution of your display

    Run:
    xdpyinfo | grep resolution
    and you should get (aut-101):
    resolution: 102x102 dots per inch
    Change it:
    xrandr --dpi 138/eDP1

    Use HP scanner

    aptitude install hplip-gui
    Run:
    hp-toolbox
    Install scanner/printer using the IP.
    Use it!

    Make good automatic passwords

    sudo apt-get install makepasswd
    makepasswd --char=10

    Recover NTFS files from corrupted filesystem

    Wondershare data recovery, tested by Eke.

    Scp files but without symlinks

    rsync --progress -avhe ssh root@10.0.0.1:/home/ssheikki .

    Configure internet in Linux

    nm-connection-editor
    gnome-control-center network
    gnome-nettool
    

    Get/change display DPI resolution

    xdpyinfo | grep inch
    xrandr --dpi 138/eDP1

    Change backlight settings

    sudo intel_backlight 20

    Adjusting sound volume from command line

    amixer -q set Master toggle unmute
    amixer -q set Master 2+ unmute && amixer -q set Speaker 2+ unmute && amixer -q set Headphone 2+ unmute
    amixer -q set Master 2- unmute && amixer -q set Speaker 2- unmute && amixer -q set Headphone 2- unmute
    

    Using Bumblebee in Linux

    optirun nvidia-settings -c :8
    To see if Nvidia is in use:
    cat /proc/acpi/bbswitch
    

    Find what eats power from your laptop

    powertop

    Put laptop in power saving mode

    8.14 Watts on Average with Standard Deviation 0.18
    sudo pm-powersave true
    8.38 Watts on Average with Standard Deviation 0.15
    Note: Does not seem to be useful.
    

    Measure laptop power consumption

    sudo add-apt-repository ppa:colin-king/powermanagement
    sudo apt-get update
    sudo apt-get install powerstat
    powerstat
    

    Grub boot selections for UX32VD

    8.96 Watts on Average with Standard Deviation 0.25
    GRUB_CMDLINE_LINUX_DEFAULT="quiet splash pcie_aspm=force drm.vblankoffdelay=1 i915.semaphores=1 nmi_watchdog=0"
    8.26 Watts on Average with Standard Deviation 0.19
    Turning off wireless and keyboard:
    7.21 Watts on Average with Standard Deviation 0.48
    Turning off the display:
    6.70 Watts on Average with Standard Deviation 0.15

    Monitor battery charging

    gnome-power-statistics
    Maybe also:
    battery-stats-collector
    

    Monitor CPU usage

    gnome-system-monitor

    Get display setting info

    sudo get-edid | parse-edid

    Changing display resolution in xorg

    Graphical tools: lxrandr, arandr
    Manual:
    xrandr
    cvt 1920 1080 60
    xrandr --newmode <ADD_CVT_OUTPUT_HERE>
    xrandr --addmode eDP1 "1920x1080_60.00"
    xrandr --output eDP1 --mode "1920x1080_60.00" --rate 60 --fbmm 295x166
    

    Change java environment

    sudo update-alternatives --config java
    sudo update-alternatives --config javac
    

    Show files that changed between revision 207 and 209

    svn diff -r 207:209 --summarize
    More details:
    svn diff -r 207:209

    Join avis and compress to mp4

    mencoder -ovc copy -oac copy input*.AVI -o joined.avi
    kdenlive # Install before x264, etc. properietary codecs
    # These did NOT work properly:
    ghb # http://handbrake.fr/
    avimerge -i input*.AVI -o joined.avi
    avisync -o synced.avi -i joined.avi -n 90 # 30fps => 3.6s delay
    

    Mounting WebDAV to folder

    sudo apt-get install davfs2 && sudo modprobe coda
    mount.davfs http://localhost/owncloud/remote.php/webdav/ mnt
    

    Collect data from running Linux

    aptitude install collectd
    sudo cp -R /usr/share/doc/collectd/examples/collection3 /var/www/
    sudo apt-get install librrds-perl libconfig-general-perl libhtml-parser-perl libregexp-common-perl
    http://localhost/collection3/bin/
    

    recordmydesktop and audio also

    apt-get install pavucontrol
    pavucontrol # change in Recording tab, ALSA capture from to "Monitor of..."
    recordmydesktop -x 50 -y 50 --width 640 --height 320 --fps 30 --full-shots -o output.ogg --device pulse
    

    How to autorotate pictures based on exif data

    jhead -autorot *.jpg

    Display file info (such access, creation date)

    stat yourfile.txt

    Add sudo rights to users, no password required

    visudo
    user ALL=(ALL) NOPASSWD: ALL

    Encode x264 video without encoding audio

    Preferred way is to require good quality:
    ffmpeg -i input.avi -c:v libx264 -preset veryslow -crf 18 -c:a copy output.mkv
    If original is 582MB, then veryslow is 152MB (26%) and slow is 163MB (28%).
    If you want to have fixed file size:
    ffmpeg -threads 4 -i joined.avi -vcodec libx264 -filter:v yadif -b 3000k -cmp +chroma -g 250 -partitions +parti4x4+partp8x8+partb8x8 -i_qfactor 0.71 -keyint_min 25 -b_strategy 1 -flags +loop -acodec copy output.avi
    

    Stabilise videos in Linux

    melt input.avi -filter videostab4 shakiness=4 -consumer xml:temp.xml all=1 real_time=-2
    melt temp.xml -audio-track input.avi -consumer avformat:output.mp4 vcodec=libx264 crf=18 tune=film preset=slow
    

    Editing Olympus u850SW videos with kdenlive

    It seems that the basic audio format "uncompressed PCM (mono, 8000 Hz)",
    does not work well with kdenlive. There is strange noise every now and then.
    One solution is to convert the videos to use lossless flac codec:
    ffmpeg -i PA100038.AVI -c:v copy -c:a flac ../PA100038.AVI
    You get good quality output at least with Theora codec.
    

    How to backup remote svn repository

    svnadmin create newrepo
    echo '#!/bin/sh' > newrepo/hooks/pre-revprop-change
    chmod 755 newrepo/hooks/pre-revprop-change
    svnsync init file:///home/ssheikki/Desktop/newrepo https://svn.kapsi.fi/ssheikki --source-username=ssheikki --sync-username=ssheikki
    svnsync sync file:///home/ssheikki/Desktop/newrepo --source-username=ssheikki --sync-username=ssheikki --sync-password=syncPassword
    

    Scrolling buffer in screen

    Press Ctrl+A + [
    Press esc to return to normal mode.
    

    Check if nameservers are working correctly

    dig @ns1.giraf.fi www.mielipiteet.fi
    dig @a.ns.joker.com www.vapahtaja.com
    Your IP should be found in the ANSWER section.
    

    How to add site in apache2 web server

    Add entry to /etc/apache2/sites-available
    sudo a2ensite <your_entry> service apache2 reload

    Encrypt/decrypt file in Linux

    gpg -c yourfile.tar
    gpg yourfile.tar.gpg
    

    HDR images/photos

    fotoxx

    Random timestamp to epoch in Linux

    echo 29/Mar/2010:12:31:04 | timestamp=`sed 's/\// /g;s/:/ /'`; date --date=$timestamp +%s

    Start new screen versus new screen window

    screen -S "newscreen" -d -m bash -c "sleep 100"
    screen -t "newwindow" bash -c "sleep 100"
    

    Select screen window from command line

    screen -X select 6

    What your shell understand with a command

    type echo
    type ls
    type dir
    

    Create loop device of a file

    losetup /dev/loop0 targetfile

    Calculate lines of code in directory

    find . -name '*.c' | xargs wc -l
    find . -name "*.cpp" -o -name "*.hpp" -o -name "*.c" -o -name "*.h"  | xargs wc -l | sort -rn - 
    find . \( -name "*.c" -o -name "*.h" \) -a ! -path "*2011*" | xargs wc -l | sort -rn -
    cloc /path/to/be/analysed1 /path/to/be/analysed2 \
    --counted=/output/path/of/recognised/files/counted_files.txt \
    --exclude-lang=m4,HTML, \
    --exclude-dir=test,backup \
    --not-match-f=\#
    

    Install another Linux in Ubuntu using VirtualBox

    apt-get install virtualbox
    Start virtualbox: virtualbox
    Follow instructions and provide the virtual image you want.
    Minimal 64bit Ubuntu 12.04.01 LTS server install took e.g. 1165MB disk space. After apt-get upgrade (latest kernel etc) 2019MB.
    After installing VirtualBox guest additions (to enable folder sharing between guest and host): 2146MB.
    Note: You can do port forwards or share disks to interact.

    Create virtual network between host and guest

    sudo apt-get install uml-utilities
    Add in host /etc/network/interfaces:
    auto tap0
    iface tap0 inet manual
        up ifconfig $IFACE 172.16.1.1 netmask 255.255.255.0 up
        down ifconfig $IFACE down
        tunctl_user <user>
    sudo ifup tap0
    Then you need to bridge to tap0 in the guest and add
    in guest's /etc/network/interfaces:
    auto eth1
    iface eth1 inet static
        address 172.16.1.2
        netmask 255.255.255.0
    Now you should be able to access 172.16.1.1 and 172.16.1.2, respectively.
    

    Install kernel modules (with m-a)

    apt-get install module-assistant dkms
    m-a prepare
    m-a update
    m-a a-i <your-module>
    

    Print block device attributes

    sudo blkid

    Record Skype phone call in Linux

    This worked at least:
    1) Start skype
    2) Start audacity
    3) Make call and press record in audacity. You might need to set levels but worked ok directly.

    List all loop devices

    losetup -a
    Print next free:
    losetup -f
    

    Unmount detached network mounts

    mount -l <netmount>

    Unmount device that says: "device is busy"

    lsof /dev/yourdevice
    ps aux | grep PIDwithOpenFile
    kill -9 PID # If it is e.g. terminal
    

    Doing calculations in command line Linux

    echo $((2*3)) # Integers only
    echo "6.6*2.4" | bc
    

    Protect your system from PHP flaws

    apt-cache show php5-suhosin

    Open .mobi files in Linu

    fbreader yourmobi.mobi

    Website to make catchy animation videos

    http://www.powtoon.com/

    Websites to make webpage mockups

    http://jetstrap.com/
    http://twitter.github.com/bootstrap/
    

    Analyse program execution details

    Linux Trace Toolkit (LTT)

    Save pdf with links

    links "http://some.url/pdf.pdf"
    Press "f" to save the file.
    

    Get certain range of bytes from a file

    curl -r -10 -o output.bin http://dom.com/file.txt # get last 10 bytes
    curl -r 0-9 -o output.bin http://dom.com/file.txt # get first 10 bytes
    

    Set resolution for 27p Dell U2713H 16/9 monitor

    cvt 2560 1440 30
    xrandr --newmode "2560x1440_30.00" 146.25 2560 2680 2944 3328 1440 1443 1448 1468 -hsync +vsync
    xrandr --addmode HDMI1 2560x1440_30.00
    xrandr --auto --output HDMI1 --mode 2560x1440_30.00 --above eDP1
    

    Browse S3 compatible cloud storages in Linux

    dragondisk

    Create file with X MegaBytes of random content

    dd if=/dev/urandom of=XMB-file.dd bs=XM count=1
    e.g. 10MB file
    dd if=/dev/urandom of=10M bs=10M count=1
    dd if=/dev/urandom of=10M bs=1M count=10
    

    Do same command in multiple machines

    Use PuSSH:
    http://sourceforge.net/projects/pussh/?source=dlp
    For example, generate random 10MB temp file to each computer:
    ./pussh -r lxbsp27[21-24] dd if=/dev/urandom of=/tmp/10M bs=1M count=10
    

    Opening and editing ROOT files (.root)

    Use TBrowser:
    root
    root [0] new TBrowser
    

    Get summary statistics of each protocol

    netstat -s

    Updatedb when using encrypted home folders

    .mlocate/updatedb
    after this locate should find your files
    

    Get CPU temperature to be detected

    sensors-detect
    sensors
    

    Open port using iptables

    iptables -I INPUT -p tcp --dport 1094 -j ACCEPT

    Find only binary files

    find . -executable -type f
    find -type f -executable -exec sh -c "file -i '{}' | grep -q 'x-executable; charset=binary'" \; -print
    

    Show info about a file

    stat <your_file>

    Direct all output to a file

    command &> file.txt

    Divide float numbers in Linux

    echo "scale=3;6.6/2.4" | bc

    Print file but skip n lines from start

    tail -n+2 file.txt

    cmake and gdb

    Add in CMakeLists.txt:
    set(CMAKE_BUILD_TYPE Debug)
    break davix_rw_test.cpp:40
    run and then use next or step/s to walk through the interesting code.
    Other convenient commands:
    list, print <varname>, set <varname> = 3, finish
    Use gdb history, add file: ~/.gdbinit
    set history filename ~/.gdb_history
    set history save
    

    Show your bash commands

    set -x
    set +x
    

    Configure ethernet connection between two computers

    1st computer with network card eth0 (e.g. Ubuntu):
    ifconfig eth0 192.168.1.1 netmask 255.255.255.0 broadcast 192.168.1.255
    2nd computer with network card p6p1 (e.g. SL6):
    ifconfig p6p1 192.168.1.2 netmask 255.255.255.0 broadcast 192.168.1.255
    Also disable automatic network configurations:
    /etc/init.d/NetworkManager stop
    /etc/init.d/ntworking stop
    

    Opening Graphical applications as a root

    xhost +local:
    beesu xeyes
    

    Check for rootkits/backdoors

    apt-cache show rkhunter
    apt-cache show chkrootkit
    

    Irc/irssi connect to a second irc server

    /server add -network freenode irc.freenode.net
    /connect freenode
    /join -freenode #<your_channel>
    

    Starts ls and date commands in two separete screen windows

    screen -dmS "hax"
    screen -S "hax" -p 0 -X stuff "ls$(printf \\r)"
    screen -S "hax" -X screen
    screen -S "hax" -p 1 -X stuff "date$(printf \\r)"
    

    Kill all detached screens

    kills () {
        for session in $(screen -ls | grep Detached | sed 's/ *\([0-9]\)\..*/\1/')
        do
            echo "Killing ${session}"
            screen -S "${session}" -X quit;
        done
    }
    

    Octave; change plot font and line sizes for presentation

    % Install: GNU Octave, version 3.6.4 (older ones won't probably work)
    plot(1:10);xlabel("example");ylabel("example");title("example");
    fontsize=20;
    hx=legend('example');set(hx, "fontsize", fontsize)
    set([gca; findall(gca, 'Type','text')], 'FontSize', fontsize);
    set([gca; findall(gca, 'Type','line')], 'linewidth', 3);
    saveas(1, "presentation.jpg");
    % Note! The Octave/gnuplot does not show the figure correctly but
    % the output is correct. Tested with gnuplot 4.4.
    

    Eject unmounted USB device in Linux (such as Kindle)

    sudo eject /dev/sdc1

    Mounting LVM volume partition

    lvm help                # Available lvm commands
    lvscan                  # List all logical volumes in all volume groups
    pvscan                  # List all physical volumes
    lvdisplay /dev/vg1      # Display information about a logical volume
    vgchange -a y vg1       # Activate your volume group
    mount /dev/vg1/root mnt # Mount a logical volume
    http://www.tldp.org/HOWTO/LVM-HOWTO/commontask.html
    

    Open emacs in Xterm

    emacs -nw [filename]

    Acer-wmi and wireless connections

    Make sure that wireless is set on when the module is loaded:
    modprobe acer_wmi wireless=1
    dmesg should print something like this:
    [  942.771786] acer-wmi: Acer Laptop ACPI-WMI Extras
    [  942.775138] Registered led device: acer-wmi::mail
    [  943.000081] b43-phy0: Radio hardware status changed to ENABLED
    
    Then just use it:
    iwconfig wlan0 essid YOUR_ESSID
    dhclient wlan0
    Alternatively you can set the wireless on after loading like this:
    echo "1" > /sys/devices/platform/acer-wmi/wireless

    Working with Logitech QuickCam for Notebooks Pro (046d:08cb)

    You need uvc-video kernel module:
    http://linux-uvc.berlios.de/
    If the webcame does not work, e.g. with xawtv, easiest "solution" is to force the camera to fullspeed mode by removing ehci-hcd:
    rmmod ehci-hcd

    Show user idle time in IRC

    /WII nickki

    Configure temperature sensors

    aptitude install lm-sensors
    Then configure using:
    sensors-detect
    You can view the sensors with:
    sensors

    Screen capture a frame from SimPartner video

    Capture jpeg from 20 seconds point:
    mplayer -nosound -vo jpeg -frames 1 -ss 20 input.avi -loop 1

    mencoder tips

    Fix header:
    mencoder -idx rikkinainen.avi -ovc copy -oac copy -o ehja.avi
    Compress a video file to mpeg4 (two pass):
    mencoder input.avi -ovc lavc -lavcopts vcodec=mpeg4:vpass=1 -oac copy -o /dev/null
    mencoder input.avi -ovc lavc -lavcopts vcodec=mpeg4:mbd=2:trell:vpass=2 -oac copy -o output.avi
    Make video from jpeg images:
    mencoder mf://*.jpg -mf w=320:h=240:fps=10:type=jpg -ovc lavc -lavcopts vcodec=mpeg4:mbd=2:trell -oac copy -o filename.avi

    How to grep more than two patterns

    Use -e switch:
    iwlist wlan0 scan | grep -i -e key -e essid

    Helvetin skandit osa II - xterm skulaa

    Start xterm with: "LC_CTYPE=fi_FI xterm" command. If this works, add 
    in the ~/.bashrc file "LC_CTYPE=fi_FI" and your skandit/ääkkoset will 
    probably work.
    

    Skandit in emacs

    Add these lines in the end of ~/.emacs file:
    (prefer-coding-system 'latin-1)
    (set-keyboard-coding-system 'latin-1)
    (set-clipboard-coding-system nil)
    (set-terminal-coding-system nil)
    

    Comment/uncomment region in emacs

    Select start of the area with:
    Ctrl+space
    Comment the area:
    Ctrl+c Ctrl+c
    Uncomment the area:
    Ctrl+u Ctrl+c Ctrl+c

    Resize your windows partition for example to install linux

    Defragment first your Windows partition in Windows.
    Then boot e.g. with Ubuntu Dekstop installation CD into Linux.
    Use gparted to resize your windows partition to desired size.
    gparted - GNOME partition editor

    Encrypted system partition at install

    Basically you need alternate installation CD of Ubuntu, e.g.:
    ubuntu-9.04-alternate-i386.iso
    NOT Desktop or Server installation versions.
    1) Choose one boot partition, e.g. 1GB, which is not encrypted.
    2) Choose root and boot partition that are encrypted, e.g. 4GB and 100GB.
    3) Choose "Configure encrypted partitions".
    4) Set boot and root sections to the partitions.
    5) Write partitions to the disk.

    Make encrypted swap partition without passphrase

    Edit /etc/crypttab, add:
    swap /dev/disk/by-uuid/SWAPPARTITIO /dev/urandom swap
    Edit /etc/fstab, add:
    /dev/mapper/swap none swap sw 0 0
    Add finally update the init ram filesystem:
    update-initramfs -u

    Edit crontab entries of the user

    gcrontab
    Check that they are ok:
    crontab -l

    Touch files recursively

    find /dir/to/be/touched/ -exec touch {} \;

    Mount luks partition which is in file

    losetup /dev/loop1 luksfile.dd
    cryptsetup luksOpen /dev/loop1 luksfile
    mount /dev/mapper/luksfile mnt
    When unmount, remember to unmount loop device also:
    losetup -d /dev/loop1

    Force hard disk check

    touch /forcefsck

    List symbols contained in an object file

    nm -D libname.so

    Linking order with g++

    "The traditional behavior of linkers is to search for external functions from left to right in the libraries specified on the command line. This means that a library containing the definition of a function should appear after any source files or object files which use it."
    For example: g++ test.cpp -lm -o test

    What process is occupying your tcp port

    Log in as root (in order to see all processes) and:
    fuser -n tcp 22
    or
    fuser 22/tcp

    Check what devices and in which ports are in your network

    For example:
    nmap -v 192.168.100.*

    unrm brute-force howto

    First, unmount the file system from where you deleted the unfortunate files.
    Then install (or better, have already installed):
    aptitude install tct
    Copy all unallocated data from the disk to safety:
    unrm -f ext2fs /dev/mapper/truecrypt5 > /media/undelete.bin
    Use less or similar to find e.g. "\document" word.

    Create A4 pdf of your picture

    convert -page A4+0+0 image.XXX document.eps2
    epstopdf document.eps2

    Get all info about USB device

    udevadm info -q all -n /dev/video0

    How to set unique udev name for your USB device

    First find the device path of your device:
    udevadm info -q all -n /dev/video0 | grep "P:"
    result is e.g. "P: /devices/pci0000:00/0000:00:13.0/usb2/2-1/2-1:1.0/video4linux/video0"
    Then check the parameters of this device:
    udevadm info -a -p /devices/pci0000:00/0000:00:13.0/usb2/2-1/2-1:1.0/video4linux/video0
    Find some unique keys to identify the device from this output, e.g.:
        SUBSYSTEM=="video4linux"
        ATTRS{vendor}=="0x1002"
        ATTRS{device}=="0x4374"
    
    Next write this info to udev rule, e.g. to /etc/udev/rules.d/10-local.rules (or any file in /etc/udev/rules.d/):
    SUBSYSTEM=="video4linux", ATTRS{vendor}=="0x1002", ATTRS{device}=="0x4374", SYMLINK+="myVideo"

    Restart the udev and you should in business:
    udevadm control --reload-rules
    OR
    service udev restart

    Resolving svn conflicts

    Write
    svn resolve [path or dir]

    Undelete vfat/fat32 data

    First copy all free data to image file:
    blkls -f fat /dev/sda2 > sda2.free.image
    Next find data from here, e.g. with foremost:
    sudo foremost -t avi -i sda2.free.image -o /boot/backup/
    The above managed to recover 100MB data from 3GB image. Below managed to recover 700MB of data:
    magicrescue -r avi -d /boot/backup/ver3 sda2.free.image
    Including some H.264 X.264 files.
    Third thing you can try is photorec:
    photorec sda2.free.image
    Proceed -> Intel
    File Opt -> riff
    Then finally Search and Other file system
    This method managed to recover 1.45GB of data from the 3GB image, including files that were not recover by other methods.

    Before all of the above methods you REALLY SHOULD however try testdisk:
    testdisk /dev/sda
    Proceed -> Intel -> Advanced -> /dev/sda2 and then Undelete
    Find the file you want to undelete and choose "c" to copy it to safety.

    Ssh connection without password

    In computer FROM which you want to connect:
    ssh-keygen --t
    cat ~/.ssh/id_rsa.pub | ssh -l USERNAME TARGET.SERVER.COM 'cat >> ~/.ssh/authorized_keys'

    Filemanager with video thumnails

    Nautilus

    Use region in Latex

    ctrl+space sets start of the region and cursor location end.
    You can cut for example with ctrl+w.

    Generate timestamp in C for filename for instance

    time_t now = time(NULL);
    struct tm *ptm = localtime(&now);
    char buffer[32];
    strftime(buffer, 32, "%Y_%m_%d_%H_%M_%S", ptm);
    cout<<"time: "<<buffer<<endl;
    

    Changing locale settings

    Check settings:
    echo ä | hexdump -C
    00000000 e4 0a # This means ISO-8859-15 in use
    00000000 c3 a4 0a # This means that UTF-8 is in use
    Which locale to use:
    If you do NOT want that applications speak finnish to use: en_US.utf8
    It should be here the right locale (do not add manually but use locale-gen):
    /etc/default/locale
    Also add in the /etc/environment:
    LC_CTYPE=en_US.utf8
    You should probably add these in .bashrc:
    export LANG=en_US.UTF-8
    export LESSCHARSET=utf-8
    To make emacs work, add these to .emacs file:
    (setq locale-coding-system 'utf-8)
    (set-terminal-coding-system 'utf-8)
    (set-keyboard-coding-system 'utf-8)
    (set-selection-coding-system 'utf-8)
    (prefer-coding-system 'utf-8)
    
    Make lynx to use utf8:
    lynx -display_charset=utf8
    Make screen use utf8:
    Ctrl+: utf8 on on

    Add timestamp and ipfrom mysql columns

    ALTER TABLE event_referer ADD ts TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP;
    ALTER TABLE event_referer ADD ip_from int(10) unsigned zerofill NOT NULL DEFAULT '0000000000';
    

    mov to avi with ffmpeg

    ffmpeg -i input.MOV -g 30 -sameq -vcodec msmpeg4v2 -acodec pcm_u8 output.avi

    Convert cr2 to jpg

    Camera white balance:
    for i in *.CR2; do ufraw-batch ${i} --wb=camera --out-type=jpeg --output=$i.jpg; done
    Works, but not so good:
    for i in *.CR2; do dcraw -c -q 0 -w -H 5 -b 8 $i | cjpeg -quality 80 > $i.jpg; echo "$i done"; done
    Gimp support (for manual tuning):
    aptitude install ufraw gimp-ufraw

    Scale/resize any pdf to A4 size

    Use acroread and print the pdf to file in A4 size, e.g. test.ps.
    ps2pdf test.ps
    Now you should have test.pdf in A4 size.

    Protect a web folder with .htaccess

    Create .htaccess file like this:
    AuthUserFile /home/users/ssheikki/.htpasswd 
    AuthName "Secret area" 
    AuthType Basic 
    require user ssheikki
    
    Create passwd:
    htpasswd -c .htpasswd ssheikki

    CVS repository to git

    git cvsimport -a -i -k -d /home/ssheikki/cvs_repository/ -C mielipiteet opinions
    Now mielipiteet directory has git repository of opinions module.

    Make copy of remote git repository

    git remote add lakka ssh://ssheikki@lakka.kapsi.fi/~/sites/ssheikki.kapsi.fi/secure-www/mielipiteet

    Check if you have BCM43xx chipset

    lspci -vvnn | grep 14e4
    02:00.0 Network controller [0280]: Broadcom Corporation BCM4313
    802.11b/g/n Wireless LAN Controller [14e4:4727] (rev 01)
    lshw -C network
      *-network
           description: Network controller
           product: BCM4313 802.11b/g/n Wireless LAN Controller
           vendor: Broadcom Corporation
    http://elrepo.org/tiki/wl-kmod
    eth0: Broadcom BCM4727 802.11 Hybrid Wireless Controller 5.100.82.112
    

    Replace all strings inside a file

    sed -i 's/old_string/new_string/g' target_file.txt

    Run ssh commands without password prompt

    sshpass -p 'password' ssh user@server 'command'

    Encrypt/decrypt directory with openssl

    mkdir test; echo "koe">test/koe; echo "koe2">test/koe2
    tar cvzf - test | openssl des3 -salt -k secretkey | dd of=encrypted_data
    dd if=encrypted_data |openssl des3 -d -k secretkey |tar xvzf -
    

    Remove bash history and exit

    history -c && exit

    Monitor how many open tcp connections there is (open sockets)

    watch --interval 1 "netstat | grep tcp | wc -l"

    Get maximum number of processes you can run

    cat /proc/sys/kernel/pid_max

    Create bootable Live USB drives for a variety of Linux distributions

    unetbootin

    Write several lines to file in bash

    cat >> filename_where_to_write.txt << EOF
    Here is text that should be appended to the file.
    Here is some more text.
    EOF
    

    Creating and dynamically linking "shared object" libraries (.so)

    # Creating
    g++ -Wall -fPIC -c *.c
    gcc -shared -Wl,-soname,libsolinktest.so.1 -o libsolinktest.so.1.0 *.o
    # Linking
    g++ -Wall main.c -L. -lsolinktest -o main
    # Using
    LD_LIBRARY_PATH=$LD_LIBRARY_PATH:. ./main
    

    Enable ssh connections remote forwarding

    Add in /etc/ssh/sshd_config, e.g. in the end:
    GatewayPorts yes
    

    Wireshark filter rules

    Show all traffic to/from ips 10.40.*.*
    ip.addr==10.40.65.31/16
    which is http protocol
    ip.addr==10.40.65.31/16 and http
    

    Record how long program run into a file

    /usr/bin/time --verbose -o time_out.txt -a ./a.out

    Check how much real memory is used by program

    top -p<PID>
    and see column RES
    

    Copy symbolic links as they are

    cp -d

    Strip the first line of a file

    echo $'1\n2\n3\n4' | tail -n +2

    Make 100MB ramdisk

    mkdir -p /mnt/tmpfs
    mount -o size=100M -t tmpfs none /mnt/tmpfs
    

    Disable yum auto updates

    yum remove yum-autoupdate
    OR
    /sbin/service yum-autoupdate stop
    /sbin/chkconfig --del yum-autoupdate
    

    Check which libraries program needs

    ldd ./main
    You can define missing ones e.g. like this:
    LD_LIBRARY_PATH=$LD_LIBRARY_PATH:../path_of_missing_library/ ldd ./main
    

    List ips that point to certain dns name

    nslookup server.com

    Execute command at certain time

    echo "ls /tmp/ > /tmp/txt.txt" | at 13:30

    Convert UNIX epoch time to human readable and back

    $ date +%s -d"Tue Jul 16 14:05:05 CEST 2013"
    1373976305
    $ date -d @1373976305
    Tue Jul 16 14:05:05 CEST 2013
    

    Kill a suspended process

    [3]+  Stopped  my-program
    kill %3
    

    Calculate floating point numbers in Linux

    echo "scale=2;2/3" | bc
    .66
    

    Concanate lines in Linux

    seq 1 6 | sed ':x;N;s/\n/ /;tx'
    1 2 3 4 5 6
    seq 1 6 | sed -e :x -e 'N;s/\n/+/;tx' |bc
    21
    

    Check git what has changed

    Last commited files:
    git log -1 --stat
    Changes in files:
    git log -p -1
    

    Rename files in linux

    There is two versions of rename:
    rename file- file2- *
    rename 's/file-/file2-/' *
    

    Replace string inside a file

    sed 's/originalstring/replacedstring/' filename.txt

    Get last command starting with letter x

    echo "!x"

    Measure disk IO

    dstat
    vmstat -n 1
    iostat -x
    

    Empty file cache

    echo 3 > /proc/sys/vm/drop_caches

    Trace system calls (e.g. IO calls)

    strace
    strace -c
    strace -f -e trace=read,write
    

    Execute further commands only if previous fail

    rm filethatdoesnotexist.txt || echo 2
    0 || 1
    

    Do no execute next commands if previous ones fail

    rm filethatdoesnotexist.txt && (echo 2;echo 3)
    rm filethatdoesnotexist.txt && echo 2 && echo 3
    

    Set which java version maven is using

    User JAVA_HOME variable to point to the java version to be used:
    export JAVA_HOME=/usr/lib/jvm/java-1.7.0-openjdk-1.7.0.25.x86_64/
    mvn -version
    export JAVA_HOME=/usr/lib/jvm/java-1.6.0-openjdk-1.6.0.0.x86_64/
    mvn -version
    

    Compile with clang instead of gcc for more verbose debugging

    export CC=clang
    export CXX=clang++
    cmake ..
    make
    

    Git: visualise and undertand a git repository

    tig
    git log --oneline --abbrev-commit --all --graph --decorate --color
    gitk
    

    Git: add new changes to previous commit

    git commit --amend

    Git: merge with scout pattern

    git status
    git checkout -b merging_feature
    git merge new_feature
    If failed:
    git reset --hard
    git checkout master
    git branch -D merging_feature
    If success:
    git checkout master
    git merge merging_feature
    

    Git: merge with savepoint pattern

    git status
    git branch savepoint
    git merge new_feature
    If failed:
    git reset --hard 
    git reset --hard savepoint # OR SHA-1 hash
    git branch -d savepoint
    If success:
    git branch -d savepoint
    

    Git: apply set of changes to current branch

    git cherry-pick <SHA1-of-commit>

    Git: Merge branch to current branch and destory merged branch (rewrite history)

    git rebase <current_branch-this_first> <merged_and_deleted_branch-this_second>

    List directory contents

    tree -C

    List all filenames and sizes

    Only filenames without path:
    find . -type f -printf "%s %f\n"
    With path:
    find . -type f -printf "%s %h%f\n"
    

    C++ code profiling with gprof

    g++ -pg main.c -o main
    ./main # Generates gmon.out file
    gprof -p -b ./main gmon.out
    

    Find lines that are in a first file from the second

    for i in {1..10}; do echo $i >> first.txt;done
    for i in {8..15}; do echo $i >> second.txt;done
    grep -x -f first.txt second.txt
    8
    9
    10
    

    Monitor traffic on remote machine with wireshark GUI

    ssh root@server.com 'tshark -f "port !22" -w -' | wireshark -k -i -
    ssh root@server.com tcpdump -iany -U -s0 -w - 'not port 22' | wireshark -k -i -
    

    svn export in git

    git archive master | tar -x -C /targetdir

    Show whole history of file in git (also renames)

    git log --follow yourfile.txt

    Grep files found with find

    grep print `find . | grep m$`

    Print nice eps figures in octave 3.6.4 etc

    set([gca; findall(gca, 'Type','text')], 'FontSize', 16);
    set([gca; findall(gca, 'Type','line')], 'linewidth', 3);
    print -depsc2 figure.eps
    

    How to create and use core dump

    g++ -g crasher.c
    ulimit -c unlimited
    ./a.out
    gdb ./a.out core.23531
    

    Get number of file handles allowed for login session

    ulimit -Sn
    ulimit -Hn
    

    Detect memory leaks, uninitialise variables, and invalid pointers with valgrind

    valgrind --tool=memcheck ./a.out
    valgrind --tool=memcheck --leak-check=yes ./a.out
    

    Static code analysers that you might want to try C/C++

    clang++ --analyze test.cpp 
    cppcheck --enable=all test.cpp
    splint test.c
    

    Show files that changed between commits

    git diff --name-only HEAD~3 HEAD
    git diff --name-only origin/HEAD HEAD
    git diff origin/HEAD HEAD file.txtku
    

    Commit only changed files from current directory to git

    git add -u
    git commit .
    

    Install perl scripts from Comprehensive Perl Archive Network (CPAN)

    perl -MCPAN -e shell 
    OR
    cpan
    Search programs: http://search.cpan.org/
    m Email::Outlook::Message
    install Email::Outlook::Message
    

    Package managers for different programming languages

    http://en.wikipedia.org/wiki/List_of_software_package_management_systems#Application-level_package_managers
    Perl - CPAN - http://en.wikipedia.org/wiki/Cpan
    PHP - Pear - http://en.wikipedia.org/wiki/PHP_Extension_and_Application_Repository
    Python - Pip - http://en.wikipedia.org/wiki/Pip_%28package_manager%29
    Python - EasyInstall - http://en.wikipedia.org/wiki/EasyInstall
    Ruby - rubygems - http://en.wikipedia.org/wiki/RubyGems
    

    Check which version of scientic linux you are running

    grep -i 'scientific linux cern' /etc/redhat-release
    cat /etc/issue
    

    Start gdb with executable parameters

    gdb --args <program> <program_parameters>

    Git usage tips - best practises

    - Compare index and HEAD
      git diff -cached
    - Add files by reviewing what you add:
      git add -p
      git add --patch
    - Use atomic commits ("commit per feature")
    - Commit message <= 50 chars
    - ref~1 (previous commit), ref^1 (previous parent)
    - Hunt bugs
      git bisect start      # Start bug hunting
      git bisect bad        # This commit is broken
      git bisect good v1.0  # This commit still worked
      git bisect reset      # When bug was found (returns to intial state)
    - Hunt bugs automagic
      git bisect start HEAD v1.0
      git bisect run test-error.sh # or make tests
    - Undo adding of file (after git add file.txt)
      git reset file.txt
    - Undo previous commit:
      git reset -soft HEAD^ # HEAD is moved back
      git reset HEAD^       # HEAD and index moved back
      git reset -hard HEAD^ # HEAD, index and working tree moved back
    - Undo hard reset (if it was mistake)
      git reflog
      git rebase <commit hash>
    - Clean repository for backup
       git reflog expire --expire=1.minute refs/heads/master
       git fsck --unreachable      
       git prune                   
       git gc 
    - Delete and rename brach
      git branch -d
      git branch -m
    - Create branch and move into it
      git checkout -b <branch>
    - Prefer rebasing over merging branches: avoids gluttering meta-data commits
      (you should not need to use normally git cherry-pick)
    - Good workflow might look like:
      # "we are on master"
      git checkout -b newfeature
      # "make changes"
      git commit -m "New feature"
      git rebase master
      git checkout master
      git merge newfeature
      git push origin master
      # if push fails (because somebody managed to change master)
      git pull --rebase
    - List branches
      git branch
      git branch -r # remote ones
    - Meld, remove, edit, reorder commits
      git rebase --interactive
    - Store changes away temporarily
      git stash save "Described changes"
    - Repository is for source code only (no binaries, distros, etc.)
    

    Configure firewall with ufw

    ufw allow <port>
    ufw deny <port>
    ufw status
    

    Capture video from screen for certain amount of time

    recordmydesktop -x 150 -y 120 -width 700 -height 500 -fps 30 --full-shots -o output.ogv
    Check the PID:
    ps aux | grep recordmydesktop | grep -v grep
    Record 50 minutes:
    sleep 3000 && kill -INT <PID>
    

    Check if DNS cache is working

    Launch wireshark and use this filter:
    dns&&ip.src==<your_ip_address>
    Try to get some domain, e.g.:
    getent hosts aalto.fi
    There should be just one DNS request.
    

    Configure caching DNS server: nscd

    yum install nscd # A Name Service Caching Daemon (nscd).
    Check if DNS cache is working - should work like a dream.
    If like nightmare, asking lots of requests every 15 seconds, do:
    rm /var/db/nscd/hosts
    service nscd restart
    If still problems, enabling debug from /etc/nscd.conf
    and checking /var/log/nscd.log should be very useful.
    

    Find pid of your applications

    pidof <application_name>
    e.g.
    pidof firefox
    

    List files opened by PID

    lsof -a -p 28290
    or by anybody
    lsof | wc -l
    sysctl fs.file-nr
    

    List content of yum package

    repoquery -l <package_name>
    e.g.
    repoquery -l NetworkManager-gnome
    

    Install driver for Broadcom BCM4313 802.11bgn Wireless Network Adapter

    Check that you have right driver:
    lspci -vvnn | grep 14e4
    Download Linux STA driver package:
    http://www.broadcom.com/support/802.11/linux_sta.php
    Install like this:
    http://www.broadcom.com/docs/linux_sta/README.txt
    If the compile fails, you might want to try:
    make API=WEXT
    sudo cp wl.ko /lib/modules/`uname -r`/kernel/drivers/net/wireless
    sudo depmod -a
    modprobe wl
    For loading the module in startup, add:
    /etc/sysconfig/modules/kmod-wl.modules 
    or simply
    echo modeprobe wl >> /etc/rc.local
    

    Configure and enable wireless connection with Enlightenment

    sudo service NetworkManager status
    sudo service NetworkManager stop
    Add system tray to englihtenment to display nm-applet:
    1) Open "Settings" -> "Modules" configuration window.
    2) Click "Systray" and "Load".
    3) In "Shelf Contents" window, click "Systray" and "Add Gadget"
    To start it automatically:
    Setting -> Apps -> Startup Applications and enable "Network Manager" on the System tab.
    Maybe worth knowing is also:
    nm-connection-editor 
    

    Running google test suite

    List available tests:
    ./unittests --gtest_list_tests
    Run specific test set:
    ./unittests --gtest_filter=T_UploadFacility.*
    Run specific test only:
    ./unittests --gtest_filter=T_UploadFacility.Callbacks
    

    Alternative for netcat server

    nc.traditional -v -l -p 5001
    is same as
    socat - TCP-LISTEN:5001,crlf
    

    Benchmark network speed

    apt-get install iperf
    Server side:
    iperf -s 
    Server listening on TCP port 5001
    TCP window size: 85.3 KByte (default)
    Client side:
    iperf -c picsurl.com
    Example output in server side:
    [  4] local 78.47.65.195 port 5001 connected with 128.141.48.102 port 57633
    [ ID] Interval       Transfer     Bandwidth
    [  4]  0.0-10.1 sec   117 MBytes  96.8 Mbits/sec
    Example output in client side:
    [  3] local 128.141.48.102 port 57633 connected with 78.47.65.195 port 5001
    [ ID] Interval       Transfer     Bandwidth
    [  3]  0.0-10.0 sec   117 MBytes  97.5 Mbits/sec
    
    You can also monitor packet losses:
    Server: iperf -s -u -i 1
    Client: iperf -c picsurl.com -u -b 10m
    Example output server side:
    [ ID] Interval       Transfer     Bandwidth        Jitter   Lost/Total Datagrams
    [  4]  0.0- 1.0 sec  11.7 MBytes  97.9 Mbits/sec   0.024 ms   70/ 8394 (0.83%)
    [  4]  1.0- 2.0 sec  11.7 MBytes  97.9 Mbits/sec   0.123 ms  182/ 8511 (2.1%)
    [  4]  2.0- 3.0 sec  11.7 MBytes  98.0 Mbits/sec   0.029 ms  202/ 8539 (2.4%)
    [  4]  0.0-10.0 sec   117 MBytes  98.0 Mbits/sec   0.215 ms 1818/85257 (2.1%)
    [  4]  0.0-10.0 sec  124 datagrams received out-of-order
    

    Check process related file descriptor (open file) limit

    cat /proc/<pid>/limits
    for example
    grep open /proc/3531/limits 
    Max open files            1024                 4096                 files 
    

    Copy your ssh public key to remote machine

    ssh-copy-id username@server.com

    Increase the number of file descriptors allowed per process

    ulimit -n <allowed_fid>
    e.g.
    ulimit -n 10000
    ulimit -a
    

    Singleton with C++98 and pthread

    Singleton *Singleton::_s = NULL;
    Singleton *Singleton::instance() {
      static pthread_once_t once_control = PTHREAD_ONCE_INIT;
      pthread_once(&once_control, &Singleton::init);
      return _s;
    }
    void Singleton::init() {
      static Singleton s;
      _s = &s;
    }
    int main(void) {
      Singleton *s = Singleton::instance();
    }
    

    Singleton with C++11

    Because static variables are initialized in a threadsafe manner, we can do:
    Singleton *Singleton::instance(){
      static Singleton instance;
      return &instance;
    }
    int main(void) {
      Singleton *s = Singleton::instance();
    }
    

    Move your git repository to new place with read only access

    Copy the git repository to new place/server, e.g. with scp:
    scp -r my_git_repository_to_be_moved my_username@my_server:/new/repo/dir/
    Create .htaccess, e.g.
    echo "AuthName \"Secure Repo\"
    AuthType Basic
    AuthUserFile /home/users/username/.htpasswd
    
    require valid-user" > .htaccess
    and password
    htpasswd /home/users/username/.htpasswd johndoe
    Then it should work, e.g.:
    git clone https://johndoe:password@my_server/public/new/repo/dir
    

    Lock screen in englightenment

    enlightenment_remote -desktop-lock
    If this does not work, make sure that you have DBus system module enabled:
    settings -> extensions -> modules -> system -> DBus Extension
    

    Mounting HFS+ volumes under SL6 Linux

    rpm --import https://www.elrepo.org/RPM-GPG-KEY-elrepo.org
    rpm -Uvh http://www.elrepo.org/elrepo-release-6-6.el6.elrepo.noarch.rpm
    emacs  /etc/yum.repos.d/elrepo.repo # Enable repos
    sudo yum install kmod-hfs kmod-hfsplus
    

    Run gdb automatically with parameters

    (echo r;cat) | gdb --args ls

    Setting core dump to local directory

    ulimit -c unlimited
    cat /proc/sys/kernel/core_pattern
    # Might be like this: |/usr/libexec/abrt-hook-ccpp %s %c %p %u %g %t e
    echo "core.%e.%p" > /proc/sys/kernel/core_pattern
    

    Create a notification displayed in your desktop environment

    notify-send "Take a break"

    Debugging undeterministic behaviour with rr

    http://rr-project.org/
    rr record
    rr replay
    

    Delete matching lines from a file

    echo "a">a; echo "b">>a; echo "c">>a;sed -i '/b/d' a;cat a

    Interpret backslash escapes in bash

    echo -e "a\ta"

    C-style for loop in bash

    for ((i = 0 ; i < 5 ; i++)); do
      echo ${i}
    done
    

    Type special symbols in Linux

    CTRL+Shift+U, release, and type code, e.g. 003A

    Find if a keyword is being used in git repository

    git log -Skeyword

    Delete all files with certain name

    Simulate:
    find . -name result.root -exec ls -laF {} \;
    Do it:
    find . -name result.root -exec rm {} \;
    

    Check what is physical printing size of a pdf

    identify -verbose A0poster.pdf |grep "Print size"
      Print size: 35.4306x49.0139
    

    Create keytab file for scripts to get Kerberos tickets

    $ ktutil
    ktutil:  add_entry -password -p ssheikki@CERN.CH -k 1 -e arcfour-hmac-md5
    ktutil:  add_entry -password -p ssheikki@CERN.CH -k 1 -e aes256-cts
    ktutil:  write_kt ssheikki.keytab
    $ kdestroy
    $ kinit -kt ssheikki.keytab ssheikki
    $ klist
    

    Kill all processes containing string RemoteClient

    # Simulate
    ps -ef | grep RemoteClient | awk '{print $2}' | xargs echo
    # Do it
    ps -ef | grep RemoteClient | awk '{print $2}' | xargs kill -9
    

    Record and play keystrokes and mouse movements in Linux

    sudo apt-get install xmacro
    xmacrorec2 > record.xmacro
    xmacroplay "$DISPLAY" < record.xmacro
    

    Compile assembly code version of C++ code

    g++ -S main.c

    Check what optimization flags do in g++

    g++ -O2 -Q --help=optimizers > o2options.txt
    g++ -O3 -Q --help=optimizers > o3options.txt
    diff o2options.txt o3options.txt
    

    Profile python scripts

    python -m cProfile -s time main.py
    # Note, that perf will NOT work (because python is interpreted and you
    # track what the interpreter is doing), e.g.
    perf record python main.py
    perf report
    

    Show bash commands as they are exucted

    set -x
    set -vx # -v prints shell input lines
    

    Play track X from cd with mplayer

    mplayer -cdrom-device /dev/sr0 cdda://X -cache 5000
    e.g.
    mplayer -cdrom-device /dev/sr0 cdda://2 -cache 5000
    

    Show lines near the lines to grep hit

    Next line:
    grep -A1 keyword *.txt
    

    Choose every Nth line from a file

    sed -n '0~Np' your_file
    for i in {1..20};do echo $i>>i.txt;done
    sed -n '0~4p' i.txt
    4
    8
    12
    16
    20
    

    White space mode in emacs

    (global-whitespace-mode 1)
    See white space problems:
    M-x whitespace-report
    Note that whitespace problems are highlighted automatically (compared
    to your indenting settings).
    

    Emacs and C indenting

    Set depth of indenting:
    (setq-default c-basic-offset 4
                  tab-width 4)
    Indent with tabs:
                  indent-tabs-mode t)
    Indent with space:
    (setq-default indent-tabs-mode nil)
    Change formating:
    Ctrl+x+h
    tabify
    untabify
    

    Modify previous git commit

    Do only in private git repository:
    git add file.cc
    git commit --amend --no-edit
    git push github --force
    

    Display variable value in emacs

    Ctrl+h v
    c-mode-common-hook
    

    Emacs and ELPA packages

    In emacs23 paste this to an empty buffer and execute it (eval-buffer):
    (let ((buffer (url-retrieve-synchronously
           "http://tromey.com/elpa/package-install.el")))
      (save-excursion
        (set-buffer buffer)
        (goto-char (point-min))
        (re-search-forward "^$" nil 'move)
        (eval-region (point) (point-max))
        (kill-buffer (current-buffer))))
    In emacs24:
    (require 'package)
    (add-to-list 'package-archives '("melpa" . "http://melpa.milkbox.net/packages/"))
    (package-initialize)
    

    Emacs and auto-complete etc.

    Either in .emacs or in .emacs.d/init.el:
    (require 'auto-complete)
    (require 'auto-complete-config)
    (ac-config-default)
    To autocomplete also structures:
    (require 'yasnippet)
    (yas-global-mode 1)
    And c/c++ headers:
    (require 'auto-complete-c-headers)
    ; define include dirs and hooks also (google how to)
    Change variable names in whole name:
    iedit
    

    Emacs and copy-paste region

    Ctrl+space
    M+w  # Copy
    Ctrl+w # Cut
    Ctrl+y # Paste
    Ctrl+g # Quit command!!
    

    Emacs switch between windows

    Ctrl+x o # Switch to other window
    Ctrl+0   # Close current window
    Ctrl+1   # Open one window full
    Ctrl+2   # Horizontal cut
    Ctrl+3   # Vertical cut
    

    Emacs regexp and occurence etc. searches

    M+Ctrl+s
    Ctrl+r # Search backward
    M+x occur
    M+x grep
    M+x rgrep
    

    Emacs, set and see variable names

    M+: (setq test '61) # test=66
    Ctrl+h v # describe variable
    test Ctrl+e Ctrl+e # Evaluate test value
    

    Emacs, go to line shortcut

    (global-set-key (kbd "C-x j") 'goto-line)

    Emacs, realtime view of web development

    impatient-mode
    M+x httpd-start
    M+x impatient
    

    Git; fixing merge/pull conflicts

    yum install meld
    git mergetool
    git merge --help
    git checkout --theirs yourconflictingfile.txt
    git checkout --ours yourconflictingfile.txt
    

    Bash completion for command parameters also

    yum install bash-completion
    # yum in[TAB]
    info     install
    

    Parallell scripting in bash

    function test {
      sleep $1;echo "$1 done"
    }
    date
    test 3 &
    test 2 &
    wait
    date # should be 3 seconds difference to first date
    

    Do while loop in bash

    cnt=0
    while : ; do
      echo "$cnt"
      let "cnt+=1"
      if [ "$cnt" -gt "3" ];then
        break;
      fi
    done
    

    Split string in two lines in bash

    $ echo "tes-"$'\n'"ti"
    tes-
    ti
    

    update-rc.d like command on Redhat

    1) cp sshpipe.sh /etc/rc.d/init.d/sshpipe
    2) Make sure you have in your script:
    #!/bin/bash
    # chkconfig: 2345 95 20
    # description: Ssh pipe
    # Opens ssh pipe
    # processname: sshpipe
    3) Then execute:
    sudo chkconfig --level 2345 sshpipe on
    4) Test
    sudo service sshpipe start
    

    Run command as different user

    sudo -u username command

    Take 2nd string split by ":"

    echo -e "1:2:3\n4:5:6" | awk -F':' '{print $2}'

    Lambda in python

    t = [x*2 for x in range(1,10) if x>2] # [6, 8, 10, 12, 14, 16, 18]
    r = filter(lambda x: x % 4 == 0, t)   # [8, 12, 16]
    

    Class in python

    class Example(object):
      def __init__(self, name):
        self.name = name
      def __repr__(self):
        return self.name
      def printName(self):
        print self.name
    print Example("test").name
    print Example("test")
    Example("test").printName()
    

    Debugging c++11 threads in g++

    g++ -pthread -g -std=c++0x -W -Wall -Wextra -pedantic main.c
    valgrind --tool=helgrind ./a.out
    

    How to use power DNS (pdns) DNS cache server

    Configure:
    /etc/pdns-recursor/recursor.conf
    Especially:
    allow-from=127.0.0.1
    disable-packetcache=yes
    forward-zones-recurse= .=your_outside_dns_ip1, .=your_outside_dns_ip2
    Add in /etc/resolv.conf
    nameserver 127.0.0.1
    Start:
    pdns_recursor
    Monitor:
    rec_control get-all | grep -e ^cache-hits -e ^cache-misses
    

    List all files that are older than 42 days

    find -mtime +240 -exec ls -laF {} \;

    Commit only some changes in git

    git add -p <filename>
    # Choose the chunks to commit: y/n
    git commit
    

    Grep only .c files

    grep -r --include \*.c USE_NO_REUPLOADS *cvmfs*

    Search all man pages for string

    man -K <search_string>

    Check what CPU you have

    lscpu
    x86info # more detailed but not standard
    

    Grep/cat file after row 666

    cat yourfile.txt | sed '1,666d' | grep search_word

    Pause mplayer on start at time 666 seconds

    echo pause | mplayer -slave -ss 666 media.avi

    List subprocess pids

    jobs -p

    Kill child process in bash when ctrl+c pressed

    function handle_sigint() {
      for subpid in `jobs -p`; do kill ${subpid} done
    }
    trap handle_sigint SIGINT
    

    Check which git branches have been merged already

    git branch --merged
    git branch -d your-merged-branch
    git push github :your-merged-branch # Github specific
    

    Develop code using some not-yet-merged pull requests

    git checkout -b devel-integ
    git merge not-yet-merged-branch not-yet-merged-branch2
    

    rm files from local directory when Argument list too long - error

    find . -type f -name "*.dd" -print0 | xargs -0 rm

    Start program in the background without screen

    nohup ./test_program
    nohup ./test_program > foo.out 2> foo.err < /dev/null &>
    

    Check what threads and processes emerge during program run

    ps -e -T -o pid,ppid,cmd > first.txt
    ./your_program_run
    ps -e -T -o pid,ppid,cmd > second.txt
    diff first.txt second.txt
    

    Show tree of user processes

    pstree ssheikki

    Print gdb value in hex

    p/x value

    Run several commands in gdb at once

    (gdb) define fc
    Type commands for definition of "fc".
    End with a line saying just "end".
    >finish
    >c
    >end
    

    Compare floats in bash

    load_threshold=0.1
    boxload=`uptime | sed 's/.* load average: \([^,]*\).*/\1/'`
    overloaded=`bc <<< "${boxload}>${load_threshold}"`
    if [ "${overloaded}" -eq "1" ]; then
      echo "Overloaded."
    fi
    

    Disable SELinux

    sestatus
    setenforce permissive
    

    Grant access to Kerberos authenticated machine

    emacs /root/.k5login

    Nscd statistics

    nscd -g
    If your "cache hits on positive entries" is always zero, change /etc/nscd.conf:
    shared                  hosts           no
    to see if it is working. Shared = yes means that clients search the nscd cache
    directly and do not go through daemon.
    

    Replace char in bash

    For example 'a' with 'b' in ${mywar}:
    newmyvar=${myvar//a/b}
    

    Check for object attached code

    objdump -S compiled_object.o

    Enable Redhat/SL package repositories with yum

    yum repolist all
    yum-config-manager --enable <the_repo_you_want>
    

    Take stuff from git stash

    git stash list
    git stash show -p stash@{0}
    See changes for certain file
    git diff stash@{0}^! -- <yourfile>
    git checkout stash@{0} -- <yourfile>
    git show stash@{0}:<path/yourfile> > newfile
    

    google-perftools cpu profiling

    g++ main.c -o myprogram -lprofiler
    CPUPROFILE=/tmp/profile ./myprogram
    pprof --text ./myprogram /tmp/profile
    pprof --gv ./myprogram /tmp/profile
    Example output:
      Total: 350 samples
      90  25.7%  25.7%       92  26.3% add
      71  20.3%  46.0%      227  64.9% method1
      66  18.9%  64.9%       69  19.7% multiply
      48  13.7%  78.6%       48  13.7% add2
      45  12.9%  91.4%       45  12.9% multiply2
      30   8.6% 100.0%      123  35.1% method2
    
    Shows that method1 took 64.9% of the cpu and method2 35.1%.

    Report memory and cpu consumption every 5 seconds

    vmstat 5

    CPU usage profiling with callgrind (part of valgrind)

    valgrind --tool=callgrind <yourprogram>
    # Monitor what is happening online
    while [ 1 ]; do callgrind_control -b;done
    qwhile [ 1 ]; do clear; callgrind_control -b; sleep 0.1;done
    Example output:
    Frame: Backtrace for Thread 1
    [ 0]  multiply(float) (5419396 x)
    [ 1]  method1() (1 x)
    [ 2]  main (1 x)
    

    Limit network ping time for certain IP with tc

    tc qdisc del dev eth0 root # Return to normal
    tc qdisc add dev eth0 root handle 1: prio
    tc qdisc add dev eth0 parent 1:1 handle 2: netem delay 100ms
    tc filter add dev eth0 parent 1:0 protocol ip pref 55 handle ::55 u32 match ip dst IP.IP.IP.IP flowid 2:1
    

    Limit network bandwidth to XXkpbs for certain IP with tc

    tc qdisc del dev eth0 root
    tc qdisc add dev eth0 root handle 1: htb
    tc class add dev eth0 parent 1: classid 1:6 htb rate XXkbps
    tc filter add dev eth0 protocol ip parent 1: prio 1 u32 match ip dst IP.IP.IP.IP flowid 1:6
    

    Check how long process has been running in Linux

    ps -p <PID> -o etime=
    ps -p 22488 -o etime=
    01:33
    

    Get preprocessed source code of C++

    g++ -E main.cc # Outputs to stdout
    # See also -S (assembler code)
    

    Get directory of where executed bash script is located

    location_dir=$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )

    Install zsh shell

    apt-get install zsh
    chsh -s /bin/zsh
    curl -L http://install.ohmyz.sh | sh
    # reboot
    echo $SHELL
    

    Comment/uncomment area in emacs

    Ctrl-Space # Select starting point
    # Move to other end of the area
    M-;
    

    Monitor system calls made by process

    strace -c # count system calls
    # e.g. futex:es (fast userspace mutex)
    

    Check if there are TCP packets that are not acknowledged

    netstat
    # Proto Recv-Q Send-Q Local Address               Foreign Address             State
    # tcp        0      0 ssheikkicga.cern.ch:ssh     pb-d-128-141-48-87.ce:37832 ESTABLISHED
    # Check the Send-Q
    

    Copy files with exclude pattern

    rsync -av --progress --exclude="build*" srcdir destdir/

    Debug python program gdb style

    python -m pdb program_name.py
    b 666
    r
    

    Copy only jpg-files and keep the directory structure

    mkdir w
    for i in `find . | grep -i jpg$`; do
      k=`echo $i | sed 's/\(.*\)\/.*/\1/'` && mkdir -p w/$k && cp $i w/$k/;
    done
    

    Use cgroup to limit disk IO

    grep cgroup /proc/mounts  # Check where mounted
    cat /proc/cgroups         # List available cgroups
    cgcreate -g blkio:/iothrottle
    ll /dev/dm-0 # Check disk major and minor number e.g. "253, 0"
    cgset -r blkio.throttle.read_iops_device="253:0 2" iothrottle
    echo $$ # get the PID, e.g. 6666
    echo 6666 > /cgroup/blkio/iothrottle/tasks
    # Note! ext3 is mounted in default with "data=ordered", this
    # means that writes are flushed to disk in the order they are
    # done, meaning slowest will slow down others also
    

    Monitor data going through pipe

    dd if=/dev/zero bs=1M count=1000 | pv > test.txt
    0.977GB 0:00:09 [ 107MB/s] [     <=>  ]
    

    Analyse source code coverage, e.g. test coverage

    gcc -Wall -fprofile-arcs -ftest-coverage main.c # creates main.gcno
    ./a.out     # creates main.gcda
    ./a.out     # updates main.gcda
    gcov main.c # creates main.c.gcov, which shows how many times
    # each line of code was executed during (./a.out)s
    

    Backup jolla sqlite calendar database

    cp /home/nemo/.local/share/system/privileged/Calendar/mkcal/db db
    sqlite3 db
    sqlite> .table
    Alarm             Components        Recursive
    Attendee          Customproperties  Timezones
    Calendars         Rdates            Version
    sqlite> SELECT Name FROM Calendars;
    Default
    Birthdays
    sqlite> SELECT count(Summary) FROM Components;
    574
    

    Stress test a web page with Apache Benchmark

    # 10k downloads with 400 parallel requests
    ab -n 10000 -c 400 http://web-page.com/dir/file.html
    

    Show full hostname with last command

    last -a <wanted_username>

    Extract string between two tags, e.g. Rate tags

    sed -n 's:.*<Rate>\(.*\)</Rate>.*:\1:p' data.txt

    Pipe data through ssh to another machine

    find . -type f | ssh user@machine 'cat - > filelist.txt'

    List all files with numbers in the end of filename

    ll -d *[[:digit:]]

    Brace expansion in shell

    $ echo File-{a,b,c}.txt
    File-a.txt File-b.txt File-c.txt
    

    Extract CD directly to mp3s

    abcde

    Open port 3128 in SL6/Redhat machine

    lokkit -p 3128:tcp # lokkit = simple firewall configuration tool

    Check which package file belongs to with yum

    rpm -qf /bin/ls

    Find files bigger than 100MB

    find . -type f -size +100M

    Adjusting scanned pdf size and quality

    convert -density 300x300 -quality 80 -compress jpeg original.pdf adjusted.pdf

    Get real/actual size of file

    wc -c < yourfile.txt
    stat -c %s yourfile.txt
    

    Kill processes by name

    pgrep -l -f example  # List the processes that would be killed
    pkill -f example     # Kill the processes
    

    Check partition table of 8TB disk

    parted -l
    # If, and only if, you get: "unrecognised disk label"
    parted /dev/sdX # e.g. sdb
    (parted) mklabel gpt # e.g. msdos cannot handle 8TB
    (parted) mkpart primary 0% 100%
    (parted) print
    Disk /dev/sdb: 8002GB
    Sector size (logical/physical): 512B/4096B
    Partition Table: gpt
    Number  Start   End     Size    File system  Name     Flags
    1      1049kB  8002GB  8002GB                primary
    

    Veracrypt create a volume

    veracrypt --text -v --create /dev/sdb1  # E.g. AES + SHA-512, might take days
    veracrypt --text -v --filesystem=none --slot=1 /dev/sdb1  # Add maybe --mount-options=nokernelcrypto
    mkfs.ext4 -m0 -T largefile /tmp/.veracrypt_aux_mnt1/volume
    tune2fs -l /tmp/.veracrypt_aux_mnt1/volume  # See what was done
    mount -o loop /tmp/.veracrypt_aux_mnt1/volume /mnt
    /tmp/.veracrypt_aux_mnt1/volume
    7.3T   51M  7.3T   1% /mnt
    

    Reboot freezed Linux computer

    https://en.wikipedia.org/wiki/Magic_SysRq_key
    echo 1 > /proc/sys/kernel/sysrq
    Alt-PrintScreen-s # Sync filesystems
    Alt-PrintScreen-b # Reboot machine
    

    Epic software development tool set

    Jira + Agile plugin (or redmine)  # Bug and feature tracking + SCRUM/KANBAN
    Jenkins + Docker + gtest + cmake  # CI an testing
    git + pull requests               # Code review
    

    Changing AFS directory rights

    $ fs la public
    Access list for public is
    Normal rights:
    system:administrators rlidwka
    system:anyuser rl
    ssheikki rlidwka
    $ fs sa private system:anyuser none # Anyuser cannot do anything
    $ fs la private
    Access list for private is
    Normal rights:
    system:administrators rlidwka
    ssheikki rlidwka
    

    Change Gnome Alt+Tab grouping behaviour

    compizconfig-settings-manager
    Step 1/2) Desktop -> Ubuntu Unity Plugin
    Open Switcher tab and make key to start the switcher and key to start the switcher in reverse disabled.
    Step 2/2) Window Management -> enable Static Application Switcher
    Open Application Switcher and change key bindings of "Next Window (All windows)" to Alt-Tab and "Prev window (All windows)" to Shift-Alt-Tab.
    

    Show computer CPU architecture

    hwloc-ls --of ascii
    lstopo --of png > topo.png
    

    Shortcuts for desktop manager (e.g. enlightenment)

    xterm -fa default -fs 10
    xterm -fa default -fs 10 -e "ssh lakka.kapsi.fi"
    

    Setup thin clients with LTSP (Linux Terminal Server Project)

    sudo apt-get install ltsp-server-standalone
    sudo ltsp-build-client
    # Configure dhcp and boot client server with PXE.
    

    Open SOCKS proxy to some server

    In the server:
    ssh -N -D 0.0.0.0:4241 ssheikki@machines-whose-network-you-want-to-use.com
    

    Netmasks explained

    x.y.z.32/29 means 2^3=8 IPs starting from 32. So x.y.z.32-x.y.z.39.

    Ways to limit CPU power usage

    1) Frequency scaling, e.g. 2.9GHz:
    echo 2900000 | tee /sys/devices/system/cpu/cpu*/cpufreq/scaling_max_freq
    2) RAPL, e.g. 66W
    power_gov -r POWER_LIMIT -d PKG -s 66
    3) cpulimit, e.g. 66% limit for PID 6666:
    cpulimit -l 66 -p 6666
    

    Fake IP to point to localhost

    themachine$ sudo iptables -t nat -A OUTPUT -p all -d 192.168.111.11 -j DNAT --to-destination 127.0.0.1
    privateVM$ ssh -v -N -R 49490:localhost:22 -2 themachine
    themachine$ ssh -p 49490 192.168.111.11
    

    Enable two finger paste in touchpad of Dell XPS

    synclient TapButton2=2
    $ synclient | grep -i button
    TapButton1              = 1
    TapButton2              = 2
    TapButton3              = 3
    

    Get SPF record of a domain

    $ dig cern.ch txt | grep -i spf
    cern.ch.                3600    IN      TXT     "v=spf1 a:cernmx11.cern.ch a:cernmx12.cern.ch a:cernmx13.cern.ch a:cernmx14.cern.ch a:cernmx15.cern.ch ?all"
    # ?Neutral, ~Softfail, -Fail, +Pass
    

    Check what process is using most CPU time in 5 minutes period

    perf record -F 99 -a -g -- sleep 300
    perf report --sort=comm  # Analysis
    

    List iptables with interfaces and IP addresses

    iptables -vL -n

    Append lines to one line

    ls -1 | tr '\n' ' '

    List all open tcp connections

    netstat -tnp

    Bash parse line by line

    IFS=$'\n'; for i in `nslookup iki.fi`; do echo "DBG: $i"; done

    Boot from Grub in single user mode (aka root console)

    add in the end of kernel entry keyword "single" in the grub

    Install development tools in yum

    yum groupinstall "Development Tools"

    Test raw CPU speed

    sysbench --test=cpu --num-threads=1 --cpu-max-prime=99999 run
    # total time: 215.4861s
    

    List TCP port numbers (not names) used by a server

    lsof -i -P | grep "TCP \*:"

    Replace string in a file

    echo "test=1" > demo.txt
    sed -i.bak 's/\(test=\).*/\1newparam/' demo.txt
    cat demo.txt
    test=newparam
    ls demo.txt*
    demo.txt  demo.txt.bak
    

    Profile a running process, to see what it is doing

    #!/bin/bash
    nsamples=1
    sleeptime=0
    pid=$1
    for x in $(seq 1 $nsamples); do
      gdb -ex "set pagination 0" -ex "thread apply all bt" -batch -p $pid
      sleep $sleeptime
    done | \
    awk '
      BEGIN { s = ""; }
      /^Thread/ { print s; s = ""; }
      /^\#/ { if (s != "" ) { s = s "," $4} else { s = $4 } }
      END { print s }' | \
    sort | uniq -c | sort -r -n -k 1,1
    Example output:
    291 pthread_cond_wait
    15  select
    

    Profile program to see what function calls it is doing

    perf record -F 100000 <yourbinary>
    perf report | grep <libThatYouAreInterested>
    Example output:
    0.06%  xrootd   libXrdServer.so.2.0.0  [.] XrdOfsFile::write
    0.03%  xrootd   libXrdServer.so.2.0.0  [.] XrdOssAioWait  
    

    Configure email server with postfix

    # apt install postfix
    # cat /etc/postfix/virtual  # Associate to external email and to local account
    @seppoheikkila.com ssheikki ssheikki@kapsi.fi
    # grep -e my -e virtual /etc/postfix/main.cf
    myhostname = mail.seppoheikkila.com
    virtual_alias_domains = seppoheikkila.com
    virtual_alias_maps = hash:/etc/postfix/virtual
    # cat /etc/mailname
    seppoheikkila.com
    # postmap /etc/postfix/virtual  # Run to update the virtual info
    

    Configure fail2ban to protect ssh

    # apt install fail2ban
    # cat /etc/fail2ban/jail.local
    [DEFAULT]
    ignoreip = 127.0.0.1/8 217.30.184.0/24 128.141.232.250/24 46.127.0.0/16
    bantime = 600
    maxretry = 3
    destemail = ssheikki@kapsi.fi
    sender = seppo@seppoheikkila.com
    sendername = seppo
    mta = sendmail
    action = %(action_mwl)s
    # dpkg -S /usr/sbin/sendmail
    postfix: /usr/sbin/sendmail
    

    Protect ssh server config

    # grep PermitRoot /etc/ssh/sshd_config
    PermitRootLogin no
    # cat /etc/hosts.allow | grep sshd
    sshd : 127.0.0.1/8
    sshd : 217.30.184.0/24
    sshd : 128.141.232.0/24
    sshd : 46.127.0.0/16
    # cat /etc/hosts.deny | grep sshd
    sshd: ALL
    

    Test a proxy with links

    links -socks-proxy proxyurl:proxyport websiteurl:websiteport
    e.g. links -socks-proxy localhost:49495 10.5.0.13:8000
    

    Create ssh socks5 proxy to a server that is behind another server

    ssh -f -N -D <PROXY_PORT_TO_USE> -oProxyCommand="ssh -W %h:%p <MIDDLE_MACHINE_IP>" <TARGET_MACHINE_IP>

    Adjust backlight in Linux

    sudo apt install xbacklight
    xbacklight -get
    xbacklight -set 1
    

    Get timestamp in Linux with milli/micro/nano seconds

    date +%s.%3N
    date +%s.%6N
    date +%s.%9N
    

    Adjust screen brightness in Linux

    xbacklight -set 1

    Check which bios version you have

    dmidecode | grep -A 20 "BIOS Info"

    Check which framework web page is using

    http://builtwith.com/ssheikki.kapsi.fi
    http://guess.scritch.org
    

    Use exfat filesystem in Linux

    sudo apt-add-repository ppa:relan/exfat
    sudo apt-get update 
    sudo apt-get install fuse-exfat
    

    Using SOCKS proxy server

    All you need a OpenSSH server where to connect:
    ssh -C -D 1080 <your_server>
    Then set your browser SOCKS host 127.0.0.1 and port 1080.
    

    Bash key shortcuts

    Ctrl+w # delete from cursor to start of word
    Ctrl+u # delete from cursor to start of line
    Ctrl+] char # Move cursor to next "char"
    

    Copy files to Android device (Samsung Galaxy tab 3 lite)

    scp -r -P49490 yourfile ssheikki@84.73.41.59:/storage/emulated/0/Download/

    Shorten ogv video without re-encoding

    oggCut -s start_time -e end_time old.ogv new.ogv
    oggCut -s 590000 -e 1315000 old.ogv new.ogv
    

    Show file information (stored in file's inode)

    $ stat test.txt 
      File: `/tmp/test.txt'
      Size: 5               Blocks: 8          IO Block: 4096   regular file
    Device: 811h/2065d      Inode: 674650      Links: 1
    Access: (0664/-rw-rw-r--)  Uid: ( 1000/ssheikki)   Gid: ( 1000/ssheikki)
    Access: 2014-06-28 11:03:39.775420193 +0200
    Modify: 2014-06-28 11:00:46.727426532 +0200
    Change: 2014-06-28 11:06:33.739413820 +0200
     Birth: -
    

    Examine inode

    debugfs /dev/dm-0
    debugfs:  cat <23601012>
    test
    debugfs:  imap <674650>
    Inode 674650 is part of block group 82
            located at block 2622677, offset 0x0900
    debugfs:  stat <674650>
    Inode: 674650   Type: regular    Mode:  0755   Flags: 0x80000
    Generation: 3333919195    Version: 0x00000000:00000001
    User:  1000   Group:  1000   Size: 5
    File ACL: 0    Directory ACL: 0
    Links: 1   Blockcount: 8
    Fragment:  Address: 0    Number: 0    Size: 0
     ctime: 0x53ae8599:b04a3cf0 -- Sat Jun 28 11:06:33 2014
     atime: 0x53ae84eb:b8dfe484 -- Sat Jun 28 11:03:39 2014
     mtime: 0x53ae84d8:ed53ab40 -- Sat Jun 28 11:03:20 2014
    crtime: 0x53ae843e:ad6e9790 -- Sat Jun 28 11:00:46 2014
    Size of extra inode fields: 28
    EXTENTS:
    (0):2705974
    debugfs:  ex <674650>
    Level Entries       Logical          Physical Length Flags
     0/ 0   1/  1     0 -     0 2705974 - 2705974 
    

    Check ECDSA key fingerprint of ssh server

    ssh-keygen -l -f /etc/ssh/ssh_host_ecdsa_key.pub

    Remove offending key number X

    If X is e.g. 4:
    Offending key for IP in /home/user/.ssh/known_hosts:4
    sed -i '4d' ~/.ssh/known_hosts
    

    Gain root in Jolla/Sailfish OS

    devel-su # and nemo-user's password

    Install packages in Jolla/Sailfish OS

    pkcon search name nano
    pkcon install nano
    pkcon install jolla-fileman
    pkcon install zypper
    pkcon install python
    pkcon install gcc gcc-c++
    

    Setup new repositories for Jolla/Sailfish OS

    ssu ar mer-tools
    ssu ur
    pkcon refresh
    pkcon install less
    zypper ar -f http://sailfish.openrepos.net/basil/personal-main.repo
    rpm --import http://sailfish.openrepos.net/openrepos.key
    zypper refresh
    zypper install harbour-warehouse
    zypper ar -f http://sailfish.openrepos.net/NielDK/personal-main.repo
    rpm --import http://sailfish.openrepos.net/openrepos.key
    zypper refresh
    # Install now software, e.g. wget, through warehouse app
    

    Import N900 VCF/Vcard files with notes to Jolla/Sailfish OS

    # @N900
    osso-addressbook-backup -e contacts.vcs
    dos2unix contacts.vcs # This is must!
    # @Jolla: Replace unsupported NOTE field e.g. with ORG:
    sed 's/NOTE:/ORG:/' contacts.vcs > contacts.vcf
    # Use jolla-fileman to open the contacts.vcf
    

    View gpx files in Linux

    viking file.gpx

    Get DNS names for all unique IP addresses in apache2 log file

    cat /var/log/apache2/access.log | awk '{print $1}' | sort | uniq -c | awk '{print $2}' | xargs -L 1 host

    Backup Jolla phone

    1) Contacts: After backup, found in /home/nemo/.vault/People/data/all.vcf
    2) Photos: Copy directly from /home/nemo/Pictures/
    3) Videos: Copy directly from /home/nemo/Videos/
    4) Calendar: config davCal account, sync to ownCloud (e.g. https://box.cern.ch/), download ics file
       OR Copy /home/nemo/.local/share/system/privileged/Calendar/mkcal/db
    5) Copy other stuff from .vault just in case:
       tar cvf ../vault.tar A* B* M* P*
       scp nemo@<IP>:vault.tar .
    

    List Outlook PST email subjects

    lspst outlook.pst &> outlook_files_list.txt

    List directories that do not have file(s) ending to kdenlive

    find . -mindepth 1 -maxdepth 1 -type d '!' -exec sh -c 'ls -1 "{}"|egrep -i -q "kdenlive$"' ';' -print

    Get average luminosity/brightness of a picture

    convert <your_image> -format "%[mean]" info:
    convert <your_image> -colorspace hsb -resize 1x1 txt:-
    

    Extracting sound track from video

    Use vlc, export and choose mp3 only.

    List home dir hidden files and folders, but not ..

    du -skhc .[!.]*

    List duplicate file names in dirs

    find dir1 dir2 -type f -printf "%f\n" | sort | uniq -c | grep -v " 1 "

    Increase mp3 audio volume

    sox --show-progress -v 10 in.mp3 out.mp3

    Format exfat in Linux

    mkfs.exfat -n <volume_label> /dev/sddX
    fsck.exfat /dev/sddX  # Check all is ok
    exfatfsck 1.0.1
    Checking file system on /dev/sdd1.
    File system version           1.0
    Sector size                 512 bytes
    Cluster size                128 KB
    Volume size                 931 GB
    Used space                   31 MB
    Available space             931 GB
    Totally 0 directories and 0 files.
    File system checking finished. No errors found.
    

    Updating locate database with .ecryptfs

    updatedb -l 0 -o /home/ssheikki/mlocate.db -U /home/ssheikki/
    export LOCATE_PATH="/home/ssheikki/mlocate.db"
    locate whateveryouwant
    

    Extract audio from mp4 in the original format

    ffmpeg -i input.mp4 -c:a copy -vn -sn output.m4a

    Backup iPhone (at least 5S)

    - Download pictures with iTunes to MacOS. If you need to mass delete pictures from iPhone, use Image Capture from MacOS.
    - Install "My Contacts Backup" app to the iPhone (free). It emails you vcf file of your contacts.
    - Sms from backup: sqlite3 3d0d7e5fb2ce288813306e4d4636395e047a3d28 "
    SELECT chat.chat_identifier, message.is_from_me, datetime(message.date + 978307201, 'unixepoch') as date, message.text 
    FROM chat 
    JOIN chat_message_join on chat.ROWID = chat_message_join.chat_id
    JOIN message on message.ROWID = chat_message_join.message_id
    order by message.date" > sms.txt
    - Notes from iTunes backup: sqlite3 ca3bc056d4da0bbf88b5fb3be254f3b7147e639c "select zcontent from znotebody" > notes.txt
    

    Find all mp4:s and show their resolution

    find . -type f -name "*.mp4" -exec sh -c "ffmpeg -i {} 2>&1 | grep 'Video:'" \;

    Setting up puppet managed machines

    Choose one puppet master and install:
    $ sudo apt-get install puppetmaster
    In all the others machines (clients):
    $ sudo apt-get install puppet
    $ echo "<puppetmaster-ip> puppet" >> /etc/hosts
    $ puppet agent -t
    Then again puppet master machine:
    $ echo "<puppetclient-ip> <puppetclient-name>" >> /etc/hosts
    $ puppet cert sign <puppetclient-name>
    Then again in clients:
    $ puppet agent -t
    

    Setting up VLAN (Virtual LAN) networks

    After configuring VLAN IDs to ports in the switch:
    1) apt-get install vlan
    2) modprobe 8021q
    3) configure /etc/network/interfaces 
    auto <interface>.<vlanid> e.g. "auto em1.1000"
    auto em1.1001
    iface em1.1001 inet static
            address 192.168.12.166
            netmask 255.255.255.0
            network 192.168.12.0
            broadcast 192.168.12.255
    4) ifup em1.1001
    

    Exexute xterm command with bigger font

    xterm -fa default -fs 12 -e "ssh yourserver.com"

    Check network card

    sudo lshw -C network

    See when server went down

    Browse the /var/log/syslog and try to find gaps in the log entries.

    Pipe output to file and show on stdout

    ls 2>&1 | tee output.file

    Start firefox in remote machine

    firefox -no-remote

    Iptables rules to use Ubuntu server as gateway for other servers

    Assumptions: 192.168.2.0/24 = internal network, em2 = external network (internet)
    iptables -t nat -A POSTROUTING -s 192.168.2.0/24 -o em2 -j MASQUERADE
    iptables -A FORWARD -s 192.168.2.0/24 -o em2 -j ACCEPT
    iptables -A FORWARD -d 192.168.2.0/24 -m state --state ESTABLISHED,RELATED -i em2 -j ACCEPT
    

    Dim screen in Linux

    xrandr --output eDP1 --brightness .4

    Backup Jolla phone (after 2.0.2.43)

    After cloud backup, e.g. Dropbox:
    1) Contacts: tmp/People/data/all.vcf
    2) Calendar:  tmp/Calendar/data/all.ics
    3) Photos: Copy directly from /home/nemo/Pictures/
    4) Videos: Copy directly from /home/nemo/Videos/
    5) Recordings: Copy directly from /home/nemo/Documents/Recordings
    

    Extact dates from MOVs (or video files)

    for i in *.MOV;do echo -n "$i: " && mediainfo $i | grep -i date |head -1;done

    Capture webcam image from command line

    sudo apt-get install fswebcam
    fswebcam --jpeg 80 -S 10 image.jpg
    fswebcam --jpeg 80 -D 3 -S 10 image.jpg  # For some webcams
    

    Make gif from jpgs with ImageMagick

    convert -delay 20 -loop 0 *.jpg out.gif
    gifsicle --resize 640x out.gif > small.gif
    

    Check what is inside a local cvs repository

    export CVSROOT=:local:/path/to/your/cvs_root
    cvs co <yourProjectName>
    cvs log
    

    Get daily comics

    dailystrips -l -a --basedir /tmp/comics dilbert wulffmorgenthaler apod userfriendly xkcd

    Resize image so that longest side is 1920 pixels

    mogrify -resize "1920x1920>" image.jpg

    Search man pages with keywords

    apropos <keyword>

    Rotate all JPGs to right orientation and set Create date as the real date

    jhead -ft -autorot *.JPG

    Check if server fingerprint mashes

    ssh -o HostKeyAlgorithms=ssh-rsa localhost

    Use still DSS ssh key

    ssh -oPubkeyAcceptedKeyTypes=+ssh-dss yourserver.com

    Play mplayer movie with utf8 subitles

    mplayer -utf8 movie.mkv

    Adjust screen brightness in Lenovo T570

    git clone https://github.com/haikarainen/light.git
    ./autogen.sh && ./configure && make && make install
    sudo /home/ssheikki/.light/light -S 1  # Set minimum brightness
    sudo /home/ssheikki/.light/light -A 5  # Increase brightness
    sudo /home/ssheikki/.light/light -U 5  # Decrease brightness
    

    Increase Firefox fonts

    Put in the Firefox address bar: 
    about:config
    layout.css.devPixelsPerPx to 1.4 (from -1)
    

    Disable ipv6 permanently

    sudo nano /etc/default/grub
    GRUB_CMDLINE_LINUX_DEFAULT="ipv6.disable=1 quiet splash"
    

    Fix enlightenment double suspend on lid close

    Use gnome-tweak-tool to disable suspend
    Power -> Don't suspend on lid close => On
    

    Switch keyboard lights off/on in Lenovo T570

    sudo echo 0 > /sys/class/leds/tpacpi\:\:kbd_backlight/brightness
    sudo echo 1 > /sys/class/leds/tpacpi\:\:kbd_backlight/brightness
    sudo echo 2 > /sys/class/leds/tpacpi\:\:kbd_backlight/brightness
    

    Record freezer/fridge/car etc running cycle

    audacity
    Transport -> Timer Record (record for one hour for example)
    Effect -> Amplify (clipping enabled and max amplification)
    View -> Fit in Window (now you should see the pattern)
    

    Forgot to use cp -p?

    touch -r orig_file copy_file

    Fill and save pdf forms in Linux

    masterpdfeditor4 
    https://code-industry.net/free-pdf-editor/
    

    Run script after a reboot

    echo "@reboot /root/my_script.sh" > cron.in
    crontab -i cron.in
    

    Choose which route network traffic is directed with metric

    interface eth0
    static ip_address=192.168.0.10/24
    static routers=192.168.0.1
    static domain_name_servers=192.168.0.1
    metric 666
    # Note! Interface with lower metric is preferred
    

    Create gif or mpeg from jpgs

    convert -delay 20 -loop 0 *.jpg out.gif
    convert -quality 100 *.jpg out.mpeg
    

    How to improve photos with Gimp

    - auto white balance
    - levels, pick gray point
    - rotate/perspective/etc
    

    Pine, IMAP and save password

    $ touch .pine-passfile
    $ grep kapsi .pinerc
    inbox-path={mail.kapsi.fi}inbox
    folder-collections=mail.kapsi.fi {mail.kapsi.fi:993/ssl}[]
    

    Terminator, open multiple terminal windows

    Multiple GNOME terminals in one window:
    terminator
    

    Browse and organise photos in folders

    Thunar

    List installed Ubuntu packages in chronological/time order

    ls -l --time=ctime --time-style=long-iso /usr/share/applications|sort -r -k6|grep "\.desktop"|sed s/'\.desktop.*'//g|awk '{print $6" "$7" "$8}'

    Rotate jpgs and set date from exif

    jhead -ft -autorot *.JPG

    Show MD (Markdown) files in terminal

    pandoc yourfile.md | lynx -stdin

    Run a version control system (vcs) command to multiple repositories

    apt-get install python-vcstool
    vcs pull -n
    vcs push -n
    

    Change interface metric value

    ifmetric wlan0 0
    The network connection defaults to the connection with the lower number.
    

    Configure touchpad middle button paste

    synclient TapButton3=2

    Tab is four spaces in emacs

    $ cat ~/.emacs
    (defun my-c++-mode-hook ()
      (setq c-basic-offset 4)
      (c-set-offset 'substatement-open 0))
    (add-hook 'c++-mode-hook 'my-c++-mode-hook)
    

    Overlay/blend three images (jpg etc)

    composite -blend 50 num1.jpg num2.jpg tmp.jpg
    composite -blend 66 tmp.jpg num3.jpg blended.jpg
    

    Kill started program if not finishing in X seconds

    timeout 2 sleep 1000

    Graphical popup screen with big font and timeout

    zenity --info --text="<span font=\"32\">Big message</span>" --timeout=2

    Change network-manager essid from terminal

    sudo nmcli device wifi connect YOURESSID password YOURPASSWORD
    # Note, you might need to add/del routers also
    

    Grep only certain files (e.g. xml files) in directory tree

    grep -r -i --include \*.xml foo *

    Remove icons from enlightenment desktop

    Settings panel -> Files -> File Manager -> Behavior -> Icons on desktop
    Settings panel -> Files -> File Manager -> Device -> Show device icons on desktop
    

    Set T570 and Asus monitor on and off

    xrandr --auto --output HDMI-2 --mode 1920x1080 --right-of eDP-1
    xrandr --output HDMI-2 --off
    

    Use vcs tool to show only changed repositories

    vcs status -s
    vcs status -s --repos
    

    Check if C++ threads are using variables without locks, aka data races

    https://github.com/google/sanitizers/wiki/ThreadSanitizerCppManual
    Simply compile your program with -fsanitize=thread and link it with
    -fsanitize=thread. To get a reasonable performance add -O2. Use -g to
    get file names and line numbers in the warning messages.
    

    Show how a file changed in last commit of git repo

    git log -p -1 HEAD z_cell.cpp

    Squash last N commits together

    squash = "!f(){ git reset --soft HEAD~${1} && git commit --edit -m\"$(git log --format=%B --reverse HEAD..HEAD@{1})\"; };f"

    Restart enlightenment from TTY console

    env DISPLAY=:0 enlightenment_remote -restart

    Rename Windows filenames to Linux

    Run in the directory where you want to do the rename.
    convmv -r -f windows-1252 -t UTF-8 .
    The above does not do the rename yet (dry run). Add
    --notest to do the real.
    

    Encrypt external HD with LUKS

    # Encrypt disk
    cryptsetup luksFormat /dev/sdc1 --align-payload 2048
    cryptsetup luksHeaderBackup --header-backup-file header_file.txt /dev/sdc1
    cryptsetup luksOpen /dev/sdc1 disk-enc
    mkfs.ext4 -m0 /dev/mapper/disk-enc
    cryptsetup luksClose disk-enc
    
    # Basic usage
    cryptsetup luksOpen /dev/sdc1 disk-enc
    mount /dev/mapper/disk-enc mnt
    umount mnt
    cryptsetup luksClose disk-enc
    

    Rotate pdf 90 degrees

    # clockwise:
    pdftk input.pdf cat 1-endeast output output.pdf
    # anti-clockwise:
    pdftk input.pdf cat 1-endwest output output.pdf
    

    Loop through file in bash

    while read line; do
      echo $line
    done <input_file.txt
    

    Screen buffer to file

    Ctrl + A and :
    hardcopy -h <your_filename>
    

    Manage passwords in Ubuntu and in Jolla

    sudo apt install keepassx
    scp keepassx_database.kdbx nemo@jolla:Documents/ownkeepass/
    

    Cut in the end of the file in nano

    Esc+T

    Backup of iPhone 5S pictures

    Use Image Capture from MacBook Air to Import All pictures and
    videos to some folder, e.g. "Movies". A full size image is are
    3264 x 2448, taking around 2MB.
    

    Bluetooth headset to work

    sudo apt install bluetooth bluez bluez-tools rfkill blueman pavucontrol
    sudo rfkill list               # Check that bluetooth is not blocked
    sudo rfkill unblock bluetooth  # Unblock bluetooth, if needed
    sudo service bluetooth start   # Start bluetooth service
    sudo blueman-manager           # Pair devices
    pavucontrol                    # Set bluetooth headset as output device
    

    Get directory and filename from full path

    dirname <full_path>
    basename <full_path>
    

    Copy paste your current working directory to anywhere

    alias cpwd='pwd | xargs echo -n | xclip -selection clipboard'
    cpwd  # and then paste (Ctrl+v) e.g. in web browser
    

    Generate new ssh key

    ssh-keygen -b 2048 -t rsa -f id_rsa_mykey
    # You can use this key for specific server by adding in
    # ~/.ssh/config
    Host gitserv
        Hostname remote.server.com
        IdentityFile ~/.ssh/id_rsa_mykey
        IdentitiesOnly yes
    

    Su to other user and use X-environment

    grep pam_xau /etc/pam.d/su  # Add the line below to this file
    session  optional  pam_xauth.so
    # Then just login; "su desired_user"
    

    Disable the monthly asking for saving sent-mail in Alpine

    Setup > Config
    search for "Pruning Rule"
    and choose:
    "don't rename, don't delete"
    

    Remove all stopped containers

    docker container prune

    Attach to running docker container

    docker exec -it fa49af191548 bash

    Push changes in new branch at git

    git checkout -b my_new_branch
    git add -p
    git commit -m "my feature"
    git push origin my_new_branch
    

    Flush output of program on every new line

    stdbuf -oL bash my_script.bash &> my_log.txt

    Change IP address with nmcli

    nmcli dev status
    nmcli con show Ethernet
    nmcli con mod Ethernet ipv4.addresses 192.168.0.102/24
    ip a
    nmcli dev disconnect enp8s0; nmcli con up Ethernet
    

    Use nomachine to open GUI of remote computer

    /usr/NX/bin/nxclient -admin  # To where you want to connect
    /usr/NX/bin/nxplayer         # From where you want to connect
    

    Merge request to gitlab

    git checkout -b fix-new-feature
    git add -p
    git commit -m "Fix: feature works"
    git push
    git checkout master
    git pull
    git branch --merged
    git branch -d fix-new-feature
    

    Matrix shell

    sudo apt install -y cmatrix && cmatrix -sab -u5

    Find all files of size with one char and cat them

    find . -type f -size 1w -exec cat {} \;
    # to delete them
    find . -type f -size 1w -delete
    

    Delete all empty files

    find . -type f -empty
    # to delete them
    find . -type f -empty -delete
    

    Parallel bzip2 folder

    apt install pbzip2
    tar cvf my.tar.bz2 --use-compress-program=pbzip2 folder/
    

    Search all devices available in your subnet

    nmap -sL 192.168.1.0/24
    Maybe more reliable (but slower):
    subnet=192.168.1;for i in {1..254};do ping -W1 -c1 ${subnet}.$i &>/dev/null && echo ${subnet}.$i;done
    

    Create encrypted zip file (works also in Windows)

    zip -e my_archive.zip my_files*
    unzip my_archive.zip
    

    Read ics (calendar) file in Linux

    https://waynemorrison.com/software/vcal
    vcal <your_vcal_file>
    # event start:  2019-09-12        10:00   FLE Standard Time
    # event end:    2019-09-12        13:00   FLE Standard Time
    # summary: Kick-off meeting 
    

    Pair Jolla with Volvo

    In Volvo:
    - Go to "Paired phones" menu.
    - Press "exit" button on steering wheel, to display "phone menu".
    - Select "Phone settings" and "Discoverable",
      this makes car discoverable for 3 minutes.
    In Jolla:
    - Search for Bluetooth devices, you should find
      "My Volvo Car".
    - Volvo asks for keypair, write e.g. 0000.
    - Press the same keypair in the Jolla.
    [src] https://www.youtube.com/watch?v=jdNf7BBknPA
    

    Encrypt single file with openssl

    # Encrypt. Put encryption password to yourpassword.txt
    echo -n "yourpassword" > yourpassword.txt
    openssl enc -aes-256-cbc -in yourfile.txt -out yourfile.txt.openssl -pass file:yourpassword.txt
    # Decrypt
    openssl enc -aes-256-cbc -d -in yourfile.txt.openssl -out yourfile.txt
    

    Grep tar file without extracting

    tar xvjf file.tar.bz2 -O | grep -B10 -A10 -i "Search this"

    Backup Jolla Sony XA2 phone

    1) Make Dropbox backup: sailfish_backup_XXX.tar.gz
    2) Copy pictures and videos
    mount_jolla ~/mnt
    cdate=`date +%Y_%m_%d`
    mkdir -p ${cdate}_Jolla_Pictures
    cp -p -r ~/mnt/Pictures ${cdate}_Jolla_Pictures/Pictures
    cp -p -r ~/mnt/Videos/Camera ${cdate}_Jolla_Pictures/Videos
    

    DHCP server on Ubuntu

    # apt install isc-dhcp-server
    # grep INTER /etc/default/isc-dhcp-server
    INTERFACES="enx503eaa9f81be"
    # grep -v "#" /etc/dhcp/dhcpd.conf | sed '/^$/d'
    ddns-update-style none;
    option domain-name "local";
    default-lease-time 600;
    max-lease-time 7200;
    INTERFACES="enx503eaa9f81be";
    authoritative;
    log-facility local7;
    subnet 192.168.42.0 netmask 255.255.255.0 {
    option routers                  192.168.42.1;
    range   192.168.42.10   192.168.42.100;
    option domain-name-servers      8.8.8.8;
    }
    # ifconfig enx503eaa9f81be 192.168.42.1
    # systemctl start isc-dhcp-server.service
    # systemctl status isc-dhcp-server.service
    

    Jumbo frames one in Linux

    ip link set enx503eaa9f81be mtu 9014
    sysctl -w net.core.rmem_max=10485760
    sysctl -w net.core.rmem_default=10485760
    

    Monitor interface network traffic

    nload -a 5 -u M -U G enp0s31f6

    Alpine search for text in all body text

    "Hit ; (select) then t (text) then a (all text),
    and then your search phrase."
    

    Convert mkv etc. to work with Chromecast

    ffmpeg -i video_in.mkv -vcodec libx264 -strict -2 video_out.mp4
    mediainfo video_out.mp4  # Optional, check what you have
    

    View webcamera in Linux

    gst-launch-1.0 v4l2src device=/dev/video1 ! xvimagesink

    How to check which deb-packages are using other package

    E.g. codemeter
    $ apt-cache rdepends codemeter
    codemeter
    Reverse Depends:
      cvb-camerasuite
    $ aptitude -v --show-summary=all-packages why codemeter
    Packages requiring codemeter:
      cvb-camerasuite P: codemeter
    

    Revert last commit in git

    git log --oneline  # To get commit hashes
    git revert <unwanted commit hash>
    

    Enlightenment key bindings

    # Terminal with black background and bit bigger font
    xterm -fg white -bg black -fa default -fs 12
    # Connect to a server
    xterm -fg white -bg black -fa default -fs 12 -e ssh yourserver.com
    # Right click the shelf and configure shelf content, especially systray
    

    Sync two clonehero installations

    hip=192.168.0.102
    sdir="/home/ssheikki/Downloads/c h/"
    #sdir="/home/ssheikki/.config/unity3d/srylain Inc_/Clone Hero"
    rsync --protect-args --progress -a "${sdir}" ssheikki@${hip}:"${sdir}"
    rsync --protect-args --progress -a ssheikki@${hip}:"${sdir}" "${sdir}"
    

    Setup touchpad with xinput

    echo $XDG_SESSION_TYPE
    #x11
    xinput list
    # ⎜   ↳ SYNA2393:00 06CB:7A13 Touchpad            id=11   [slave  pointer  (2)]
    xinput --list-props 11 | grep Tap
    #         libinput Tapping Enabled (310): 0
    xinput --set-prop 11 310 1
    xinput --list-props 11 | grep Middle
    #         libinput Middle Emulation Enabled (328):        0
    xinput --set-prop 11 328 1
    $ cat /etc/X11/xorg.conf.d/30-touchpad.conf
    Section "InputClass"
        Identifier "touchpad"
        Driver "libinput"
        MatchIsTouchpad "on"
        Option "Tapping" "on"
        Option "TappingButtonMap" "lmr"
    EndSection
    # Install synaptics in parallel
    sudo apt install xserver-xorg-input-synaptics-hwe-18.04
    

    Download audio only from Youtube

    alias youtube-dl-audio='youtube-dl --ignore-errors --output "%(title)s.%(ext)s" --extract-audio --audio-format mp3'
    youtube-dl-audio https://<youtube_url>
    

    Get file creation time in Linux

    inode=`stat -c %i FILETOCHECK.txt` && sudo debugfs -R "stat <${inode}>" /dev/nvme0n1p2 | grep -i crtime
    crtime: 0x5ca4a2b0:df7a3b24 -- Wed Apr  3 15:10:24 2019
    

    Forgot to pull before pushing a commit

    git commit -m "Change"
    # Ooops! Forgot to pull first
    git reset --soft HEAD~1
    git pull
    git commit -m "Change"
    git push
    

    Find broken symbolic links

    find . -xtype l

    Remove trailing whitespaces

    sed -i 's/[ \t]*$//' yourfile.cpp

    Check with netcat what Content-Type charset webpage is returning

    printf 'GET /index.php HTTP/1.1\r\nHost:www.mielipiteet.fi\r\n\r\n' | nc -v www.mielipiteet.fi 80 | grep charset
    # Remember HTTP header overrides the META header!
    

    Update default charset for a LAMP webserver

    - Check mysql server setting:
    mysql> SELECT SCHEMA_NAME 'database', default_character_set_name 'charset', DEFAULT_COLLATION_NAME 'collation' FROM information_schema.SCHEMATA;
    - Check php default encoding:
    grep default_charset /etc/php/7.4/apache2/php.ini | grep -v "^;"
    - Check apache2 default encoding:
    less /etc/apache2/conf-enabled/charset.conf
    

    Setting Postfix SMTP mail server

    - Make sure that there is reverse DNS defined, e.g. in Hetzner webpage:
    nslookup 95.217.158.247
    - Make sure that you have valid ipv6 address:
    ifconfig  # In your server
    ping6 2a01:4f9:c010:7005::1  # Outside your server
    - Postfix should reply from port 25
    nc 95.217.158.247 25
    220 yourdomain.com ESMTP Postfix (Ubuntu)
    - Create SPF record for domain to authenticate email
    Create DNS TXT record like this (e.g. in joker.com):
    "v=spf1 ip4:95.217.158.247 ip6:2a01:4f9:c010:7005::1 -all"
    you can verify that the record exists:
    nslookup -type=txt yourdomain.com
    - Check that email is sent from the domain you want
    $ grep myorigin /etc/postfix/main.cf
    myorigin = /etc/mailname
    $ cat /etc/mailname
    yourdomain.com
    - When sending email, make sure X-Postfix-Sender is same as
    From sender, e.g. in php use "-f" additional parameter:
    mail($mailto, $subject, $text,
    "From: admin <" . $from . ">",
    "-f $from");
    

    Calculate statistics for numbers in a file

    sudo apt install datamash
    printf '1.2\n2\n4\n' > numbers.txt
    cat numbers.txt | sed 's/\./,/' | datamash mean 1 min 1 max 1 | sed 's/,/\./g'
    

    Create swap file to Ubuntu

    sudo fallocate -l 2G /swapfile  # 1-2 x actual memory
    sudo chmod 600 /swapfile
    sudo mkswap /swapfile
    sudo swapon /swapfile
    sudo echo '/swapfile none swap sw 0 0' | sudo tee -a /etc/fstab
    sudo echo 'vm.swappiness=10' | sudo tee -a /etc/sysctl.conf  # 10=server
    

    Upload file to post form with curl

    curl -i -X POST -H "Content-Type: multipart/form-data" \
    -F 'lahetys=Send' \  # This is upload button name
    -F "tiedosto=@testi2.txt" \  # This is file to upload
    http://www.example.com/upload.php  # This is your domain
    

    Open ssh pipe and copy files without losing connection

    # Open pipe
    ssh -p49592 -o ServerAliveInterval=5 -o ServerAliveCountMax=1 -N -R 49599:localhost:22 username@exampleserver.com
    # Copy files (start inside screen on a remote server)
    rsync --partial -e "ssh -o ServerAliveInterval=5 -p49599" yourfiles* username@exampleserver.com:
    

    Replace master branch with other branch in git

    git checkout new_branch
    git merge --strategy=ours master
    git checkout master
    git merge new_branch
    

    Firefox fonts bigger

    about:config
    layout.css.devPixelsPerPx = 2
    toolkit.legacyUserProfileCustomizations.stylesheets = True
    
    Check with "about:profiles" what is your profile directory and make file:
    $ cat ~/.mozilla/firefox/qx.default-release/chrome/userChrome.css
    @namespace url("http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul");
    /* #tabbrowser-tabs, #navigator-toolbox, menuitem, menu, ... */
    * {
        font-size: 14px !important;
    }
    /* exception for badge on adblocker */
    .toolbarbutton-badge {
        font-size: 8px !important;
    }