Tag Archives: Linux

Bash Tips for Power Users

Every Geek site needs an obligatory Bash Tips post

Copy Files Securely Between Two Machines

I used to always forget the syntax for this, until I realized that the syntax is exactly like the standard cp command. In fact, you can copy files like you normally would using scp, on your local machine. The following are equivalent:

$ cp file file.orig
$ scp file file.orig

Where they differ is, scp lets you copy files over a network, through SSH. Here’s an example:

$ scp contents.txt silver@ssh.domain.com:/tmp

This will copy local file contents.txt to /tmp on the remote machine ssh.domain.com, as user silver. Here are some more examples:

$ scp draft.pdf ssh.domain.com:

(copy draft.pdf to my home dir on remote machine. username is implied to be the same locally and remotely.)

$ scp swine.jpg rex@ssh.domain.com

(read: This will copy swine.jpg to local machine as a file named rex@ssh.domain.com. To make it go remote, append a : to the address, like above)

scp supports, among other things, compression (-C) and recursive copying of directories (-r).

$ scp -rC code/ ssh.domain.com:/archive/code_02032009

Trying to copy to a directory you don’t have permission to (/usr etc) will fail.

Don’t Get Lost Jumping To and Fro Between Directories

You can use cd - to jump to the previous (NOT parent) dir. For example:

kiwi@localhost: ~ $ cd /usr/local/share
kiwi@localhost: /usr/local/share $ cd -
/home/kiwi
kiwi@localhost: ~ $ cd -
/usr/local/share
kiwi@localhost: /usr/local/share $

Another way is using pushd/popd – A Last In First Out (LIFO) stack of dirs.

kiwi@localhost: ~ $ pushd /usr/local/share/
/usr/local/share ~

pushd is like cd but keeps note of the current dir before cd’ing into a new one. The stack of dirs is listed every time you invoke pushd (the “/usr/local/share ~” output you see above.)

kiwi@localhost: /usr/local/share $ pushd /
/ /usr/local/share ~

Stack is ordered left to right, latest push first. If we pop the first dir off:

kiwi@localhost: / $ popd
/usr/local/share /tmp ~
kiwi@localhost: /usr/local/share $

We’re back in the share dir. We can keep popping until there’s nothing left (throws an error):

kiwi@localhost: /usr/local/share $ popd
/tmp ~
kiwi@localhost: /tmp $ pushd /lib
/lib /tmp ~
kiwi@localhost: /lib $ popd
/tmp ~
kiwi@localhost: /tmp $ popd
~
kiwi@localhost: ~ $ popd
bash: popd: directory stack empty

Working with Long Lines

No need for more Bash shortcut cheat sheets, but here are some useful ones to help you work with long lines.

You can jump to the start & end of a line using CTRL+a & CTRL+e respectively. Example (* is the cursor):

kiwi@localhost: ~ $ echo al the ducks are swimming in the w*

and you want to fix the first word. You can hop to the beginning of the line with CTRL+a:

kiwi@localhost: ~ $ *echo al the ducks are swimming in the w

and now you can jump to the end of the misspelled word “al” using CTRL+Right twice to correct it:

kiwi@localhost: ~ $ echo all*the ducks are swimming in the w

Now ctrl+e to jump to the end of line:

kiwi@localhost: ~ $ echo all the ducks are swimming in the w*

Instead of backspacing every character, use ALT+Backspace to backspace entire words. You can also delete all or part of a line using CTRL+u combo. It deletes everything before the cursor. Likewise, CTRL+k wipes out everything after the cursor. I’ve developed a habit of using CTRL+e CTRL+k to delete lines.

Bash has a lot of ALT commands that let you move and manipulate words. ALT+l and ALT+u will make a word in front of the cursor lowercase or uppercase, for example. A neat one I don’t think I ever used is ALT+\ It pulls everything after the cursor left to the first non-whitespace character. Here’s an example, * is the cursor:

BEFORE:

$ my     spacebar is    *sticky

AFTER (ALT+\):

$ my     spacebar issticky

Avoid Retyping Commands & Arguments

ESC + . is very useful. Escape followed by a period will output the argument you sent to your last Bash command. Command calls themselves are outputted if they were invoked without any arguments (popd, ls, etc).

Example, unzipping a file and moving the archive to /tmp:

$ unzip archive-with-a-long-ambiguous-name-03092009-5960-1.2.5.zip
$ mv archive-with-a-long-ambiguous-name-03092009-5960-1.2.5.zip /tmp

In the mv command, the archive name was outputted by pressing ESC+. (full command being mv (ESC+.) /tmp) There was no need to type the long archive name twice.

The argument is taken from your bash history. You can keep invoking ESC+. to cycle back through all your recent command arguments. (history -c to clear)

Try not to forget this; You’ll naturally find plenty of uses for it.

Another way to avoid re-typing commands is CTRL+R. It will initiate a search of your command history. Begin typing, and watch Bash try to complete your command from previous ones you entered.

Command Getting Too Big? Send it to your Editor

Sometimes you begin writing what you think will be a simple command, only to realize that it has grown too complex for the command line, and you wish you were in your text editor.

First make sure your default editor is set. This is either in $EDITOR (export EDITOR=/usr/local/bin/vim) or elsewhere depending on the distro.

Use “fc” to open the last executed command in your editor:

ls -paul --sort=size
... ls output ...
fc

Now the ls line will be open in your editor. But what if you hadn’t executed the command yet? No problem. You’re sending off an email, but quickly realize that the command line isn’t ideal for everything:

echo -e "Dear Santa, \n\n\tIt has become evident that your fat ass is contributing to Global Warming, primarily due to the large quantity of coal you distribute annually. We hereby

No matter where you are on the line, hit CTRL+x, CTRL+e to invoke your editor, which now contains what you were typing on the cmd line.

I always find myself wanting to finish a command in vim, but unwilling to type the first few lines over, especially when I’m trying to write a for loop or any ugly multiline Bash code.

IMPORTANT: Whatever you type in your editor is executed automatically after you quit the editor.
Continue reading Bash Tips for Power Users

How to Block AIM’s Annoying ‘AOL System Msg’ in Pidgin

The following plugin for Pidgin will block the incredibly annoying and useless notifications from AOLSystemMsg on AIM.

“AOL System Msg: Your screen name (mrEman) is now signed into AOL(R) Instant Messenger (TM) in 2 locations. Click here for more information.”

To use, paste code in file, save file as blockaolsystemmsg.pl in ~/.purple/plugins/ and then open (or re-open) Pidgin and go to Tools -> Plugins (or press CTRL+U), and enable “Block AOLSystemMsg.” That should be it!

If you’re having any trouble, try going to Help -> Debug to open up Pidgin’s debug console.

#!/usr/bin/perl
# BlockAOLSystemMsg plugin tested on Pidgin 2.5.5. Put in ~/.purple/plugins/ and enable
use Purple;
our $target = 'AOL System Msg'; # case-insensitive
our $plugin_name = 'Block AOLSystemMsg'; 

%PLUGIN_INFO = (
  perl_api_version => 2,
  name => $plugin_name,
  version => "0.1",
  summary => "Blocks the screen name 'AOL System Msg'",
  description => "Ignore annoying 'your SN has signed on at 2 locations' AIM message",
  author => "Isam ",
  url => "http://biodegradablegeek.com",
  load => "plugin_load",
  unload => "plugin_unload"
);

sub loginfo { Purple::Debug::info($plugin_name, " @_\n"); }
sub minimize {
  my $r = lc($_[0]);
  $r =~ s/ //g;
  return $r;
}

sub plugin_init { return %PLUGIN_INFO; }

sub plugin_load {
  my $plugin = shift;
  $target = minimize($target);
  loginfo("Sight set on '$target'");
  Purple::Signal::connect(Purple::Conversations::get_handle(),
                          'receiving-im-msg', $plugin, \&callback, '');
}

sub plugin_unload {
  my $plugin = shift;
  loginfo('Block AOLSystemMsg Unloaded.');
}

sub callback {
  my ($acc, $sender, $msg, $flags) = @_;
  if (minimize($sender) eq $target) {
    loginfo("(BLOCKED) <$sender> $msg");
    return 1
  };
}

update: Fixed the botched code. Thanks.

Gentoo Sucks, Ubuntu Doesn’t.

I used Gentoo for a few years, and at first I loved it. Mainly because of portage, but the only distro I had experience with before Gentoo was Slackware, and I used to install packages and dependencies manually, so you can see why Gentoo would was so appealing to me.

When I first began my new job, the only distro available was Ubuntu, which deep down I hated without any real reason. I guess I saw Ubuntu as being “too user-friendly” and Mandrake-ish: Bloated and sluggish. But 10 minutes into using it, I made the decision that as soon as I get home, I’m wiping out Gentoo and installing Ubuntu.

You Learn From Compiling Apps Yourself

This is somewhat true, but I don’t believe it applies to Gentoo/portage. There’s nothing educational about watching shit scroll across the screen. None. If you want a real learning experience, try Slackware or Arch. You’ll learn if you’re forced to figure out what an app depends on, and what the most efficient compile flags are for your system. With Gentoo, the app is being compiled from scratch, but you aren’t doing any work, or research, for that matter. Running 1 command and then grabbing a bite while you wait for portage to do all the work for you isn’t going to teach you more than installing an RPM.

Gentoo’s installation isn’t going to teach you much of anything either, except maybe that patience is a virtue. The Gentoo docs are great, but each step is spoon fed to you. You’re basically copying and pasting commands so you can compile all the necessary files to get you started. After installation, Gentoo is as user-friendly as Ubuntu, even if it doesn’t seem like it at first.

Compiled Apps Are More Efficient Than Packages

Maybe. Prebuilt packages are usually compiled independently for each arch, and are already optimized, probably by people way more experienced in the field than you. Compiling your own apps can be slower if you don’t know what you’re doing, but even if you optimize your portage compile flags, the peformance difference between a prebuilt package for the specific arch vs an app compiled on that arch is minimal. There are too many drawbacks to compiling every app from scratch to make this tiny performance boost (which is just theoretical) worth it. Continue reading Gentoo Sucks, Ubuntu Doesn’t.

Script to Quickly Setup WebApp Environment and Domain

Just sharing a script I wrote to quickly deploy WordPress (and eventually a few other webapps) sites, which somebody might find useful. This uses Linode‘s API* to add the domain name to the DNS server along with some subdomains. If you’re using another server, (Slicehost, your own, etc), you can alter the dns class to use that API, or just ignore the DNS stuff completely; Its optional.

This will be updated periodically as I refactor and add support for more apps (notably Joomla and Clipshare – though this would violate their terms unless you have the unlimited license). This was written primarily because I couldn’t stand setting up another vhost and WordPress installation. There are plenty of existing deployers but I plan on adding very specific features and tweaking this for in-house work. I also wanted to try Rio (Ruby-IO). GPL license. Go nuts.

* As of 10/11, the apicore.rb file on the site has some syntactic errors in the domainResourceSave method. I sent an email out to the author about it. Problems aren’t major. You can get my apicore.rb here.

This won’t run unless you create the appropriate folder structure in /etc/mksite/. I’ll get going on this in a bit. See the code below:

Continue reading Script to Quickly Setup WebApp Environment and Domain

Top 5 Linux Apps That’ll Boost Your Productivity

These are not in any specific order. Also, some might be available on other operating systems.

Tomboy

This is the best note taking app I’ve ever used. It sits in your taskbar, doesn’t annoy you and doesn’t hog your cpu cycles or memory. When you wanna jot down something, hit a global shortcut, type away, and then close. Notes are saved as you type, and it automatically links notes together if you use CamelCase words. It’s written in C#, and still pretty young, but I’ve never had a problem with it in regard to stability or compatibility.

If your distro’s repository doesn’t have a package for the latest version (0.12.0, I highly recommend downloading a newer binary and/or install from trunk/)

Official site: http://www.gnome.org/projects/tomboy/
Subversion: http://svn.gnome.org/viewvc/tomboy/trunk/


Tilda and friends


You know those slide-down consoles in FPS games like Quake, UT, Half-Life, that you invoke by hitting tilde (~), and use to enter your leet r_picmip hacks? Tilda is a Quake style drop-down terminal that gives you the same quick access to your Linux console on any workspace. No more opening a new terminal window for every little task.

Official site: http://sourceforge.net/projects/tilda/

Tilda isn’t the only app of its kind. It’s not even the first. Check out the alternatives as well:
sjterm (“works well with Compiz”): https://gna.org/projects/stjerm/ (alt page)
Yet Another Kuake (Yakuake, for KDE): http://yakuake.uv.ro/
Kuake: http://www.nemohackers.org/kuake.php
Visor (OS X): http://docs.blacktree.com/visor/visor


RescueTime

RescueTime is a little program you download (Mac, Windows, Linux) that sits in the background and checks what windows/apps have focus, and uses this data to compile statistics about your computer habits and productivity. It creates neat graphs and shows how productive you are compared to others within a certain time frame.

The commercial versions have some great team features but the free one is enough to track your own productivity. If you’re paranoid, run it through a proxy or chew some Alprazolam or Zyprexa. It’s worth it.

An app sorta like this was an idea I had but never implemented. It was one of those wake up in the middle of the night with an epiphany, scramble to find a pen and paper to jot it down before it’s gone forever idea, that you then wake up and either find silly or just toss in the idea bin never to be thought of again. The idea stemmed from wanting to create a chart of how I spend my time and compare myself week by week. My proposed implementation was a lot simpler though. I was thinking about having it only track apps that you specify.

This differs from RT which has a gigantic db of categorized apps and lets you choose categories to tag as productive or not (i.e., rhythmbox and mplayer would go under audio/video) I like RT’s implementation.

Official Site: http://rescuetime.com/
Unofficial Linux client (works great): https://launchpad.net/rescuetime-linux-uploader


Screen


Screen is something you find on everybody’s list of Top/Fav Linux apps. If you use the console a lot, especially remotely, screen is a must have.

It keeps a persistent console session open, and lets you attach and detach from it anytime you want, which is great if you get disconnected while working over a network, or when you want to continue what you’re doing at home from work or while on the road. It also has neat features like split screen, tabbed consoles, etc.

When you first run it, you might not notice anything different, but you’re actually in a screen session. Press CTRL+a, followed by ‘?‘ to see a list of shortcuts. Tilda + screen = hacks.

Note: The CTRL+a keystroke is part of many of screen’s shortcuts. Unfortunately, it’s a shortcut in Bash that I frequently use (lets you jump to the beginning of the line), so this is annoying to me. There are ways around this but I’ve just gotten used to the workaround. To jump to the beginning of the line in screen, press CTRL+a, a

Official site: http://www.gnu.org/software/screen/

You might have it installed. If not:

sudo apt-get install screen

Also check out screenie, a wrapper for screen:

sudo apt-get install screenie


Google Calendar Prism


Digital calendars are either too lean (lack features), or are too bloated to keep open. I don’t need the email features that come with some of them, and hate the fact that they’re written in Java.

I tried a number of apps before trying web apps, and now use Google Calendar. It’s secure, fast and you can see your life anywhere. One nice feature is being able to add to or edit the calendar from your PDA or using text messages. I was initially weary of putting my calendar online, but the benefits outweigh the cons (paranoia).

Going back to desktop apps. The only decent one I’ve tried was Rainlendar, but it’s broken on Linux and it’s closed source. Besides, I only liked it because it was simple but synced with Google Cal. At the time, the only alternative I considered was keeping a tab open with Google Calendar, which I wasn’t going to do because Firefox needs to be xkill’d every few days. Then it hit me; Mozilla Prism!

Prism is (basically) a stripped down web browser that is meant to help integrate web apps onto your desktop. prism-google-calendar is a packaged Mozilla Prism setup with Google Calendar out of the box.

It runs independent of your browser and can be treated as a webApp. And since it has its own memory space, it doesn’t go sluggish with Firefox and never needs to be restarted.

I keep it open fullscreen on my second monitor, and can glance at it anytime I feel lost in life.

The only thing missing is a decent alarm feature. Javascript alert()s are shit, and I don’t want annoying emails about my events. I suppose there are hacks around the problem but I learned to glance at the calendar often and don’t need reminders so much anymore.

sudo apt-get install prism-google-calendar



Simple and Effective Productivity Tip When Working on the Computer

This falls under “Why Didn’t I Think of This Before?

Not being able to remain focused and get things done while working on a computer is common. There’s a countless number of things to do even offline. You can, and probably do:

  • Organize folders/desktop/etc
  • Finally get around to actually opening up an ebook you’ve downloaded
  • Customize your desktop (and then hours later, frustrated, revert it back to how it was originally)
  • Edit apps/recompile your kernel
  • hell, even making selection squares on your desktop using the cursor is a blast when you’re trying to avoid work.

One thing I’ve always thought of but never really pursued was to write some ADHD scripts. Blocking certain sites or having them only enabled for a fixed period of time each day, being able to hide distracting windows/icons/shortcuts while working, something that would sound very annoying alarms and spam me with pop-ups to ruin my illusions of grandeur, timers, etc.. Well, none of that happened, and I’d probably find ways to hack around any methods I implement anyway.

Satori

A few weeks ago, out of the blue, I had a sudden insight. Creating a new user account! It’s obvious.. too obvious, but I’ve never met anybody that does it. Not only was my regular account unorganized and loaded with distractions, but I normally keep Swiftweasel/Firefox open forever (almost literally) and can’t stand the memory-leak feature.

If it gets bad enough, I revert to “xkill” (kill -9) the browser and relaunch it. It helps, but I hate waiting for a few dozen (literally) tabs to load. Off-topic, but in my experience, there is no real workaround for this issue. None of the cache/memory settings do anything significant, and not keeping the browser open isn’t an option. Though I’m using Prism for some things now.

There are extensions to save your tabs and stuff, but.. meh… I’ll just suffer.

My work account:

  • Doesn’t have my fun desktop shortcuts, nor my Swiftweasel bookmarks and extensions.
  • Has the Gnome “typing break” option enabled (System->Preferences->Keyboard). This locks the keyboard for as long as you want, at any interval you want. It reminds you to take breaks and helps reduce the risk of Repetitive Strain Injury (a serious problem).I have enabled it on my regular now too. Since I type a lot when working, my work account setting for this feature is to halt the keyboard for 3 mins every 20 mins. On my regular account I halt for 10 minutes every 60 minutes + have the option to postpone a break enabled.
  • Has a high contrast/big font theme with a plain background. Actually, I now use the default Ubuntu background on all my accounts. It’s aesthetically appealing without affecting usability.
  • Different compiz-fusion settings. Despite the increased RAM usage (which isn’t a lot), Compiz-fusion can increase productivity significantly. Zoom, ADHD plugin, the window switchers and live previews, among others, are helpful, depending on what you’re working on.
  • Different widgets (see above). I use screenlets.
  • Less memory usage because I don’t need to keep my browser running and there aren’t many tabs open at once. Switching user accounts also gives me back the memory my browser hogs on my regular account, and I can restart the session when I switch back.
  • Different startup options, auto-join IRC channels and Pidgin settings, Bash aliases, shortcuts, default editors, file associations, IDE and editor settings, drives mounted (it’s good to have different partitions for different things), etc. It’s true that most editors and apps let you save profiles, but I only use that for backup or to ‘branch’ settings.

Making a new account is surprisingly tedious, but well worth it. Be sure to keep things in sync between each account if you need to.

Example to create name and password, and give the new account sudo rights:

$ sudo useradd -m -G users,video,cdrom,fuse -c "Zug Zug?" peon
$ sudo passwd peon
$ visudo

If the above is overkill, an excellent solution is to just manage multiple browser profiles. This page explains how you can manage profiles in Firefox.

Besides working less, and taking breaks, how do you stay focused on task, especially in regards to programming and computers?

How to Speed Up Fullscreen Flash Playback in Linux

Hardware acceleration is not available in fullscreen mode for Linux yet, at least not with Adobe’s Flash 9.x plugin. This isn’t much of a problem with small videos, but it results in choppy, sluggish playback in fullscreen mode. Fortunately, there’s a simple way around this while we wait for Adobe to address the issue.

Workaround

If you’re using compiz-fusion, a simple fix is to enable Zoom Desktop in the CompizConfig Settings Manager (command “ccsm” or System->Preferences->Advanced Desktop Effects Settings in Gnome/Ubuntu).

Now just zoom into the small video. You need to play around with the settings a little to make it easier to get the video centered (decrease zoom step, etc). It works perfectly.

This is actually a nice hack. It’s simple and gives you the option of zooming in further than fullscreen takes you, and you can choose certain parts of the video. Yes, you would be able to do this normally by resizing and moving the window if HW acceleration was available, but I like being able to quickly zoom in and out with the mousewheel.

Using Vim as a Complete Ruby on Rails IDE

vi traced with an optical mouse

NOTE: If you are experiencing segmentation faults with vim and rails.vim, see this post.

When coding in Ruby on Rails, you’ll usually be switching between files and running scripts a lot. It can be time-consuming and frustrating coding Rails using a traditional text editor designed for working on big files individually. Vim lets you hop around within a file with enough speed to activate the cosmic treadmill – but without a plethora of hacks and custom key mappings, it’s weak as a Rails IDE. Fortunately, for those of us who are reluctant to kick the vim habit, Tim Pope comes to the rescue with rails.vim; A plugin that makes working with Rails in vim painless and efficient. In this guide, I will explain how to install and use rails.vim, along with a few other plugins you’ll find useful when writing Rails applications.
Continue reading Using Vim as a Complete Ruby on Rails IDE