Tuesday, May 7, 2013

Haml comments

I've had to look this up about three times which means I should add it to the blog.

This technique was found at Makandra Cards.

To comment out code such that haml does not process it (it generates no HTML):

-# = link_to 'foo'

Wednesday, December 5, 2012

Ruby one liners

One liner to edit a file in place

This command edits a file in place, performing a global text search/replace. The -p switch tells ruby to place your code in the loop while gets; ...; print; end. The -i tells ruby to edit files in place, and -e indicates that a one line program follows.

ruby -p -i -e '$_.gsub!(/248/,"949")' file.txt

One liner to remove DOS line endings from a file

A Unix or Mac text file uses a single line feed character to mark the end of a line (hex 0A). Files created on DOS or Windows use two characters, carriage return and line feed (hex 0D 0A). This command edits a file in place, removing the carriage returns.

ruby -p -i -e '$_.gsub!(/\x0D/,"")' file.txt

One liner to edit files in place and backup each one to *.old

ruby -p -i.old -e '$_.gsub!(/248/,"949")' *.txt

Saturday, November 3, 2012

Finding files owned by a user or group

To find all files from the current directory owned by user "john":

find . -user john -print

To find all files from the current directory owned by group "john":

find . -group john -print

You can also use numeric IDs (-uid or -gid) instead. The find command searches hidden files and directories automatically (those with names that start with a period).

Saturday, August 11, 2012

Finding the size of directories and subdirectories

I can't take credit for this sequence of commands. I found several variations through search engines. I apologize for not linking to the site(s) where I found it. I can't find them. However, it is useful enough to document for posterity.

ls -AF | grep \/ | sed 's/\ /\\\ /g' | xargs du -sh

Monday, July 9, 2012

Installing Python modules

Installing a Python module

Most modules come with a setup.py program that uses the distutils module import function to install a new module. Use this command in the module directory to install it:
python setup.py install

Successful installation should make the module available in either /usr/bin/ or /usr/local/bin/.

Listing installed modules

pydoc modules

Saturday, June 9, 2012

Crontab [classic]

Crontab fields
Here is a typical crontab entry and the definition of cron fields:
00 03 * * * /tmp/zing.sh
  1. minute (00-59)
  2. hour (00-23)
  3. day of the month (01-31)
  4. month of the year (01-12)
  5. day of the week (0-6 with 0=Sunday)
  6. program or command to run
An asterisk (*) in any field means run every time for this field.
Ranges (X-Y) and steps (X-Y/Z) can also be defined.

Special @reboot option
Recent versions of cron support @reboot instead of time and day settings to run a command after boot. For example:
@reboot /usr/bin/command-to-run

I am not sure if I like this option, compared to using system start up scripts, like /etc/rc.d/rc.local.

User crontabs (including the root user)
To edit a user crontab (including root):
crontab -e
To delete a crontab:
crontab -r

User crontabs are stored as text files in /var/spool/cron/.

System crontab
The system crontab is stored in /etc/crontab. It can be changed (as root) with a text editor.
The system crontab has one extra field before the program to run, which is the user to run the command as (usually root).

Thursday, June 7, 2012

Rsync [classic]

Rsync is a file and directory syncronization tool. It can be used to keep local and/or remote files syncronized. What makes rsync powerful is that it can detect changes within large files and only transfer the parts that are different. This is much more efficient than transfering entire files, especially as the file sizes get bigger.

Note: examples that use a shell use ssh with public key authentication

To synchronize a local directory with a remote one (pull), use:

rsync -r -a -v -e "ssh -l username" --delete hostname:/remote/dir/ /local/dir/

To synchronize a remote directory with a local one (push), use:

rsync -r -a -v -e "ssh -l username" --delete /local/dir/ hostname:/remote/dir/

To synchronize a local file with a remote one, use:

rsync -a -v -e "ssh -l username" hostname:/filename /local/filename

To synchronize a remote file with a local one, use:

rsync -a -v -e "ssh -l username" /local/filename hostname:/filename

To synchronize a local directory with a remote rsync server:

rsync -r -a -v --delete rsync://rsync-server.com/stage/ /home/stage/

To synchronize a local directory with a local directory (make a backup), use:

rsync -r -a -v --delete /local/dir/ /backup/dir/