Navigating the Raspberry Pi’s File System. Raspbian Linux Shell Commands and Tools – Part 2

Just the Basics

Raspbian Linux GUI File Browser
VS transparent
Raspbian Command Line - Shutdown - help screen

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

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.

The 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 /home/pi
– If you are logged in as root, home is /root

image
Raspbian Command Shell - navigation using 'cd' command

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:
cd mydirectory

To move up one level from the current directory, type cd and two full stops:
cd ..

To jump directly to your user’s folder, use the tilde character (‘~’):
cd ~

To jump directly to the root of the file system (the highest level), use the forward slash character (‘/’):
cd /

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:
cd /home/pi/mydirectory

– 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:
cd ../../mysubdirectory

Displaying the Directory Contents: the ‘ls’ Command

Use the ls (short for ‘list’) command to list the contents of the current directory. Simply type:
ls

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’
ls -h

image
Raspbian Command Shell - ls (list) command

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:
--More--

If you wish to avoid wading through pages and pages of output, press control + 'c' together to return to the command prompt.

Raspbian Command Line - Output of ls Piped Via more
Raspbian Command Line – Output of ls Piped Via more

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:
pwd

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 pwd:
/home/pi

Visualise the Directory Hierarchy

The 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:
tree

For example:

pi@retropie /opt/piprime $ tree
.
├── mprime.py
├── prime.py
├── README.md
├── sprime2.py
└── wilson-prime-finder
    └── LICENSE

1 directory, 5 files

Should the output scroll off of the screen, make use of the more, as mentioned previously:

tree | more

Raspbian Command Line - Output of tree Piped Via more
Raspbian Command Line – Output of tree Piped Via more

The 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

Retro Resolution is entirely a labour of love. Please consider offering a donation if the information here has helped illuminate, enlighten, or otherwise assisted you!
Donate Using PayPal

Related Posts

Overclocking and Stability Testing the Raspberry Pi

About
Disclaimers
Privacy Policy
Terms and Conditions
© Retro Resolution

6 thoughts on “Navigating the Raspberry Pi’s File System. Raspbian Linux Shell Commands and Tools – Part 2

  1. Hi! thank you for this page but i need a little more information please.

    How do I navigate to a USB SD Card and copy a file to my Pi folder?

    Thank you,
    Colin

    Like

    1. Hi,

      I’m glad the article was of use. Regarding accessing a usb-mounted device (flash drive, USB hard disk etc), the Linux operating system treats everything as a directory or file. On the Raspberry Pi, USB drives appear within the folder:

      /media/pi/device-name

      For example, when I attach a Kingston USB drive, the device name is read as Kingston,and it is made available from the ‘pi’ directory, which itself appears within the ‘media’ directory at the root of the filesystem.

      A device may have a non-obvious name, such a part number. You can use the command ‘lsusb’ before attaching a USB drive then immediately afterwards, to see the name of a device.

      The ‘pi’ directory will be named as something else if the currently logged in user is not named ‘pi’

      Hope this helps.

      Like

      1. That makes a lot of sense now, thank you! You’ve put it in a much clearer way than anything else I’ve read.
        Whether or not I can do it is a different thing.

        Much appreciated,
        Colin

        Like

      2. Yes, it worked thank you!
        It took a while but after I found what I wanted I found another way. Apparently, in the top right corner of the screen the ‘Pause/Play’ icon is the ‘Drive’ selector and that took me straight to my USB.

        However, I’m glad I did it your way first otherwise I’ll never learn.

        This year is the first time I’ve tried programming since I purchased my Sinclair Spectrum in 1982. I loved their BASIC language as well as Machine Code – Peeking & Poking but I find this Linux harder.

        Thanks again,
        Colin

        Like

      3. Hi Colin, I’m glad you got it working.

        The tutorial is aimed at mounting a drive outside of the Raspbian LXDE desktop (X-Windows); the desktop has built-in auto mounting and ejection, which I believe is what you’re describing.

        For users of RetroPie, generally the system is configured to boot directly to EmulationStation, hence the requirement to provide an alternate method to mount a USB drive (to access game roms, record gameplay etc)

        I began coding just slightly later than you it seems, with a ZX81 in 1983 and a Spectrum in 1984 – Linux is certainly orders of magnitude more complex!

        Thanks again for your kind feedback.

        Like

Please Leave a Reply! Anonymous comments and 'markdown' formatting enabled.