Difference between revisions of "CLI Spells for the Raspberry Pi"

From eLinux.org
Jump to: navigation, search
(Task $ chown Change Ownership of directories and files)
(Some useful commands)
 
(20 intermediate revisions by 6 users not shown)
Line 1: Line 1:
 
[[Category:RaspberryPi]]  
 
[[Category:RaspberryPi]]  
This Page is a Quickstart for CLI on the Raspberry PI.--[[User:Brian|Brian]] 09:13, 20 April 2012 (UTC)
+
This Page is a Quickstart for Command Line Interface on the Raspberry PI.--[[User:Brian|Brian]] 09:13, 20 April 2012 (UTC)
 
[http://elinux.org/RPi_Beginners Back to Beginners Page]
 
[http://elinux.org/RPi_Beginners Back to Beginners Page]
  
 
== Why Do I Need the CLI? ==
 
== Why Do I Need the CLI? ==
  
(CLI-Command Line Interpreter)
+
(CLI-Command Line Interface)
  
Raspberry Pi ships with a version of Linux. The CLI is run from the GUI by running a treminal or if there is no GUI running and the RPi is working, then the CLI is running on your screen.
+
The Raspberry Pi operating systems that you can download for your SD card are all versions of Linux. A graphical user interface (GUI) for Linux is provided by the X Windowing System, together with a window manager. There are lots of different window managers, for example the one being used at time of writing with the Debian "squeeze" OS is LXDE. For some OS distributions, the windowing system is started up automatically when the machine starts up, but if not then you are in the Command Line Interface, which allows you to type commands and get information back. Most of us will immediately type "startx" to bring up the GUI, but even when you are using the GUI you can still get a CLI in the form of a terminal window. You can bring this up from the start menu; usually it is called something like X Terminal.  
  
The Raspberry PI ships with a GUI. Running the XServer & Windows Manager (The two bits of software that give you a GUI) takes up a lot of Processor Power and Memory. A Raspberry Pi doing some task or other may well have no screen at all and you may manage it remotely using a remote log in to a terminal. You do not want to be running a GUI unless you need it.
+
You need the CLI because there are many things that either can't be be done using GUIs, or are much easier using the CLI. In addition there are situations when you would rather not run the X Windowing system, because it takes up a lot of processor power and memory. Also a Raspberry Pi doing some task or other may well have no screen at all, and you may manage it remotely using a remote log in to a terminal.  
  
 
Windows and Mac users are generally guided away from the CLI hence this Quick start Guide.
 
Windows and Mac users are generally guided away from the CLI hence this Quick start Guide.
 +
The commands here have mostly been described and tested using debian linux which at the time of writing (April 2012) was the version of linux that was most active in the RPi world. It is not meant to advocate this distribution in particular. The author ([[User:Brian|Brian]] 09:27, 30 May 2012 (UTC)) is actually a Fedora user most of the time.
  
 
The quick answer to "why do I need the CLI?" then, is that the CLI is going to be a very useful tool for Raspberry Pi users.
 
The quick answer to "why do I need the CLI?" then, is that the CLI is going to be a very useful tool for Raspberry Pi users.
 +
 
== Commands are just programs ==
 
== Commands are just programs ==
When you type a into the terminal you are running programs. Most of the commands listed here run programs that give you the ability to command the system to do something. When you add programs to your pi, you will be able to run more commands. This means that if a command you run and expect to work returns the message
+
When you type into the terminal you are running programs. Most of the commands listed here run programs that give you the ability to command the system to do something. When you add programs to your pi, you will be able to run more commands. This means that if a command you run and expect to work returns the message
 
$ bash: foo: command not found...
 
$ bash: foo: command not found...
  
It just means that you have not installed "foo" yet. Most of the commands described i this page are the kind of house keeping commands that come with bash. Each program has an original author who is acknowledged at the bottom of the man page.
+
It just means that you have not installed "foo" yet. Most of the commands described in this page are the kind of house keeping commands that come with bash. Each program has an original author who is acknowledged at the bottom of the man page.
  
 
To use the CLI you need to know commands and the default command set is contained in the "shell" you are using. There are Lots of shells out there and the one on this Raspberry Pi is Bash [http://en.wikipedia.org/wiki/Bash Wikipedia Bash Entry].
 
To use the CLI you need to know commands and the default command set is contained in the "shell" you are using. There are Lots of shells out there and the one on this Raspberry Pi is Bash [http://en.wikipedia.org/wiki/Bash Wikipedia Bash Entry].
Line 26: Line 28:
 
If you want to know how to use commands without using the Internet then use man (short for manual).
 
If you want to know how to use commands without using the Internet then use man (short for manual).
  
$ man <command>  
+
$ man <command>  
 
or
 
or
$ info <command>
+
$ info <command>
  
 
from the CLI to be sent to a page of guidance.
 
from the CLI to be sent to a page of guidance.
Line 38: Line 40:
 
Commands take the form  
 
Commands take the form  
  
<Command> | -<Switches> | <Parameters> | <Target>
+
<Command> | -<Switches> | <Parameters> | <Target>
  
<> are often used in Syntax guides to indicate that the <> surround a place for a command rather than an actual command. | is used to denote OR or Optional so in the line above we read that a Command can be used on its own but may be followed by  
+
<tt><nowiki><></nowiki></tt> are often used in Syntax guides to indicate that the <tt><nowiki><></nowiki></tt> surround a place for a command rather than an actual command.  
 +
<tt><nowiki>|</nowiki></tt> is used to denote OR or Optional so in the line above we read that a Command can be used on its own but may be followed by  
 
Switches (single letters, preceded by a hyphen that adjust what the command does),  
 
Switches (single letters, preceded by a hyphen that adjust what the command does),  
 
Parameters (Things that the command needs to know in order to work) and a
 
Parameters (Things that the command needs to know in order to work) and a
 
Target (The thing that the command will be applied to).
 
Target (The thing that the command will be applied to).
  
e.g. "ls -l /home/brian" means List in Long Format (the -l switch) the directory (target) /home/brian
+
e.g. "<tt>ls -l /home/brian</tt>" means List in Long Format (the <tt>-l</tt> switch) the directory (target) <tt>/home/brian</tt>
  
mount -F smbfs //workgroup;fred:foo@192.168.1.99/homes /mnt/net
+
mount -F smbfs //workgroup;fred:foo@192.168.1.99/homes /mnt/net
  
Means use the username fred and password foo (parameters) to make the shared drive called homes on the Windows server at 192.168.1.99 (parameter) appear in the directory tree at the point /mnt/net (target) using the Server Message Block Filing System (the -F switch)
+
Means use the username <tt>fred</tt> and password <tt>foo</tt> (parameters) to make the shared drive called <tt>homes</tt> on the Windows server at <tt>192.168.1.99</tt> (parameter) appear in the directory tree at the point <tt>/mnt/net</tt> (target) using the Server Message Block Filing System (the <tt>-F</tt> switch). (BTW, this assumes that you have the samba client software installed on your pi)
  
 
== Task [http://en.wikipedia.org/wiki/Cd_%28command%29 $ cd]. Navigating the file system using "cd" ==
 
== Task [http://en.wikipedia.org/wiki/Cd_%28command%29 $ cd]. Navigating the file system using "cd" ==
 
The file system in Linux is Hierarchical with nested directories. (Often called Folders) in a "Tree".
 
The file system in Linux is Hierarchical with nested directories. (Often called Folders) in a "Tree".
The top of the directory structure is "/" and Directories underneath / are referred to using "Paths" just like urls in a web browser. To go to a particular place in the directory structure you use ''cd''.
+
The top of the directory structure is "<tt>/</tt>" and Directories underneath <tt>/</tt> are referred to using "Paths" just like urls in a web browser. To go to a particular place in the directory structure you use ''<tt>cd</tt>''.
 
''e.g.''  
 
''e.g.''  
[http://en.wikipedia.org/wiki/Cd_%28command%29 cd] / will take you to the top of the Directory Tree
+
cd /
 +
will take you to the top of the Directory Tree
  
 
To go to a particular place use cd followed by the location in the tree.
 
To go to a particular place use cd followed by the location in the tree.
 +
cd /home/brian/Documents
 +
will take you to "<tt>brian</tt>'s" home directory <tt>Documents</tt> Folder. '''File & Directory Names are Case Sensitive in Linux'''
  
cd /home/brian/Documents will take you to "Brian's" home directory Documents Folder. '''File & Directory Names are Case Sensitive in Linux'''
+
To go one Directory "Up" the tree then  do  
 
+
cd ..
To go one Directory "Up" the tree then  do "cd .."
 
  
 
== Task [http://en.wikipedia.org/wiki/Ls $ ls]. Listing the files and Folders in a particular place ==
 
== Task [http://en.wikipedia.org/wiki/Ls $ ls]. Listing the files and Folders in a particular place ==
$ ls list all the file in the current directory.
+
$ ls  
$ ls -l list all the file in the current directory and display the long version of the information about each file or directory.
+
list all the file in the current directory.
The output may be colour coded depending on the terminal preferences that you have set.
+
$ ls -l  
ls -l tells you (amonst other stuff) the size, owner and security setting on each file.
+
list all the file in the current directory and display the long version of the information about each file or directory.
 +
The output may be colour coded depending on the terminal preferences that you have set. <tt>ls -l</tt> tells you (amongst other stuff) the size, owner and security setting on each file.
 
Other useful invocations are  
 
Other useful invocations are  
ls -R Recursive. i.e. list contents of sub-directories as well as this directory.
+
ls -R  
ls -A Show "Hidden" Files. ie. Hidden files are file names that start with a dot which in Unix GUI file navigators are (by default) hidden.
+
Recursive. i.e. list contents of sub-directories as well as this directory.
 +
ls -A  
 +
Show "Hidden" Files. ie. Hidden files are file names that start with a dot which in Unix GUI file navigators are (by default) hidden.
  
 
Examples.
 
Examples.
  
$ ls
+
$ ls
 
+
An_Gott_und_meine_Mutter.mid  Domestic        Programming          Test.mid
+
An_Gott_und_meine_Mutter.mid  Domestic        Programming          Test.mid
 
+
An_Gott_und_meine_Mutter.mscz  Engineering    Quantum Physics      Tutoring
+
An_Gott_und_meine_Mutter.mscz  Engineering    Quantum Physics      Tutoring
 
+
appliances                    FluidR3_GM.ins  School              Windaz
+
appliances                    FluidR3_GM.ins  School              Windaz
 
+
...
+
...
 
+
$ ls -l
+
$ ls -l
 
+
total 336
+
total 336
 
+
-rw-rw-r--.  1 brian brian  2429 Apr  2 20:27 An_Gott_und_meine_Mutter.mid
+
-rw-rw-r--.  1 brian brian  2429 Apr  2 20:27 An_Gott_und_meine_Mutter.mid
 
+
-rw-rw-r--.  1 brian brian  4085 Apr  2 19:52 An_Gott_und_meine_Mutter.mscz
+
-rw-rw-r--.  1 brian brian  4085 Apr  2 19:52 An_Gott_und_meine_Mutter.mscz
 
+
drwxrwxr-x.  4 brian brian  4096 Apr  2 20:38 appliances
+
drwxrwxr-x.  4 brian brian  4096 Apr  2 20:38 appliances
 
+
-rw-rw-r--.  1 brian brian  10919 Apr  2 19:52 brotplot.odt
+
-rw-rw-r--.  1 brian brian  10919 Apr  2 19:52 brotplot.odt
 
+
drwxrwxr-x. 7 brian brian  4096 Apr  5 21:26 Consultancy
+
.....
 
 
drwxrwxr-x. 2 brian brian  4096 Apr  2 19:52 Desktop
 
  
 
== Task [http://en.wikipedia.org/wiki/Chown $ chown]  Change Ownership of directories and files ==
 
== Task [http://en.wikipedia.org/wiki/Chown $ chown]  Change Ownership of directories and files ==
  
 
In the Long List above, the files are owned by Brian and are also in the Brian Group.
 
In the Long List above, the files are owned by Brian and are also in the Brian Group.
$ chown fred brotplot.odt will result in this result in ls -l
+
$ chown fred brotplot.odt  
 
+
after executing <tt>ls -l</tt> will result in  
-rw-rw-r--.  1 fred brian  10919 Apr  2 19:52 brotplot.odt
+
-rw-rw-r--.  1 fred brian  10919 Apr  2 19:52 brotplot.odt
  
The file is still in group brian but is owned by fred
+
The file is still in group <tt>brian</tt> but is owned by <tt>fred</tt>
  
$chown foo:foo brotplot.odt will result in
+
$chown foo:foo brotplot.odt  
 +
will result in
  
-rw-rw-r--.  1 foo foo  10919 Apr  2 19:52 brotplot.odt
+
-rw-rw-r--.  1 foo foo  10919 Apr  2 19:52 brotplot.odt
  
The group has change also. See Also [http://en.wikipedia.org/wiki/Chgrp chgrp]
+
The group has changed also. See Also <tt>[http://en.wikipedia.org/wiki/Chgrp chgrp]</tt>
  
 
== Task [http://en.wikipedia.org/wiki/Chmod $ chmod]  Change Access to directories and files ==
 
== Task [http://en.wikipedia.org/wiki/Chmod $ chmod]  Change Access to directories and files ==
When long list files, the left hand column takes the form xuuugggwww.
+
When long list files, the left hand column takes the form <tt>xuuugggwww</tt>.
x is a letter describing what kind of thing the list entry is and can be various things but most commonly is d, l or -.
+
<tt>x</tt> is a letter describing what kind of thing the list entry is and can be various things but most commonly is <tt>d</tt>, <tt>l</tt> or <tt>-</tt>.
d stands for directory, l for link - for a file.
+
<tt>d</tt> stands for directory, <tt>l</tt> for link <tt>-</tt> for a file.
  
The uuugggwww describe access rights for the owner (user), group and the world. (i.e. everyone not in the set owner or group).
+
The <tt>uuugggwww</tt> describe access rights for the owner (user), group and the world. (i.e. everyone not in the set owner or group).
In each triplet, the order is rwx, read for ability to read, w for ability to write and x for ability to execute.
+
In each triplet, the order is <tt>rwx</tt>, <tt>r</tt> for ability to read, <tt>w</tt> for ability to write and <tt>x</tt> for ability to execute. If the <tt>x</tt>flag is not set then the file will not run as a program.
  
 
This line from  above tells us that fred and the group called students both have read and write access where everyone else has read access only.
 
This line from  above tells us that fred and the group called students both have read and write access where everyone else has read access only.
  
-rw-rw-r--.  1 fred students  10919 Apr  2 19:52 brotplot.odt
+
-rw-rw-r--.  1 fred students  10919 Apr  2 19:52 brotplot.odt
  
There are various ways of chmod use but probably the easiest is to consider the groups u for user-owner, g for group, o for other(everyone else) and a for all three.
+
There are various ways of <tt>chmod</tt> use but probably the easiest is to consider the groups <tt>u</tt> for user-owner, <tt>g</tt> for group, <tt>o</tt> for other(everyone else) and a for all three.
  
 
Use chmod as follows
 
Use chmod as follows
 +
chmod o+w brotplot.odt
 +
means add write access for the other group to brotplot.odt
 +
chmod a-r brotplot.odt
 +
means remove read access for the all three (user, group and other) from brotplot.odt
 +
chmod u+x brotplot.odt
 +
means add execute access for the user/owner to brotplot.odt
  
chmod o+w brotplot.odt means add write access for the other group to brotplot.odt<br>
+
== Task [http://en.wikipedia.org/wiki/Sudo $ sudo]. Invoking commands as an admin user ==
chmod a-r brotplot.odt means remove read access for the all three (user, group and other) from brotplot.odt<br>
+
This command introduces the SuperUser or root user. Many commands are only permissible to the root user and by invoking sudo before a command, you are saying "do the following command as the root user". When you do this, you will be asked for your password and if you have system permissions (commonly called being in the admin group) to be a root user then the command will be run. If you do not then a message will tell you so.
chmod u+x brotplot.odt means add execute access for the user/owner to brotplot.odt<br>
+
 
 +
<tt>root</tt> is the name for the main administrator of a Unix like system. The default Debian distribution has no <tt>root</tt> passwd set and you do privileged commands by prepending the command with <tt>sudo</tt>.
 +
 
 +
Example
 +
adduser brian
 +
adduser: Only root may add a user or group to the system.
 +
 +
sudo adduser brian
 +
# You now enter a sript that sets up a new user called brian
  
== Task [http://en.wikipedia.org/wiki/Sudo $ sudo]. Invoking commands as an admin user ==
+
Anytime you get an "<tt>Only root can ....</tt>" message, try the <tt>sudo</tt> command.
This command introduces the SuperUser or root user. Many commands are only permissible to the root user and by invoking sudo before a command, you are saying "do the following command as the root user". When you do this, you will be asked for your password and if you have system permissions (commonly called beinmg in the admin group) to be a root user then the command will be run. If you do not then a message will tell you so.
 
  
There is another way of doing things as root user and that is by using  
+
There is another way of doing things as <tt>root</tt> user and that is by using
  
 
== Task [http://en.wikipedia.org/wiki/Su_%28Unix%29 $ su -] Becoming the admin user ==
 
== Task [http://en.wikipedia.org/wiki/Su_%28Unix%29 $ su -] Becoming the admin user ==
which stands for "substitute user" (or SuperUser) & as invoked here means "become the root user". From this point on, all command run as the super user. Invoking this command will cause you to be asked for the root password. Some systems do not allow this to happoen in a default install in which case you have to enable the root user.
+
which stands for "substitute user" (or "switch user") and as invoked here means "become the <tt>root</tt> user". From this point on, all commands run as the super user. Invoking this command will cause you to be asked for the <tt>root</tt> password. Some systems do not allow this to happen in a default install in which case you have to enable the root user.
 +
To enable the root user issue
 +
passwd root
 +
(You will have to enter a new passwd). You will be asked twice for the password and assuming you can manage to enter the same password twice, you now have a root user and you can issue
 +
 
 +
su -
 +
 
 +
 
 
When you enter passwords in Unix like systems, you will not see asterisks or anything at the prompt but the password is being entered nevertheless.
 
When you enter passwords in Unix like systems, you will not see asterisks or anything at the prompt but the password is being entered nevertheless.
If you habitually do everything as the root user, eventually you will do something both educational and disasterous. For example, you could invoke  
+
If you habitually do everything as the <tt>root</tt> user, you shouldn't. Eventually you will do something both educational and disastrous. For example, you could invoke  
  
"rm -r -f /* " and delete all the files in the whole system.  
+
rm -r -f /*
 +
and delete all the files in the whole system.  
  
$ rm -r -f ./* would just delete the files in the current directory. You do not usually get warnings as root user. (Other than this one).
+
$ rm -r -f ./*  
 +
would just delete the files in the current directory. You do not usually get warnings as <tt>root</tt> user. (Other than this one).
  
 
== Task [http://en.wikipedia.org/wiki/Touch_%28Unix%29 $ touch]. Create a new empty file ==
 
== Task [http://en.wikipedia.org/wiki/Touch_%28Unix%29 $ touch]. Create a new empty file ==
 
Go to where you want the file to be and invoke
 
Go to where you want the file to be and invoke
  
$ touch <filename>
+
$ touch <filename>
  
 
== Task [http://en.wikipedia.org/wiki/Mkdir $ mkdir]. Create a new empty directory ==
 
== Task [http://en.wikipedia.org/wiki/Mkdir $ mkdir]. Create a new empty directory ==
 
Go to where you want the directory to be and invoke
 
Go to where you want the directory to be and invoke
  
$ mkdir <Directory Name>
+
$ mkdir <Directory Name>
  
 
== Task [http://en.wikipedia.org/wiki/Rm_%28Unix%29 $ rm]. Remove a file ==
 
== Task [http://en.wikipedia.org/wiki/Rm_%28Unix%29 $ rm]. Remove a file ==
 
Go to where you want the directory to be and invoke
 
Go to where you want the directory to be and invoke
  
$ rm filename
+
$ rm filename
 
 
== Task [http://en.wikipedia.org/wiki/Rm_%28Unix%29 $ rm]. Remove a Directory ==
 
Go to where you want the directory to be removed
 
  
$ rm -r -f <Directory Name>
+
== Task [http://en.wikipedia.org/wiki/Rm_%28Unix%29 $ rmdir]. Remove a Directory ==
 +
Go to where you want the directory to be removed.  The directory will already need to be empty of all contents.
 +
rmdir
  
This will remove the directory, all its contents and also any sub directories.
+
If the directory is not yet empty
 +
$ rm -r -f <Directory Name>
  
 +
This will remove the directory, all its contents and also any sub directories and their contents.
  
 
== Task [http://en.wikipedia.org/wiki/Mount_%28Unix%29 $ mount]  Connect to a device or Filing System ==
 
== Task [http://en.wikipedia.org/wiki/Mount_%28Unix%29 $ mount]  Connect to a device or Filing System ==
  
Mount is the way in which you connect a Unix system to external devices. There is no "C" drive as in Windows. What happens in Linux (& FreeBSD/OSx) is that a device is mounted in the filing system somewhere and when you navigate to that place. The items offered by the device will appear at that point.
+
<tt>mount</tt> is the way in which you connect a Unix system to external devices. There is no "C" drive as in Windows. What happens in Linux (and FreeBSD/OS X) is that a device is mounted in the filing system somewhere and when you navigate to that place. The items offered by the device will appear at that point.
 
   
 
   
 
This is a complex command. The switches, parameters and target of the Mount command will vary according to the protocol of the system being mounted. Some things will automount. This is why, when you plug a SDcard into a modern Linux, the filing system will automatically pick it up.
 
This is a complex command. The switches, parameters and target of the Mount command will vary according to the protocol of the system being mounted. Some things will automount. This is why, when you plug a SDcard into a modern Linux, the filing system will automatically pick it up.
  
Manual Mounting requires a "Mount Point". That means a directory which will be filled with the mounted device when it is mounted. Often, this is in the directory /mnt/ somewhere.
+
Manual Mounting requires a "Mount Point". That means a directory which will be filled with the mounted device when it is mounted. Often, this is in the directory <tt>/mnt/</tt> somewhere.
  
 
Generally, first ensure there is a mount point, if not then create a directory at the point needed.
 
Generally, first ensure there is a mount point, if not then create a directory at the point needed.
e.g. $ mkdir /mnt/netfolder
+
e.g.  
 +
$ mkdir /mnt/netfolder
  
The issue Mount with the necessary switches, parameters and directories.
+
Then issue <tt>mount</tt> with the necessary switches, parameters and directories.
  
 
e.g. From above.
 
e.g. From above.
  
mount -F smbfs //workgroup;fred:foo@192.168.1.99/homes /mnt/net
+
mount -F smbfs //workgroup;fred:foo@192.168.1.99/homes /mnt/net
 +
 
 +
 
 +
== Some useful commands ==
 +
 
 +
startx
 +
If you are a bit nervous about the command line, this might be
 +
your favourite. It gets you escape to a nice graphical interface.
 +
free 
 +
Show how much memory is available.
 +
df / 
 +
Show how much disk space is used.
 +
hostname -I
 +
Show your IP address. Try this command with your network cable
 +
connected and disconnected to see the difference.
 +
lsusb
 +
Show what is plugged into the USB port. Try this command with
 +
your mouse connected and disconnected to see the difference.
 +
 
 +
=References=
 +
<references/>
 +
 
 +
{{Template:Raspberry Pi}}
 +
[[Category:RaspberryPi]]

Latest revision as of 14:38, 22 August 2012

This Page is a Quickstart for Command Line Interface on the Raspberry PI.--Brian 09:13, 20 April 2012 (UTC) Back to Beginners Page

Why Do I Need the CLI?

(CLI-Command Line Interface)

The Raspberry Pi operating systems that you can download for your SD card are all versions of Linux. A graphical user interface (GUI) for Linux is provided by the X Windowing System, together with a window manager. There are lots of different window managers, for example the one being used at time of writing with the Debian "squeeze" OS is LXDE. For some OS distributions, the windowing system is started up automatically when the machine starts up, but if not then you are in the Command Line Interface, which allows you to type commands and get information back. Most of us will immediately type "startx" to bring up the GUI, but even when you are using the GUI you can still get a CLI in the form of a terminal window. You can bring this up from the start menu; usually it is called something like X Terminal.

You need the CLI because there are many things that either can't be be done using GUIs, or are much easier using the CLI. In addition there are situations when you would rather not run the X Windowing system, because it takes up a lot of processor power and memory. Also a Raspberry Pi doing some task or other may well have no screen at all, and you may manage it remotely using a remote log in to a terminal.

Windows and Mac users are generally guided away from the CLI hence this Quick start Guide. The commands here have mostly been described and tested using debian linux which at the time of writing (April 2012) was the version of linux that was most active in the RPi world. It is not meant to advocate this distribution in particular. The author (Brian 09:27, 30 May 2012 (UTC)) is actually a Fedora user most of the time.

The quick answer to "why do I need the CLI?" then, is that the CLI is going to be a very useful tool for Raspberry Pi users.

Commands are just programs

When you type into the terminal you are running programs. Most of the commands listed here run programs that give you the ability to command the system to do something. When you add programs to your pi, you will be able to run more commands. This means that if a command you run and expect to work returns the message $ bash: foo: command not found...

It just means that you have not installed "foo" yet. Most of the commands described in this page are the kind of house keeping commands that come with bash. Each program has an original author who is acknowledged at the bottom of the man page.

To use the CLI you need to know commands and the default command set is contained in the "shell" you are using. There are Lots of shells out there and the one on this Raspberry Pi is Bash Wikipedia Bash Entry.

To find the syntax for a command do an Internet search for the command or find a Bash Quick Reference Card. In this wiki entry, each command has a link to the Wikipedia entry for that command.

If you want to know how to use commands without using the Internet then use man (short for manual).

$ man <command> 

or

$ info <command>

from the CLI to be sent to a page of guidance.

The guidance given by "man" is sometimes a bit formal to the point that it could be said, "if you think you know a command then go to the command's man page in order to find out that you don't really".

General Syntax of Commands

Commands take the form

<Command> | -<Switches> | <Parameters> | <Target>

<> are often used in Syntax guides to indicate that the <> surround a place for a command rather than an actual command. | is used to denote OR or Optional so in the line above we read that a Command can be used on its own but may be followed by Switches (single letters, preceded by a hyphen that adjust what the command does), Parameters (Things that the command needs to know in order to work) and a Target (The thing that the command will be applied to).

e.g. "ls -l /home/brian" means List in Long Format (the -l switch) the directory (target) /home/brian

mount -F smbfs //workgroup;fred:foo@192.168.1.99/homes /mnt/net

Means use the username fred and password foo (parameters) to make the shared drive called homes on the Windows server at 192.168.1.99 (parameter) appear in the directory tree at the point /mnt/net (target) using the Server Message Block Filing System (the -F switch). (BTW, this assumes that you have the samba client software installed on your pi)

Task $ cd. Navigating the file system using "cd"

The file system in Linux is Hierarchical with nested directories. (Often called Folders) in a "Tree". The top of the directory structure is "/" and Directories underneath / are referred to using "Paths" just like urls in a web browser. To go to a particular place in the directory structure you use cd. e.g.

cd /

will take you to the top of the Directory Tree

To go to a particular place use cd followed by the location in the tree.

cd /home/brian/Documents 

will take you to "brian's" home directory Documents Folder. File & Directory Names are Case Sensitive in Linux

To go one Directory "Up" the tree then do

cd ..

Task $ ls. Listing the files and Folders in a particular place

$ ls 

list all the file in the current directory.

$ ls -l 

list all the file in the current directory and display the long version of the information about each file or directory. The output may be colour coded depending on the terminal preferences that you have set. ls -l tells you (amongst other stuff) the size, owner and security setting on each file. Other useful invocations are

ls -R 

Recursive. i.e. list contents of sub-directories as well as this directory.

ls -A 

Show "Hidden" Files. ie. Hidden files are file names that start with a dot which in Unix GUI file navigators are (by default) hidden.

Examples.

$ ls

An_Gott_und_meine_Mutter.mid   Domestic        Programming          Test.mid

An_Gott_und_meine_Mutter.mscz  Engineering     Quantum Physics      Tutoring

appliances                     FluidR3_GM.ins  School               Windaz

...

$ ls -l

total 336

-rw-rw-r--.  1 brian brian   2429 Apr  2 20:27 An_Gott_und_meine_Mutter.mid

-rw-rw-r--.  1 brian brian   4085 Apr  2 19:52 An_Gott_und_meine_Mutter.mscz

drwxrwxr-x.  4 brian brian   4096 Apr  2 20:38 appliances

-rw-rw-r--.  1 brian brian  10919 Apr  2 19:52 brotplot.odt

.....

Task $ chown Change Ownership of directories and files

In the Long List above, the files are owned by Brian and are also in the Brian Group.

$ chown fred brotplot.odt 

after executing ls -l will result in

-rw-rw-r--.  1 fred brian  10919 Apr  2 19:52 brotplot.odt

The file is still in group brian but is owned by fred

$chown foo:foo brotplot.odt 

will result in

-rw-rw-r--.  1 foo foo  10919 Apr  2 19:52 brotplot.odt

The group has changed also. See Also chgrp

Task $ chmod Change Access to directories and files

When long list files, the left hand column takes the form xuuugggwww. x is a letter describing what kind of thing the list entry is and can be various things but most commonly is d, l or -. d stands for directory, l for link - for a file.

The uuugggwww describe access rights for the owner (user), group and the world. (i.e. everyone not in the set owner or group). In each triplet, the order is rwx, r for ability to read, w for ability to write and x for ability to execute. If the xflag is not set then the file will not run as a program.

This line from above tells us that fred and the group called students both have read and write access where everyone else has read access only.

-rw-rw-r--.  1 fred students  10919 Apr  2 19:52 brotplot.odt

There are various ways of chmod use but probably the easiest is to consider the groups u for user-owner, g for group, o for other(everyone else) and a for all three.

Use chmod as follows

chmod o+w brotplot.odt 

means add write access for the other group to brotplot.odt

chmod a-r brotplot.odt 

means remove read access for the all three (user, group and other) from brotplot.odt

chmod u+x brotplot.odt 

means add execute access for the user/owner to brotplot.odt

Task $ sudo. Invoking commands as an admin user

This command introduces the SuperUser or root user. Many commands are only permissible to the root user and by invoking sudo before a command, you are saying "do the following command as the root user". When you do this, you will be asked for your password and if you have system permissions (commonly called being in the admin group) to be a root user then the command will be run. If you do not then a message will tell you so.

root is the name for the main administrator of a Unix like system. The default Debian distribution has no root passwd set and you do privileged commands by prepending the command with sudo.

Example

adduser brian
adduser: Only root may add a user or group to the system.

sudo adduser brian 
# You now enter a sript that sets up a new user called brian

Anytime you get an "Only root can ...." message, try the sudo command.

There is another way of doing things as root user and that is by using

Task $ su - Becoming the admin user

which stands for "substitute user" (or "switch user") and as invoked here means "become the root user". From this point on, all commands run as the super user. Invoking this command will cause you to be asked for the root password. Some systems do not allow this to happen in a default install in which case you have to enable the root user. To enable the root user issue

passwd root

(You will have to enter a new passwd). You will be asked twice for the password and assuming you can manage to enter the same password twice, you now have a root user and you can issue

su -


When you enter passwords in Unix like systems, you will not see asterisks or anything at the prompt but the password is being entered nevertheless. If you habitually do everything as the root user, you shouldn't. Eventually you will do something both educational and disastrous. For example, you could invoke

rm -r -f /*  

and delete all the files in the whole system.

$ rm -r -f ./* 

would just delete the files in the current directory. You do not usually get warnings as root user. (Other than this one).

Task $ touch. Create a new empty file

Go to where you want the file to be and invoke

$ touch <filename>

Task $ mkdir. Create a new empty directory

Go to where you want the directory to be and invoke

$ mkdir <Directory Name>

Task $ rm. Remove a file

Go to where you want the directory to be and invoke

$ rm filename

Task $ rmdir. Remove a Directory

Go to where you want the directory to be removed. The directory will already need to be empty of all contents.

rmdir

If the directory is not yet empty

$ rm -r -f <Directory Name>

This will remove the directory, all its contents and also any sub directories and their contents.

Task $ mount Connect to a device or Filing System

mount is the way in which you connect a Unix system to external devices. There is no "C" drive as in Windows. What happens in Linux (and FreeBSD/OS X) is that a device is mounted in the filing system somewhere and when you navigate to that place. The items offered by the device will appear at that point.

This is a complex command. The switches, parameters and target of the Mount command will vary according to the protocol of the system being mounted. Some things will automount. This is why, when you plug a SDcard into a modern Linux, the filing system will automatically pick it up.

Manual Mounting requires a "Mount Point". That means a directory which will be filled with the mounted device when it is mounted. Often, this is in the directory /mnt/ somewhere.

Generally, first ensure there is a mount point, if not then create a directory at the point needed. e.g.

$ mkdir /mnt/netfolder

Then issue mount with the necessary switches, parameters and directories.

e.g. From above.

mount -F smbfs //workgroup;fred:foo@192.168.1.99/homes /mnt/net


Some useful commands

startx 

If you are a bit nervous about the command line, this might be your favourite. It gets you escape to a nice graphical interface.

free  

Show how much memory is available.

df /  

Show how much disk space is used.

hostname -I

Show your IP address. Try this command with your network cable connected and disconnected to see the difference.

lsusb 

Show what is plugged into the USB port. Try this command with your mouse connected and disconnected to see the difference.

References