Terminal

This page is for beginners to Linux that would like to try the terminal. Below are some basic commands you can use in the terminal app to get you started.

Contents

[edit] Opening the terminal

[edit] N900

  1. Navigate to the application menu
    • If using a firmware release older than PR1.2, navigate to ‘More...’
  2. Tap ‘X Terminal’

Alternatively, use the Ctrl+Shift+X key combination even without opening the application menu.

[edit] N800 and N810

  1. Click the command button on the main page
  2. Select ‘Utilities’
  3. Select ‘X Terminal’

[edit] 770

Terminal is not installed by default on the 770. You need to install it from the Application manager first. After that it will be available in the same menu as on the N8X0.

[edit] Using the terminal

Before you start, backup your data So long as your data is backed up, then you can start to try out the terminal without any fears of losing any data. This page does not cover gaining 'root' on your device so you should come to no harm. If you are gaining root, then it would best to not only have a backup before you start tinkering, but also to familiarise yourself with how to reflash your device in extreme cases where you need to get back to where you started.

But don't be afraid of the terminal and command line, it can be a fast and powerful tool when used correctly and non-destructive commands will not harm your device or your data.

Understand what a command does before you type it Fundamentally you should understand what a command does before typing it in and pressing enter. If you have a linux installed on your desktop PC, there you can normally type the command with the parameter --help to get a list of options, i.e. ls --help, or look up manual pages if installed, with the most helpful linux command man for "manual", like:

man ls [enter]

or

man man [enter]

to find out what a command does and what are and the options it allows.

However on maemo the default shell is busybox which has lots of commands like ls built in, and most of them are crippled in functionality so don't support a really helpful --help function. Also the man command is not available on standard maemo. So maemo NITs are basically a hostile environment for beginners to learn about linux.

There are ways to mitigate this:

  • install man. The pkg is available e.g. here:[1] and also in your application manager when you got the extras-devel catalog enabled.
  • install bash (version 3 or 4), a proper fullsize shell though without many builtins. (http://maemo.org/packages/view/bash3/ http://maemo.org/packages/view/bash4/, again also in your application manager, from extras catalog.
  • install proper original tools to replace the crippled ones of busybox, when you already installed bash. Refer to e.g. User:Joerg_rw/tools

WARNING: You might feel like getting rid of supposedly useless busybox when you did the above suggested installation of bash and tools. DO NOT uninstall busybox, it's needed for maemo bootup!

If you're reluctant to do all of the above (installing man package mandb-N900 is strongly recommended anyway), you may look up general linux cmdline help here: Linux commands, and details about crippled busybox here: list of Busybox Linux commands and options for each one. Man pages online you'll find at [2], further links are listed at [3].

You find more links that give you easy as well as ambitious starts on linux shell here: Bash-Beginners-Guide "Shell Programming!" Common shell programs Bash reference BASH Programming - Introduction HOW-TO and generally under http://tldp.org/

[edit] Basics

One of the most useful yet unknown to beginners: <TAB> completes anything at any moment, If there are ambiguities, <TAB> will complete only the unambiguous part. Two times <TAB> shows all alternatives when there are ambiguities.

Try <TAB><TAB> on an empty line to get a list of all commands that are "known" to the shell (will not find commands that are not in $PATH and so would need full pathname to invoke them)

[edit] Basic commands

Some example commands, all here are non-destructive. Press enter after each command to execute it.

The terminal should open with a

~ $

To exit the terminal at any time type, exit and enter, so

~ $ exit

echo outputs/prints text to stdout:

~ $ echo "hello world"
hello world

Echo can also be used with variables, such as USER, which contains the current user’s username. Variables are prefixed with a '$':

~ $ echo $USER
user

Use uname to list the kernel version, and use the -a argument to output all known information:

~ $ uname -a
2.6.21-omap1

cat spits out an entire text file to terminal. resolv.conf (remember *NIX is case-sensitive) is where DNS servers are stored. The # hash/pound character is a comment, which means that the rest of the line is ignored.

~ $ cat /etc/resolv.conf
#nameserver 127.0.0.1

nameserver 192.168.1.1

#nameserver 4.2.2.2

Basic example of a variable:

~ $ foobar='example'; echo $foobar
example

Example of directing output to a file. Files stored in /dev/shm are stored directly in RAM and thus are deleted on shutdown:

~ $ uname -s > /dev/shm/os; cat /dev/shm/os

[edit] Tab autocompletion

In the bottom of the X terminal you find the "tab-key" (it does not exist on the hardware keyboard. The tab-key will autocomplete commands/directory/files when pressed. If you want to see what is in a specific directory you could double-tab. The tab function is a very good way and should be a habit. It not only makes using the terminal faster. It also makes sure you spelt everything correctly.

ls /home/opt/themes/sun[tab] 

will finish the word as "sunset".

ls /home/opt/[tab][tab] 

shows you what is in the opt folder, for example the "themes" folder

Another good tip is "ctrl+c". You will find Ctrl-key on the keyboard. Together with "c" it will cancel (interrupt) any previously given command (NOTE: can also be used in DOS terminals)

[Ctrl-key] c

For example, if you write "find /" it will start listing all the files on the N900. If you press ctrl+c it will stop.

[edit] Managing Files

[edit] Listing files

To list files in a directory type ls and hit enter, so

~ $ ls

To list files in a directory with permissions, owners, time, the use the long format with ls, which is ls -l:

~ $ ls -l

To list hidden files and directories

~ $ ls -la

To list all mp3 files on memory card mmc1

~ $ ls -l /media/mmc1/*.mp3

[edit] Copying files

To copy files use cp, so

~ $ cp file file2

This would make a copy of file and call it file2. Note that "file2" can be a whole other path (like /home/user/MyDocs/afolder/file2). If file2 already exists it will be overwritten.

You can also copy multiple files in one go, like so:

~ $ cp file1 file2 file3 /the/destination

Where the destination must be a directory.

[edit] Moving files

To move files use mv

~ $ mv file2 mydirectory/file2

moving file2 to mydirectory directory

You can also use mv to rename a file

~ $ mv file newname

But what happens if I want to copy stuff into the directory I am already in do I have to type out the whole directory tree again. Well you can but it is much simpler to do the following:

~ $ mv mydirectory/file .

The dot at the end of the command means the current directory.

You can also move multiple files in one go, like so:

~ $ mv file1 file2 file3 /the/destination

[edit] Removing files

To delete a file, use the rm command:

~ $ rm file

To delete a directory and all of its subdirectories and files, use the recursive option with rm:

~ $ rm -r directory

Be careful when you use the recursive delete, it will remove everything under the given directory without asking you for a confirmation.

[edit] Directories

To change directory use cd, so

~ $ cd MyDocs

To go back a level on a directory

~/MyDocs $ cd ..

To go back two levels on a directory

~ $ cd ../..

To go back three... I think you get the idea

By the way, the bit before the $, tells you the directory you are in. The symbol ~ means your home directory, however to find out which directory you are in

~ $ pwd

pwd stands for present working directory

To create a directory, use mkdir

mkdir mydirectory

And to remove an empty directory, use rmdir

rmdir mydirectory

[edit] Directory structure

One thing to note is the directory structure on a Maemo device. When you open the terminal on the device you are in the directory /home/user Equivalent to MyDocuments in Windows. The top level directory like C: drive on Windows is a / known as a root directory. Try and stay in the /home/user directory at first, or look at your memory cards which is in the directory /media. Further information about filesystem can be found at an article named N900 filesystem.

[edit] Finding files

find /media -name "*.jpg"

Finds all files ending with a .jpg, i.e. photo files in /media directory, memory card. The * is a wildcard.

[edit] Wildcards

  • * means any number (zero or more) of characters
  • ? means exactly one character
  • [a-d] means exactly one character from the range 'a' to 'd' (ie 'a', 'b', 'c' or 'd')
  • [xyz] means exactly one character from the set 'x', 'y' and 'z'
  • [a-ckp1-3] means one of 'a', 'b', 'c', 'k', 'p', '1', '2' or '3' and so on...

So

find . -name "birthday?[bg]*"

Will find any files starting with birthday, followed by any one character, then either a lowercase b or g, with * meaning any other characters if any.

If you want to stop a search, or any command for that matter press and hold Ctrl and then press c:

<Ctrl> c

To find a word in a file you use grep. For example, if you want to find the word Maemo in file mydocument you type

grep "Maemo" mydocument

[edit] Opening tar.gz files

To open a tar.gz you need to unpack it, it is an archive file, like zip files. I recommend creating a new directory before unpacking, so

mkdir mynewdirectory
cd mynewdirectory/
tar -xvfz myarchive.tar.gz

This will unpack myarchive.tar.gz into mynewdirectory/

[edit] Starting a GUI application

Everything is run from a terminal in *NIX. A GUI program is a running instance of a terminal process. Therefore the camera program can be launched. The & (ampersand) tells the shell to run the command as a background job:

~ $ /usr/bin/camera &

[edit] Show disk usage in megabytes

~ $ du -m

To show disk (memory cards, etc.) partitions and their space

~ $ df -h

To show only rootfs partition

~ $ df -h /

[edit] Show a list of installed packages

Show a list of all your programmes installed including those not visible in the Application Manager"

dpkg -l

[edit] Piping

You can do fancy things and link commands together with what is called a pipe (this symbol: | ). Press Chr on the N810 or Fn on the N900 to find the pipe | symbol. You can program the terminal to show the pipe symbol, by going to tools->settings and entering bar as a toolbar shortcut. What the pipe does is that it takes one program's output and gives it to another as input.

So using du with the sort command you can list files and sort in order of megabytes,

~ $ du -m | sort -r -n | more

du -m lists files in megabytes then pipes it to sort to sort it in order of megabytes, largest first, then the more command shows you one page of the screen at a time, pressing enter to show more pages.

Show Linux Kernel messages:

~ $ dmesg | more

ps lists running processes, you might also be interested in the interactive top command. Piped to awk which in this case says to only show the 5th column (separated by tab characters)

~ $ ps | awk '{ print $5 }'
/usr/bin/osso-xterm
/usr/libexec/gnome-vfs-daemon
bash
ps
awk

[edit] Networking

The user PATH does not include /sbin for some reason by default on NITs, which is annoying as you need to type in /sbin/ifconfig when normally just ifconfig would be sufficient. You can create a symlink to /usr/bin to make things easier:

~ $ ln -s /sbin/ifconfig /usr/bin/ifconfig

Example use of grep, which outputs the entire line matching the pattern, in this case "inet addr" which is piped into it from the output of ifconfig:

~ $ ifconfig | grep "inet addr"

This advanced example pipes from ifconfig to grep to awk and finally to cut to output the clean IP address which can then be fed into something else if desired:

~ $ ifconfig | grep "inet addr" | awk '{ print $2 }' | cut -d : -f2

[edit] Writing a shell function

A simple shell function that uses a variable and if/then/else logic:

~ $ minute() { foo=`date +%S`; echo $foo; if [ "$foo" -lt "30" ]; then echo "under half"; else echo "over half"; fi; }
~ $ minute
29
under half

Using the shell function we just wrote, outputs the current time in seconds (only the seconds part) and whether it is closer to the last minute or next minute. Shell functions are only available for the terminal session. Save in .bashrc or .profile to be persistent.

[edit] Gain root access

You might need root access in order to make some advanced changes.

[edit] Debian package management

Update local metadata detailing what apps are available in the repositories. This is important so apt-get can find the latest software:

apt-get update 

Upgrade any installed apps with newer versions (the -y is optional and just avoids it prompting you as to whether or not to proceed):

apt-get upgrade -y  DEPRECATED!

Remove any downloaded package archives. Once the package is installed the downloaded file is no longer needed. The Application Manager does this automatically but if you are using "apt-get" you'll have to do it manually:

apt-get clean

The same as "apt-get clean" but only remove package archives which are obsolete (no longer in repsitories). Having run "apt-get clean" it is pointless to run this command:

apt-get autoclean

Remove any packages which are no longer needed. These are applications/libraries which were automatically installed because of dependencies but are no longer required:

apt-get autoremove

DEPRECATED! May remove essential pkgs, see http://talk.maemo.org/showthread.php?t=70875

Install any missing dependencies (which should fix the broken packages issue I had):

apt-get -f install

Search repositories for packages by keyword:

~ $ apt-cache search nano
nano-tiny - free Pico clone
traceroute-nanog - Determine route of packets
nano - nano editor

Install the nano package (an easy to use CLI text editor):

~ $ apt-get install nano

Instead of using the repos, use wget to download a .deb package from a website:

~ $ wget http://nitstuff.appspot.com/dists/chinook/user/binary-armel/nano_2.0.6_armel.deb

Install a locally-stored Debian package:

~ $ dpkg -i nano_2.0.6_armel.deb

To extract the .deb file for any reason

~ $ dpkg -x nano_2.0.6_armel.deb /path/to/extraction/directory

To extract the control file from the .deb file

~ $ dpkg-deb -e nano_2.0.6_armel.deb

then you will find the control file named as "control" in "/home/user" if you used "sudo gainroot" to gain root access or in "/root" if you used "root" command to gain root access

[edit] Further reading

There are infinite possibilities with the terminal, I'd recommend using google if you want to find out more or look at Busybox commands or learning the shell. Again to reiterate please make sure you backup your data before tinkering and when you want to try a new command, make sure you understand what it does first.