submit to reddit       

Bash basics

Linux is based on Unix, which was developed long before graphical user interfaces were common. It's a very text oriented operating system, and learning how to use the command line is extremely useful. Almost every aspect of the Linux operating system can be accessed from the command line.

The program that processes the commands you type in a terminal is called bash. It's called bash because it's derived from a program called the Bourne shell. The name bash is an abbreviation of 'Bourne Again SHell'.

If your Pi is configured to boot to the desktop, you can open a terminal window by double clicking on the LXTerminal icon on the desktop. Otherwise, when your Pi boots up, you just need to login and you'll be presented with a bash prompt.

The prompt is made up of your user name followed by an '@' symbol and the host name of your Pi. The next part of the prompt shows the current working directory, which is your home directory by default. Note that the Linux shorthand for your home directory is simply the '~' character. Finally, there's a '$' at the end of the prompt. If you log in as root, you'll see a '#' instead.

Most commands are separate programs or scripts. The same basic set of commands is available on most Unix/Linux systems, although there can be variations in the syntax, and some programs might behave slightly differently on different systems.

Essesntial commands

Start the desktop

If your Pi is set up to just log into the command line, then you can start the desktop by typing 'startx' at the command prompt. If your Pi is already set up to boot the desktop, then you don't need to use this command.

List the contents of a directory

The ls command is used to view the contents of a directory. You can see the contents of the current directory by typing 'ls' at the command prompt. Adding the '-l' option makes ls show a detailed view of the results including the permissions of each file/directory, its owner and the time it was last modified:

ls -l

The ls command can list the contents of other directories as well as the current working directory. This command gives a detailed listing of the contents of /etc/init.d/:

ls -l /etc/init.d

Note that files that start with a '.' are hidden files, so you need to use the 'a' option if you want hidden files to be shown in the output:

ls -a

Change the current working directory

If you want to work on files in a particular directory, you can use the cd command to change the working directory:

cd /etc

This command will set the current working directory to your home directory:

cd ~

Editing files on the command line

Nano is a text editor that runs in a command prompt, so it's useful in situations where the desktop isn't running. To open a file in nano, type this command:

nano somefile.txt

When you've finished editing the file, press Control O, and you'll see a prompt at the bottom of the screen asking you what file name to write. Just press the return key to save the file under its current name. You can also edit the file name and press return, in which case you'll be prompted to save the file under a different name. Type Y to confirm or Control C to cancel.

If you use the -c option when you start nano, a status bar will be shown at the bottom of the screen with the line number that the cursor is on.

Close nano by typing Control X.

Editing files in the desktop environment

If you've booted into the desktop, you can open the leafpad editor using this command:

leafpad somefile.txt

Leafpad is a lot like Notepad on Windows, and it supports common keyboard shortcuts like Control C and Control V to copy and paste.

Executing commands as another user with sudo

Some commands require administrator privileges. The sudo command allows other commands to be executed as a different user. By default, the sudo command grants the privileges of the root user. For example if I want to edit /etc/network/interfaces, I would need administrator privileges because this file is owned by root. This command will start nano with root privileges, so if I make changes to the file, I'll be able to save them:

sudo nano /etc/network/interfaces

The sudo command can be used to obtain the privileges of other users too. I've installed the Apache web server, and the user www-data is automatically created during this process. If I want to edit a file using www-data's permissions, I can use the -u option:

sudo -u www-data nano /var/www/somefile.txt

Install software and manage updates

Raspbian uses the apt-get utility to manage the software installed on your Pi. The apt-get command requires root privileges, so you need to use it with sudo. Apt-get downloads software packages from the Raspbian repository, so your Pi must be connected to the internet for apt-get to work. The first time you run your Pi (or any Linux system), you should install any updates that are available. It's important to keep Raspbian up to date as patches can include security enhancements, bug fixes and performance improvements.

These two commands should be used together:

sudo apt-get update sudo apt-get upgrade

The first one downloads a list of all the latest packages, but doesn't actually install them. The second command checks to see which packages are out of date, fetches available updates, and installs them. This can take a few minutes when you update your Pi for the first time.

You can use apt-get to install new programms. Suppose I want to install a program called tree which graphically displays directory trees in a command prompt. I can install it with this command:

sudo apt-get install tree

I can remove tree with this command:

sudo apt-get remove tree

Some programs have configuration files. The command above will remove the program, but leaves configuration files behind. If you want to get rid of the configuration files, add the --purge option to the command.

Shutdown and reboot

It's best to shutdown any Linux device in a controlled manner, otherwise its file-system could get corrupted. To shutdown your Raspberry Pi, type this command:

sudo halt

You need to use sudo because the halt command can only be run by the root user. In order to reboot the Pi, use this command:

sudo reboot

Keyboard shortcuts

Bash stores a history of all the commands that you type in a file in your home directory named .bash_history. At a command prompt you can use the up and down arrow keys to navigate through the command history.

The tab key for can be used for command and file name completion. For example, if there's a file in my home directory called somefile.txt, I would type this command:

nano some

Instead of hitting the return key, pressing the tab key will make bash check to see if there's a file starting with 'some'. It will find somefile.txt and automatically complete the command:

nano somefile.txt

Now if I hit the return key, nano will open the file.

Getting Help

The whatis command gives a brief description of what a command does:

whatis ls

You can find the location of a program with the whereis command:

whereis ls

The man command shows the manual page for a command:

man ls

Type 'q' to exit from man. You can also view the Debian man pages online. Note that man pages are divided into 8 sections. General purpose Linux commands are in section 1.

Be careful with bash commands - there's no undo button, so back up files before you edit them, and check commands before you press the return key.

See also: explainshell.com/


More from this category:



comments powered by Disqus

Follow me