Categories
Blog Command Line Featured Linux

10 Command Line Tricks I Wish I’d Always Known

One thing that Linux Guru’s and Terminal Wizards often argue is that it’s way quicker to use the command line to do anything in Linux than it is through menus and what not. While the jury is still out on this, there are a lot of things they know which do help put the ball in their court which can often take a lot of time exploring the terminal to discover. I’ve compiled a list of my favourite commands and tricks that I think that would have saved me a bunch of time if I’d been aware of them when I first ventured in to Linux.

1. Repeat Recent Commands:

Let’s say you’ve just ran a command but forgot you needed to run it as sudo, you can use !! to repeat it with the additional sudo at the beginning, as below:

alex:~$ apt-get install package
alex:~$ sudo !!
sudo apt-get install package

Additionally you’re able to use this to go back further than the last command by replacing the second ! for -n (where n is the number of commands ago you want to repeat).

alex:~$ ls
alex:~$ cd dir
alex:~$ cat file
alex:~$ !-3
ls

How about if you want to reuse the arguments from the last command but not the command itself, well it can do that too:

alex:~$ ls /home/alex/Desktop
alex:~$ cd !*
cd /home/alex/Desktop

2. Replace String In Last Command:

The use of the following command allows you to repeat the last command but replacing a string within it, useful if you made a mistake or typo. The follow example shows correcting a path for a change directory command.

alex:~$ cd /this/dir/is/wrong
alex:~$ ^wrong^right^
cd /this/dir/is/right

3. Reset:

Chances are, you’ll come across a situation where you’ve seemingly borked a terminal. Maybe you used cat on the wrong file or a program tried to export some strange characters which changed the settings. The following command should get you back to a fresh terminal quickly and you can often run it even when you don’t think you can. If the screen is really borked, try hitting enter before hand, typing reset and hitting enter again.

alex:~$ reset

4. Running A Command In The Background:

Adding an & after the command you run allows you to continue to use the terminal screen while keeping the process running. Below shows an example of opening gedit in the background but allowing me to continue using the terminal for other commands. Great for editing and compiling code with the minimal number of terminals open.

alex:~$ gedit &

Once ran it’ll give you the process ID which allows you to kill it easily once you’re done with it.

5. Exiting A Terminal & Keeping Background Processes Running:

If you’ve opened a bunch of processes in the background (as per previous command) but now you want to close the terminal screen but not these processes you can use the following to release the ownership of the processes and exit.

alex:~$ disown -a && exit

6. htop:

Similar to ‘top’ (which allows you to view tasks in real-time with other useful info such as memory and CPU usage) but it’s displayed in a much easier to understand and interactive. It allows you to kill tasks without entering its PID, search, filter, sort and a bunch of other features.

alex:~$ htop

7. Reverse History Search:

Ctrl + r

Using this will allow you to start typing and it’ll find the last command that contained the string you’re typing, hitting enter will execute that command. For example:

alex:~$ cd /home/alex/this/is/a/dir
ctrl+r & typing "a/dir"
(reverse-i-search)`': cd /home/alex/this/is/a/dir

8. Piping (Combining) Commands:

Piping commands allows you to pass the data that would usually be outputted to be directly passed as an input to another command, for example below is a list command being passed in to the less command to allow for scrolling of the data. This will work for most commands and experimentation is the best form of learning in this case.

alex:~$ ls | less

9. Auto Complete Command & File Names:

Half way through typing a command you can hit the Tab key and it’ll auto complete the word for you, if there are more than one other possible combinations a double tap of it will show you the possibilities. This also works for directory and files and can save some serious typing time.

10. Other useful Keyboard Shortcuts:

A few other keyboard shortcuts that are also useful to know (and not always as obvious as they should be):

Ctrl + c – kill current process
Ctrl + z – put current process in to the background
Ctrl + l – clear the terminal
Ctrl + a – set cursor to the start of the line
Ctrl + e – set cursor to the end of the line
Ctrl + shift + c – copy
Ctrl + shift + c – paste

Categories
Android Blog

The 5 Best Features of ICS

Data Usage & Data Limit:

Forget 3Gwatchdog, Google have built in a data monitoring tool which is also capable of cutting you off when you’ve eaten too much data (if you want it to). It gives you information on how much you’ve used, what apps have been using it and how much and a handy chart which tries to predict from your previous usage how much you’ll be using over the next coming days.

image

Static Search Bar:

Some people seem to be in two minds about the new location of the search bar on the home screens, however you can’t hate it too much since it keeps the old 4×4 grid of icons and widgets but squeezes into the top so you can have all those short-cuts you had before, and more if you were wasting 1×4 grid cells with the old search widget!

image

Face Unlock:

Face unlock isn’t really a feature many people are going to use, in all fairness, but you will impress your friends with it a few times that’s for sure. That is until they realise they can just hold up a picture of you to the camera and get in to your facebook and frape you. It’d be nice to see a more sophisticated face unlock screen but more times than not you’ll be falling back to the pattern unlock because it can’t detect your face. Despite these issue, it’s a cool option to have and could be useful when you need to unlock your phone while concentrating on something else.

Dock Folders:

The dock bar at the bottom of the home screen can now handle folder, drag an icon over one that’s already docked and you’ll create a folder which can be clicked on to view both. While some people may wish to keep their dock bar for quick access of apps and prefer only to require a single click, for others, such as myself it’s an easy way of keeping the home screen looking clean and minimalistic.

image

Updated Multi-tasking Control:

The new multi-tasking screen or app switching screen is insanely useful, just holding down the home button for a couple of seconds brings up the overlay screen. Here you can see all the apps that are running on your phone, clicking on one will bring it in to focus and alternatively you can slide the app to the left or right to shut it down completely. Google have really given a lot more control over running apps than they have before and in a simple and efficient way.

image

Categories
Blog Posts Programming Python

Python – Making Use of Google’s Text To Speech Translation Tool

Text to speech (tts) is a difficult task to get right and there are quite a few packages that show just how hard with their terrible tinny voices, these are also usually only limited to English, which for the larger part than most people would like to believe don’t find it very useful. Nevermind, Google to the rescue, their Translation tool has had the ability to also voice translations for a while now. So wouldn’t it be awesome if we could utilize this, stable, constantly developing and ultimately free utlilty? Yes? Well luckly for you it’s possible. It’s been done before with the use of JavaScript, like here but those more used to scripting and desktop programming don’t worry, you can use it too.

Basically it works by sending a request to the Google translate servers which then, pretty much instantly, reply with an MP3 file which contains the requested words in their most sexy of robot voices.

Check this URL for an example of said voice – http://translate.google.com/translate_tts?q=check+out+my+sexy+robot+voice (if it doesn’t load in your browser, open VLC, CTRL+N and paste the URL in)

First, we want to be able to get python to stream an MP3 file off the web:

More info on this can be found here, the following code was is a stripped down version of the code found at codeboje.de.

[gist][/gist]

Now we want to make a string to send :

We’re going to use command line arguments as the string which wants to be read by the Google TTS engine, the following code will grab those arguments and concatenate them into a string. It also has to be in a format like a usual Google URL, these usually replace white space for the ‘+’ sign, that’s easy to do.

[gist][/gist]

Now let’s just pass this URL to the bit we wrote to stream an MP3 and watch it go:

Full Source Code:

(Uses command line arguments as it’s input, run like ‘python ttsString.py Hello World‘)
[gist][/gist]

That’s pretty much how easy it is to use Google’s free online TTS engine, one main thing to watch out for is the 100 character limit to the use of this service in this way along with the API call limits which apply to each IP address. There are loads of cool things this can be used for and I’d love to see what anyone comes up with, keep me posted and have fun!

Categories
Blog Posts Programming Python

Python – Writing a ‘Fuzzy Clock’

If you want a clock that’s a little more human, there’s no better way than a ‘Fuzzy’ clock, essentially this converts the time in to a more brain friendly format such as “Twenty past Twelve” rather than 12:22.

Writing a simple fuzzy clock
Source Code:

[gist][/gist]

Output:

$ python fuzzytime.py
It’s 25 to 2

Writing a more readable fuzzy clock:

Now, this returns in a form of “It’s 20 past 1” rather than “It’s twenty past one”, if you’re going to want this then it’s only a little more tricky. For this I used a lookup table which would essentially replace the numbers for the written numbers.

Source Code:

[gist][/gist]

Output:

$ python fuzzytime.py
It’s two o’clock

Categories
Blog Electronics Posts

Densitron – DD-160128FC-2A – Review

Product: RGB OLED 160X128 Display
Manufacturer: Densitron
Price: £20.33 (At the time of this review.)

Datasheet: DD-160128FC-2A Datasheet | Farnell Mirror
EAGLE library: http://prusadjs.cz/eagle/OLED.lbr

The DD-160128FC-2A is a solid little screen, at around 4.3cm (from corner to corner) it’s perfect for displaying information on small mobile robots, a little serial terminal screen for desktops, wrist mounted electronics and a whole bunch more. The only issue with the design of the screen is that it doesn’t come with any mount holes, leaving the only real option of mounting this to a case or project box with glue on the back (which makes it not reusable) or sandwiching the displays edges between two layers (which could prove difficult with only a few millimetres border).

The display boasts a bright back-light thanks to the use of OLED (organic light-emitting diode) which also keep the power consumption low (3.3V input) making this an awesome little display for mobile use when a power supply isn’t available. The colours are surprisingly deep and vibrant for such a small display and there is no visible flicker from the refresh rate. The 160×128 resolution is easily enough to display around 10 lines each with 18 characters on, alternatively it would look great displaying a camera video feed or a slideshow of pictures.

An EAGLE library was created for this OLED screen which includes pin descriptions and correct sizing for those wishing to create their own PCBs for projects using this display. It can be found here.

For those wishing to use this product in conjunction with an mbed (more info here) someone (simonb, over at the mbed community) has created a driver for the “Densitron DD-160128FC-1A”, this should work perfectly well with this newer module screen and all the source code and much more information is available through the mbed website – here. This is a great library which allows you full control over colour, fills, text, individual pixels and even the orientation of the screen.

If you’d prefer not to attempt creating your own PCB and all that pesky soldering, or are not very good at soldering I strongly advise buying the breakout board for this screen (this can be found at Farnell here), it’ll allow you to develop and play around with the board much easier and it’s only around £15.

Densitron also offer a bunch of other screens with a range of sizes, resolutions and prices and due to the ease of use of their products they seem to be an ideal for hobbyists, students and anyone looking to prototype a product quickly. Check out their entire range over at Farnell – http://uk.farnell.com/densitron/

Keep tuned for more projects using this, all will include schematics and source code.

Categories
Arch Linux Blog Command Line Fedora Linux Posts Ubuntu

Switching Between Two Resolutions in Linux

I have a TV and my monitor connected to my PC by a VGA switch box but what’s annoying is they aren’t the same resolution. This causes problems because I can’t see what I’m doing when I have switched to TV and usually have to leave the setting manager open so that when I change between them I just have to hit return and the resolution changes. That’s great and all but it’s far more effort than I want for something I do fairly often.

My plan was initially to create two xorg.conf files and switch between them using a script, I figured this would be the easiest way even though I haven’t messed around with xorg stuff for a few years ever since things just started working better in Linux. Turns out I couldn’t even find where they keep the xorg.conf file, it sure isn’t in /etc/X11 where it was the last time I looked..

On the search for this file though I came across another useful tool called xrandr, which essentially allows you to change the resolution from the command line.

Xrandr is used to set the size, orientation and/or reflection of the outputs for a
screen. It can also set the screen size.

If invoked without any option, it will dump the state of the outputs, showing the
existing modes for each of them, with a ‘+’ after the preferred mode and a ‘*’
after the current mode.

There are a few global options. Other options modify the last output that is spec‐
ified in earlier parameters in the command line. Multiple outputs may be modified
at the same time by passing multiple –output options followed immediately by
their corresponding modifying options.

For more information on xrandr check out the manual page (or type man xrandr in terminal).

It turns out it’s an extremely easy tool to use, with a command as simple as the following changing the resolution:

xrandr --output VGA1 --mode 1440x900

So the next thing I did was create the following script which allows the resolution to switch between 1440×900 and 1360×768 (my monitor and my TV native resolutions).

#!/bin/bash

TV="1360 x 768"
MONITOR="1440 x 900"

TEST="$(xrandr | grep current | sed -e 's/.*current //;s/, maximum.*//')"

#echo $TEST

if [ "$TEST" == "$MONITOR" ]
        then
                xrandr --output VGA1 --mode 1360x768

elif [ "$TEST" == "$TV" ]
        then
                xrandr --output VGA1 --mode 1440x900

else

        exit

fi

This script simply checks what the current resolution is being used and then toggles between the two set resolutions.

Categories
Blog Kinect Machine Vision Posts Programming Robotics

Kinect Development – Day 1

Head over to this page if you want some tutorials on getting started with the Kinect and libfreenect, I’ll update more as time goes on and I have free time.

I’ve been meaning to grab myself an Xbox 360 Kinect for a while, not because I’m a big motion controlled game fan but for machine vision development. Within the first month of the Kinect open source drivers being released the coolest things were seen, from motion controlled media centres to 3D modelling. I’ll admit, I’m a little late to the game, mostly due to the amount of work in my final year at university and other general business. Over the summer I’ll have plenty of time to do a couple of projects and hopefully come up with something cool and contribute to the scene.

Anyway enough of the small talk, I’ve decided to blog in as much detail the journey through the development, from the installation of the libraries to writing the first and last bit of code as a sort of a set of tutorials for anyone else who wants to get into it.

There are currently two main sets of drivers/libraries out there libfreenect and OpenNI both sporting hip, cool, open source names. So which one do you choose? Well, here’s a brief description of both.

Let’s start with OpenNI, these are the Official PrimeSense (the people that Microsoft paid to actually create the Kinect) these allow access to audio, video and depth with the addition of PrimeSense’s NITE Middleware. NITE is the software library used for skeletal tracking, hand gesture recognition and scene analyzer (used to separate figures in the foreground from the background) .

Alternatively there is the libfreenect libraries, from the community over at openkinect.org. While these are admittedly lacking slightly in features such as skeletal tracking and hand gesture recognition they much make up for it in the dedication to open source and the creation of the best suite available. These have access to video, microphones, motors and LED with speakers currently being worked on. They work under a variety of language wrappers for most OS’s and will of course by my personal library of choice.

Fortunately, you won’t have to decide which one you’d prefer ’cause you can run them both on the same machine but you’ll have to look into licencing information for releasing projects with OpenNI so it’s unlikely you’ll want to combine them (or even allowed?).

libfreenect Installation:

OpenKinect’s getting started page provides a well enough documented installation guide that anyone should be able to get them up and running under Windows/Linux or OSX. With Ubuntu being the distro of choice for installation guide. – http://openkinect.org/wiki/Getting_Started

If you’re running Arch, there are a few AUR packages available however they all seem to have lacked updates for a few months but the manual build is pretty simple on the getting started page, I’ve also added a quick list of commands to get you there:

Grab the git copy of the libraries:

git clone https://github.com/OpenKinect/libfreenect.git
cd libfreenect/

Make, install:

mkdir build
cd build/
cmake ..
make
sudo make install
sudo ldconfig /usr/local/lib64/

Add your user to allow access to the connect by creating a group called video and adding your user to it:
note: this can be skipped if you don’t mind running as root/sudo

sudo nano /etc/udev/rules.d/66-kinect.rules
sudo usermod -G video username

Test the kinect with the example program:

bin/glview

If all went well you should have seen a sight similar to the screenshot above, if not check out the OpenKinect page for more information and see if the problems you’re having haven’t already been resolved.

Categories
Blog Final Year Project Posts

FYP – Pedestrian Detection – Test 2

The latest version of my final year project, almost complete. It’s designed to be a pedestrian detection system used in reversing cameras on cars. Possibly more information to come, see here – projects/finalyearproject/

Categories
Arch Linux Blog Command Line HowTo Linux Posts Ubuntu

HowTo: Remove Every Other Line in Text Files – Linux

Let’s say you’ve got a text file, of any size, big or small, and you want to remove every other line of that file, well here are a few commands in Linux that allow you to do this.

Example, you want to get from this:

1
2
3
4
5
6
7
8
9
10

To this:

1
3
5
7
9

The sed way:

 sed -n "p;N;" file.txt > newfile.txt

The awk way:

 awk 'NR%2 != 0' file.txt > newfile.txt

Here you can actually specify N lines, replace 2 in the above command and you’ll be able to take out every N’th number. As an example, here’s the above replaced with a 3 on the file:

1
2
4
5
7
8
10

Easy as pie, right?

Categories
Blog Posts Robotics

Inverted Pendulum

As part of my Robotics university course we used an Inverted Pendulum rig to learn some control applications/algorithms. For extra marks we had the challenge to swing the pendulum from hanging down to an upright balancing position. Below is my first attempt which still needs a little tweaking to the values to reduce swings needed, increase reliability and reduce travel distance of the balancing algorithm but the general idea is there and it manages to get the pendulum up and balancing in a reasonable amount of time.

Due to the project being part of the course I’m unable to release more information about it.