Just the Basics
Following on from Don’t Fear The Command Line: Raspbian Linux Shell Commands and Tools – Part 1, in which tools for monitoring the Raspberry Pi’s hardware and running programmes are introduced, along with the Package installer (APT), this post concentrates solely on the file system.
For those of us of a certain vintage, the command line may seem a reasonably natural and intuitive interface with the computer; for many, however, it is arcane and daunting. Even for those well versed in the DOS command line, the Linux shell is sufficiently alien to cause headaches.
The aim here isn’t to educate Linux gurus, rather to provide some guidance to those either new to command line interfaces in general, or to the Linux shell (bash, in the case of Raspbian) in particular.
Navigating the file system from a command line can be especially troublesome when compared to the intuitive visual representations provided by graphical user interfaces, hence this brief guide.
Topics Covered in this Guide
- Moving Up, Down and Around the Directory Hierarchy
- Displaying the Directory Contents: the ‘ls’ Command
- Using the ‘more’ Command to Handle Overflowing Output
- Make use of Autocomplete to Speed Navigation
- Where am I? The Print Working Directory (‘pwd’) command
- Visualise the Directory Hierarchy
Please note: The Raspbian Linux Command Shell is case sensitive. Commands need to be typed exactly as shown, as do directory (folder) and file names.
Moving Up, Down and Around the Directory Hierarchy
Moving around the file system hierarchy to reach a specific directory is achieved through the cd command.
cd (short for ‘change directory’) command takes a path as a parameter, which instructs the system in how to reach a different directory. This can be achieved in several different ways.
First, we’ll introduce the building blocks that can be used to construct a path (a route from one location in the file system to another):
/. represents the directory you are currently in.
This is useful when executing a script file, which requires a path to that file to be provided (typing the script file name alone will not work).
../ indicates the directory one level above the one you are currently in.
This is useful both for navigating and for providing partially qualified paths (see below).
~ represents the home directory.
– If you are logged in as a standard user, this will be the user’s own directory. For example, for the user pi, home is
– If you are logged in as root, home is
Now we can use the
cd command with various paths:
To move down into a directory which is at the level you are currently within, type
cd followed by the directory name:
To move up one level from the current directory, type
cd and two full stops:
To jump directly to your user’s folder, use the tilde character (‘~’):
To jump directly to the root of the file system (the highest level), use the forward slash character (‘/’):
To jump directly to a known location, you can provide a fully or partially qualified path:
– A fully qualified path starts at the root of the file system, and includes all directories and sub-directories in the hierarchy required to reach a destination:
– A partially qualified path is one relative to the current location, making use of the
.. command. For example, to move up a couple of directories from the current location, and then down into a directory that exists at the location those two levels up:
Displaying the Directory Contents: the ‘ls’ Command
ls (short for ‘list’) command to list the contents of the current directory. Simply type:
By default, directories and files marked as ‘hidden’ will not be shown. These items have names prefixed with a full-stop. By convention this includes most files used for system configuration.
To include hidden files in the listing, add the option, or switch, ‘-h’
Using the ‘more’ Command to Handle Overflowing Output
Often when issuing commands the resultant output will fill more than the available screen space; although there is a buffer, allowing the user to scroll backwards through the output, this can also be overrun.
To alleviate this issue, the
more command can be appended to most other commands. The
more command causes the output to halt after the screen has been filled; pressing any key will scroll the screen to show the next line, until no more data is available.
To use the
more command, we pipe the output of a command such as
ls to it, using a special operator, which is the pipe character ‘|’. On a UK keyboard this is generally found on the key to the left of Z (holding shift at the same time):
ls | more
Where there is too much data to display, the system will display the following prompt at the bottom of the screen:
If you wish to avoid wading through pages and pages of output, press
control + 'c' together to return to the command prompt.
Make use of Autocomplete to Speed Navigation
The Tab key can be used to autocomplete command, directory, and file names. Simply begin typing, then hit the Tab key.
– If there is only a single item which matches the partially typed entry, the full name of the item will be completed for you.
– If more than one item matches, the system will display all of the matches. You may need to press Tab several times. In this case, typing more characters to narrow down the selection to one item, then pressing Tab again is probably the fastest way to make use of the autocompletion.
Where am I? The Print Working Directory (‘pwd’) command
It’s easy to become lost when navigating deep hierarchies of nested directories. To determine your current location, used the
pwd (Print Working Directory) command, simply type:
Why is this required? As an example, when first logging in, the prompt displays:
pi@retropie ~ $
Although the tilde indicates the current location is user Pi’s home directory, this is clearer when using
Visualise the Directory Hierarchy
tree command helps navigation by visualising the directory structure. Unfortunately it is not installed as standard on Raspbian Linux. Fortunately using the Rasbpian Package Management tool, it can easily be installed:
sudo apt-get update
sudo apt-get install tree
Once installed, simply type:
pi@retropie /opt/piprime $ tree
1 directory, 5 files
Should the output scroll off of the screen, make use of the
more, as mentioned previously:
tree | more
tree command supports a wide range of options. Some of the most useful are:
-a list all files and directories, including hidden
-d list only directories
-L n display to a maximum depth of n levels
-C display output with colourised formatting (this becomes the default once used)
E.g. to display the tree to a maximum of three levels deep, with hidden items included, type:
tree -a -L 3
For more detail on the
tree command and its options, see the comprehensive overview on cyberciti.biz here
A Labour of Love
- Links: Raspberry Pi and Gaming Emulation via RetroPie
- Don’t Fear The Command Line: Raspbian Linux Shell Commands and Tools – Part 1: Just the Basics
Overclocking and Stability Testing the Raspberry Pi
- Part 1: Overclocking
- Part 2: Stress testing the CPU with mprime
- Part 3: Stress testing the RAM with Memtester
- Part 4: Stress testing the SD card storage with the Stability Test Script