CLI Spells for the Raspberry Pi
- 1 Why Do I Need the CLI?
- 2 General Syntax of Commands
- 3 Task cd. Navigating the file system using "cd"
- 4 Task ls. Listing the files and Folders in a particular place
- 5 Task Chown Change Ownership of directories and files
- 6 Task $ sudo. Invoking commands as an admin user
- 7 Task $ su - Becoming the admin user
- 8 Task $ touch. Create a new empty file
- 9 Task $ mkdir. Create a new empty directory
- 10 Task $ rm. Remove a file
- 11 Task $ rm. Remove a Directory
- 12 Task Mount
Why Do I Need the CLI?
(CLI-Command Line Interpreter)
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 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.
Windows and Mac users are generally guided away from the CLI hence this Quick start Guide.
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.
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. Here, 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 <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 a 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:email@example.com/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)
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 (amonst 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.
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
-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
drwxrwxr-x. 7 brian brian 4096 Apr 5 21:26 Consultancy
drwxrwxr-x. 2 brian brian 4096 Apr 2 19:52 Desktop
Task Chown Change Ownership of directories and files
In the Long List above, the files are owned by Brian and are alos in the Brian Group. $ chown fred brotplot.odt will result in this result in ls -l
-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 change also. See Also chgrp
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 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
Task $ 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. 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
"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 $ rm. Remove a Directory
Go to where you want the directory to be removed
$ rm -r -f <Directory Name>
This will remove the directory, all its contents and alos any sub directories.
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 offerred 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.