Using SCP to transfer files

Using SCP is a great way to transfer files from one machine to another. SCP uses SSH and can move files to/from localhost <-> remotehost or remotehost <-> remotehost.

SCP Syntax: localhost -> remotehost

scp /path/to/source-file user@host:/path/to/destination-file

SCP Syntax: remotehost -> remotehost

scp user@src-host:/path/to/src-file user@dest-host:/path/to/dest-file

SCP Examples

scp file.txt mydomain.com:~/mydir/file.txt
 
scp local_dir/filename myname@host1:remote_dir
 
scp local_dir/* myname@host1:remote_dir
 
scp myname@host1:remote_dir/filename .
 
scp myname@host1:remote_dir/filename  myname@host2:another_dir

Use cron and ntpdate to keep your linux server’s time updated

To sync your linux server’s time with the NTP Pool Time Servers, use the following command.

ntpdate pool.ntp.org

If you’d like to sync with a time server weekly, add a new file named ntpdate-sync to /etc/cron.weekly. Then set the file permissions to 755 and set owner:group to root:root. Finally, exit set the file contents to the following.

#! /bin/sh
/usr/sbin/ntpdate pool.ntp.org

To sync on a custom schedule, add a new file named ntpdate-sync to /etc/cron.d. Then set the file permissions to 644 and set owner:group to root:root. Finally set the file contents as follows.

# /etc/cron.d/ntpdate-sync: crontab fragment for ntpdate
# Run ntpdate at 2:30AM on the first of every month
30 2 1 * * root /usr/sbin/ntpdate 2>&1 | /usr/bin/tee -a /var/log/cron/ntpdate-sync.log

Update – 10/18/09
Changed custom schedule cron example from once every 15 minutes to once per month.

Using SVN copy to branch or tag within a repository

To “branch” or “tag” within a repository, use svn copy. The syntax is simple.

svn copy {SRC} {DST}

The source and destination can be either a working copy path or full URL in any combination. So to tag a revision from trunk the following command could be used.

svn copy --username myusername --password mypassword http://domain.com/svn/trunk http://domain.com/svn/tag/0.1.1332 -m "commit message"

SVN Clients

Mac SVN Clients

Windows SVN Clients

Recover the MySQL Root Password in Linux

Step 1 – At the linux shell, stop the current mysqld process, start the mysqld_safe process with –skip-grant-tables switch and login as root (no password).

/etc/init.d/mysql stop
mysqld_safe --skip-grant-tables &
mysql -u root

Step 2 – At the mysql shell set the root password and flush privileges.

USE mysql;
UPDATE user SET password=PASSWORD("new-password-here") WHERE User='root';
FLUSH PRIVILEGES;
QUIT;

Step 3 – Back at the linux shell stop the mysqld_safe process and start the normal mysqld process. At this point you should be able to successfully login as root using the password from Step 2.

/etc/init.d/mysql stop
/etc/init.d/mysql start
mysql -u root -p

Set MySQL Root Password

To reset the MySQL root password, login to the mysql shell and run the following commands.

SET PASSWORD FOR root@localhost=PASSWORD('RootPasswordHere');
FLUSH PRIVILEGES;

Grant MySQL Privileges

To grant all privileges to a user from any location login to the mysql shell and run the following queries.

GRANT ALL PRIVILEGES ON *.* TO username@localhost IDENTIFIED BY 'PasswordHere' WITH GRANT OPTION;
GRANT ALL PRIVILEGES ON *.* TO username@"%" IDENTIFIED BY 'PasswordHere' WITH GRANT OPTION;
FLUSH PRIVILEGES;

Apache Cache Control using mod_expires with Expires By Type

Use Apache’s mod_expires to explicitly set the expiration of a file by it’s type. This will enable to browser to cache these static assets and greatly increase performance.

<IfModule mod_expires.c>
    ExpiresActive on
    ExpiresDefault "now"
    ExpiresByType text/html "now"
    ExpiresByType text/xml "now"
    ExpiresByType text/css "access plus 8 hours"
    ExpiresByType text/plain "access plus 8 hours"
    ExpiresByType application/x-javascript "access plus 8 hours"
    ExpiresByType application/x-shockwave-flash "access plus 8 hours"
    ExpiresByType application/pdf "access plus 8 hours"
    ExpiresByType image/gif "access plus 8 hours"
    ExpiresByType image/png "access plus 8 hours"
    ExpiresByType image/jpeg "access plus 8 hours"
    ExpiresByType image/x-icon "access plus 8 hours"
    ExpiresByType video/x-flv "access plus 8 hours"
    ExpiresByType video/quicktime "access plus 8 hours"
</IfModule>

NewsGator, FeedDemon and NetNewsWire RSS Feed Readers

There are a ton a excellent desktop and web based RSS feed readers available to choose from. My favorites by far are FeedDemon for Windows and NetNewsWire for Mac.

The last few years I’ve been using both with NewsGator Online to read and sync my news feeds daily. I’ve found this to be the perfect combo, allowing me to move from one machine to the other without seeing the same news item twice.

Earlier this week it was announced that both FeedDemon and NetNewsWire would be available for FREE (previously $30 each). If you’ve been looking for a commercial quality RSS feed reader then I highly recommend you give these a shot!

NewsGator Online

NewsGator Online Screenshot

FeedDemon

FeedDemon Screenshot

NetNewsWire

NetNewsWire Screenshot

Using the ternary operator in PHP

The ternary operator is an excellent and often underutilized way to quickly evaluate a variable in place of an if/else statement. The syntax is clean and can greatly simplify code.

( expr1 ) ? ( expr2 ) : ( expr3 )

Take the following code for example where we determine how to greet a user.

<?php
    if(isset($user)) {
        echo $user->name;
    } else {
        echo "Guest";
    }
?>

The code above is simple enough but let’s see how we can improve it by refactoring using a ternary operator.

<?php echo (isset($user)) ? $user->name : "Guest"; ?>

Update 1: There is a great collection of examples over at dzone.

Update 2: Found another nice tips and tricks post over at dzone.

Update 3: One more to check out.