Tuesday, December 16, 2008

Find Usage

"Grep" through each file in a directory:
ls xargs grep -i 'STRING'

List all directories and subdirectories in a directory
find . -type d

List all files in a directory no recursion
# workaround for "parameter list is too long".
find . \( ! -name . -prune \) -name 'STRING*' -print
find . -maxdepth 1 -name 'STRING*' -print # GNU find only

Remove all files in a directory no recursion
# workaround for "parameter list is too long".
find . \( ! -name . -prune \) -name 'STRING*' -print -exec rm {} \;

Find a file with a given inode and delete it
ls -lai # lists the inodes next to the files
find . -inum 12345 -exec rm {} \; # finds and removes by inode

http://www.faqs.org/qa/qa-1381.html
http://sial.org/howto/shell/

Monday, October 13, 2008

Django Model Many-to-One

Define the models

from django.db import models

class Reporter(models.Model):
.. first_name = models.CharField(max_length=30)
.. last_name = models.CharField(max_length=30)
.. email = models.EmailField(blank=True, null=True)

def __unicode__(self):
.. return u"%s %s" % (self.first_name, self.last_name)

class Article(models.Model):
.. headline = models.CharField(max_length=100)
.. pub_date = models.DateField()
.. reporter = models.ForeignKey(Reporter, blank=True, null=True)

def __unicode__(self):
.. return self.headline

.. class Meta:
.... ordering = ('headline',)
------------------------------------------------
Add some objects

# import models from core.models
>>> from core.models import Reporter, Article
>>> from datetime import datetime # needed for inserting current time
# add some reporters

>>> r1 = Reporter(first_name="Joe", last_name="Jaz")
>>> r1.save()
>>> r2 = Reporter(first_name="Jane", last_name="Smith")
>>> r2.save()
# add some articles
>>> a1 = Article(headline="Article1", pub_date=datetime(2008,10,14))
>>> a1.reporter = r1 # can add separately
>>> a1.save()
>>> a2 = Article(headline="Article2", pub_date=datetime(2008,10,15), reporter=r2)
>>> a2.save()
# show an article's reporter
>>> a1.reporter

<Reporter: Joe Jaz>
>>> a2.reporter.first_name
'Jane'
# create an article "under" a Reporter via the Reporter object
# the 'article_set' method is generated on model creation using the lower-case model name
# the create method automatically does a save()
>>> a3 = r1.article_set.create(headline="Article3", pub_date=datetime.now())
>>> a3.headline
'Article3'
>>> a3

<Article: Article1>
# returns object with Article type
>>> a3.reporter
<Reporter: Joe Jaz> # returns object with Reporter type - the article reporter
# create an article with no reporter and then add it to a particular reporters set of articles
# the add method automatically does a save()
>>> a4 = Article(headline="Article4", pub_date=datetime.now())
>>> r2.article_set.add(a4)
# show all of the given reporter's articles
>>> r2.article_set.all()
[<Article: Article2>, <Article: Article4>]
# count the number of articles that belong to a reporter
>>> r2.record_set.count()
2
>>> r1.record_set.count()
2
# associate article 4 with reporter 1 (instead of reporter 2)
>>> r1.article_set.add(a4)
# we can assign it back and use different syntax this time
>>> a4.reporter = r2
>>> a4.save()
# we can assign multiple articles to a reporter at a time
# this syntax calls save automatically
>>> r1.article_set = [a2, a4]
# we can create an article and assign it with a reporter_id instead of the reporter object
# the reporter_id field is generated with the model
>>> a5 = Article(headline="Article5",pub_date=datetime.now(),reporter_id=r1.id)
>>> a5.save()
# or reporter_id can be a string
>>> a6 = Article(headline="Article6",pub_date=datetime.now(), reporter_id="1")
>>> a6.save()
# Do a query to find reporters of article 1 and article 3
>>> Reporter.objects.filter(article__in=[a1,a3])

http://www.djangoproject.com/documentation/models/many_to_one/

Sunday, October 12, 2008

Django OneToMany & ManyToMany Recursive Models

Define the models in core.models.

from django.db import models

class Place(models.Model):
.. name = models.CharField(max_length=50);
.. parent = models.ForeignKey('self', null=True);

.. def __unicode__(self):
.... return "%s > %s" % (self.name,self.parent )

class Fan(models.Model):
.. name = models.CharField(max_length=8)
.. idol = models.ManyToManyField('self', null=True)

.. def __unicode__(self):
.... return self.name

----------------------------------------------------------
Interact with a Foreign Key (One to Many)

# import Place model
from core.models import Place
# add a Place and save
world = Place(name="Earth", parent=None)
world.save()
# add two Places with the world as a parent
country1 = Place(name="US", parent=world)
country1.save()
country2 = Place(name="UK", parent=world)
country2.save()
# add two Places with the US as a parent
city1 = Place(name="Chicago")
city1.parent = country1 # make the association through an update
city1.save()
city2 = Place(name="New York", parent=country1) # associate through insert
city2.save()
# select all the places
Place.objects.all()

------------------------------------------------------
Interact with a Many to Many model

from core.models import
Fan

# Define 2 people
person1 =
Fan(name="Joe")
person1.save()
person2 = Fan(name="Jane")
person2.save()
# associate the two people
person1.idol.add(person2)

------------------------------------------------------

Monday, September 22, 2008

Java Jar Commands

Creating the File structure
# simple suggested structure
project_directory/
\
_ bin/ # binary files
\_ byte_file.class # compiled java bytecode
_src/ # source files (optional)
_META-INF/ # standard location for manifest file
\_MANIFEST.MF # manifest file containing jar variables

Editing the MANIFEST.MF
The manifest can contain variables such as the following:
Main-Class: class_name # name of a class with a "main" method that will run when jar is run.
Class-path: path/to/classes/in/package/ # path start at root of package

Create the Jar
jar -cvfm jar_name.jar path/to/manifest.mf dir/to/class/files/
jar -cvfm project.jar META-INF/MANIFEST.MF bin/
-c # compress into a jar
-v # verbose output
-f # compress to a file specified on the command line (versus to stdout)
-m # specify a path to a manifest file.

Execute the Jar
java -jar jar_name.jar

Extract a Jar file
jar -xvf jar_name.jar

Tuesday, August 19, 2008

SSH Client Persistance

Keep SSH connections from timing out
On your client, add this line to your /etc/ssh/ssh_config:
ServerAliveInterval 30

Saturday, August 09, 2008

Sound Card Info

Check what ALSA soundcards are available on a Linux system:
cat /proc/asound/cards

Check what features each ALSA card has:
cat /proc/asound/devices
# note that the left number column corresponds to a soundcard in the above list

List sound cards detected
aplay -l

Set the default sound card:
# Enter this into /etc/asound.conf or .asoundrc

pcm.!default {
type hw
card 1
}
ctl.!default {
type hw
card 1
}

Using alsamixer to select which sound card to affect:
alsamixer -c <sound_card_number_from_proc>
Using mplayer with a non-default sound device
mplayer -ao oss:/dev/dsp *.mp3 # device 0
mplayer -ao alsa:device=hw=0.0 *.mp3 # device 0
mplayer -ao oss:/dev/dsp1 *.mp3 # device 1
mplayer -ao alsa:device=how=1.0 *.mp3 # device 1


http://seehuhn.de/pages/alsa
http://ubuntuforums.org/showthread.php?t=747054

Thursday, July 17, 2008

Sendmail/Postfix Info

Sendmail view mailq:
mailq
sendmail -bp

Sendmail view verbose info:
sendmail -v

Postfix flush mail queue:
sudo postfix flush
sudo postfix -f

Remove all mail in queue: 
sudo postsuper -d ALL

Remove all mail in deferred queue: 
sudo postsuper -d ALL deferred


Postfix Info
http://www.akadia.com/services/postfix_mta.html

http://www.cyberciti.biz/tips/howto-postfix-flush-mail-queue.html
http://www.unix.com.ua/orelly/networking/tcpip/ch10_08.htm
http://shinphp.blogspot.com/2011/09/ubuntu-clean-sendmail-queue.html


Wednesday, July 09, 2008

Join UNIX Command

Join takes two sorted text files and joins them together on common keys, similar to the Join SQL statements.

Basic join syntax:
join file1 file2

Specify a delimiter other than the default of whitespace (for input and output):
join -t: file1 file2 # sets the delimiter to be a colon


Join on specific columns in each file:
join -1 1 -2 1 file1 file2 # this will make file1's first column join with the file2's first column.

Join on a specific column and output specific columns:
join -11 -21 -o 1.1 1.2 2.1 2.2 file1 file2 # the -o options takes space-seperated arguments that take the form of file.column to specify only particular columns.

Specify filler text in a result column when two keys do not match between files:
join -e "none" file1 file2 -o 1.1 1.2 2.1 2.2 # the -e option needs to be used with -o

http://www.softpanorama.org/Tools/join.shtml
http://www.computerhope.com/unix/ujoin.htm

Monday, June 02, 2008

Compile Python 2.5 From Source

Download from Python web page
http://www.python.org/

UnTAR and enter the directory
tar -jxvf Python-2.5.2.tar.bz2
cd Python-2.5.2

Configure using an alternate install dir
(use ./configure -h for config options)
./configure --prefix=/opt/python/

Build and install the software
make | tee makelog
make test | tee maketestlog
make install | tee makeinstalllog
This will create a python binary. If you specified the prefix, it will put it in the bin/ directory under the prefix directory. However, this compiles the command line python version. To get it working with Apache, preform the following.

Download mod_python
http://www.modpython.org/

Untar and enter into the directory
tar -zxvf mod_python-3.3.1.tgz
cd mod_python-3.3.1

Configure mod_python.
It needs to know where python binary to use, and the apache build tool apxs is located during the configure.

./configure \
--with-apxs=/usr/sbin/apxs \ # the apache build tool
--with-python=/opt/python25/bin/python2.5 \ # path to python binary
| tee output.config
make | tee output.make
make install | tee output.makeinstall

make install should place a binary module in /etc/httpd/modules
(or /usr/lib/httpd/modules)
The module will be called mod_python.so

Configure Apache
You will need to tell Apache to load the module by adding the following line in the Apache configuration file (httpd.conf)
LoadModule python_module modules/mod_python.so
<Directory /path/to/somewhere >
AddHandler mod_python .py
PythonHandler mptest
PythonDebug On
</Directory>

Imaging Support

To be able to manipulate images with python, you need the PIL library. Download it from http://www.pythonware.com/products/pil/
After untaring and entering the directory, install by: python setup.py install
It will copy files into your default (`which python`) python install.

http://www.modpython.org/live/current/doc-html/inst-apacheconfig.html

Sunday, April 20, 2008

Basic apache .htaccess authentication


To restrict access to a specific directory

Create a .htaccess file in the directory that you want to restrict
AuthUserFile /path/to/.mypasswds
AuthGroupFile /dev/null
AuthName "Title for password box"
AuthType Basic
require user username
require user user2 # you can add more users by setting multiple require statements
# Require valid-user  # you can allow any user listed in password files.

To generate an encrypted password
Execute this shell command which will create the file .mypasswds with a hashed password entry
htpasswd -c /path/to/.mypasswds username
New password: password
Re-type new password: password

Enable the configuration
The module mod_auth must be loaded in Apache2 config
LoadModule auth_module modules/mod_auth.so
Edit the Apache2 config files to set the AllowOverride for the directory you want to protect
<Directory "/var/www/html">
Options Indexes FollowSymLinks
AllowOverride AuthConfig
</Directory>

http://www.yolinux.com/TUTORIALS/LinuxTutorialApacheAddingLoginSiteProtection.html
http://engr.oregonstate.edu/computing/web/43
http://httpd.apache.org/docs/2.0/howto/auth.html

Thursday, April 17, 2008

Compile PHP 5 From Source

Before configuring, you must have development versions of the given packages installed. For example, for mcrypt support, you must install mcrypt-devel. In the list below, this applies to zlib-devel, libxml2-devel, mysql-devel, bzip2-devel, httpd-devel, and mhash-devel. Some configure options below didn't require a development version.

View configure options
./configure --help
Many configure options allow an optional =/path syntax to allow you to specify a non-default location for the development libraries.

Special configure notes
The --prefix config option allows you to specify a non-default location to install the php distribution (The root of the install, if you will)
The --exec-prefix directory allows you to specify an alternate location to install the compiled PHP binary files. In this example, they are set to the same location in the /opt directory.
Highly recommended to install
--with-apxs (Apache 1.x) or --with-apxs2 (Apache 2.x). This creates the Apache module called libphp5.so that is normally installed in the Apache modules directory. This module is what Apache uses to run PHP web sites (and not the command line PHP program from the --prefix directory).

Configure command
./configure \
--prefix=/opt/php5/dist \
--exec-prefix=/opt/php5/dist \
--with-mysql \
--with-pdo-mysql \
--with-zlib \
--with-bz2 \
--enable-zip \
--with-openssl \
--with-mcrypt \
--with-mhash \
--with-curl \
--with-gd \
--with-jpeg-dir \
--with-png-dir \
--with-ttf \
--with-apxs2 \
--enable-ftp \
--enable-exif \
--with-freetype-dir \
--enable-calendar \
--enable-soap \
--enable-mbstring \
--with-libxml-dir=/usr/lib

Brief explanation of given config options
--prefix=/opt/php5/dist # sets where the root of the install will be
--exec-prefix=/opt/php5/dist # sets where the root of the binary install will be
--with-mysql # add mysql support
--with-pdo-mysql # add support for php mysql database abstraction layer
--with-zlib # add support for compressing using zlib
--with-bz2 # adds support for bunzip2
--enable-zip # adds zip compression support
--with-openssl # adds openssl functionality
--with-mcrypt # adds support for mycrypt style functions (encryption)
--with-mhash # adds special hashing functions
--with-curl # adds support for cURL which
--with-gd # adds support for manipulating images with php
--with-jpeg-dir # adds support for manipulating jpegs with php
--with-png-dir # adds support for manipulating png files with php
--with-ttf # adds support for manipulating true type fonts
--with-apxs2 # adds support for apache2 module (important)
--enable-ftp # adds support for php ftp
--enable-exif # adds support for manipulating Image metadata
--with-freetype-dir # adds support for FreeType2
--enable-calendar # don't know
--enable-soap # adds support for SOAP XML stuff
--enable-mbstring # multi-byte string support
--with-libxml-dir=/usr/lib # adds XML support (required)

Compile and install PHP
make | tee output.make
make test | tee output.maketest
make install | tee output.makeinstall

Special notes about the compile
When using --with-apxs or --with-apxs2, the libphp5.so will be compiled in the "libs/" directory of the PHP source code. The make install will try to copy this automatically to your Apache modules directory. (In the case of the above example, make install placed the module in the /etc/httpd/modules directory - this is really a simlink to /usr/lib/httpd/modules/ in this case.)
The command line PHP binary will be located in the bin/ directory of the path specified by the --prefix option.

Install the mod_php shared library into apache
After a compile of php with the --with-apxs or --with-apxs2, and placement in the /etc/httpd/modules directory (as mentioned above), the module needs to be referenced in the httpd.conf
LoadModule php5_module /usr/lib/httpd/modules/libphp5.so

# Also, the following needs to be added to the httpd.conf

# Cause the PHP interpreter to handle files with a .php extension
AddHandler php5-script php
AddType text/html php

# PHP Syntax Coloring
AddType application/x-httpd-php-source phps

# Add index.php to the list of files that will be served as directory
DirectoryIndex index.php

http://dan.drydog.com/apache2php.html
http://us.php.net/manual/en/configure.php#configure.options.servers

Thursday, February 07, 2008

Grub Config

Example of menu.lst booting a kernel

title Item Title Goes here

# this first root option selects which partition to set GRUB root
root (hd0,0) # name of the partition that grub should look for the kernel image

# the second root option in the kernel statement is the root partition that Linux will mount to
kernel (hd0,4)/boot/vmlinuz ro root=/dev/sda5 # location of read only image.

initrd (hd0,4)/boot/initrd.img # location of initrd image
boot # actually boots the system with the given commands


Example of menu.lst chainloading an OS

title Item Title Goes here
root (hd0,1)
chainloader +1

Installing grub into a boot sector (within the os)

sudo grub
> root (hd0,0)
> setup (hd0)
exit