Skip to main content
Library homepage
 

Text Color

Text Size

 

Margin Size

 

Font Type

Enable Dyslexic Font
Engineering LibreTexts

Introduction to Linux for the Raspberry Pi-command line

( \newcommand{\kernel}{\mathrm{null}\,}\)

Basics of the Linux command line on the RPi

Raspbian is both a desktop computer with a GUI and potentially a server.

It is a server if VNC and/or ssh are running.

To access the command line ssh into your RPi or if you are in through VNC open a terminal by pressing (at the same time) Ctr-Alt-t, (t is for terminal).

Basics of the Linux command line on the RPi.PNGclipboard_e544b32cce2639a2946135caedd52034b.png

Above is the typical command prompt for a Linux terminal.

The RPi comes with a pre-installed user named ‘pi’.

 

 

Do you want to be the master or the servant?
Make the computer work for you not the other way around.

* Command completion
    using tab
* Command history
    recall a previous command (up arrow)
    show all previous commands (history)


 

The path and permissions graphic is an example of the “ls -l” command (directory long listing).

On the fare right are the file names.  This includes two script, helloWorld.py and helloWorld.sh.  Also in blue is a directory named googleSheets.

On the fare left are the permissions for these files.  The first character is the file type.  The dash ‘-‘ indicates a regular file, the ‘d’ is for directory. An ‘l’ (L) would indicate a symbolic link.

The directory googleSheets has the letter “d” as the first character.

The next sets of characters are in three sets of three.  The first set of three are permissions for the user (the owner of the file).  Next are permissions for the group.  Users can be in a group with file permissions set accordingly.

The last set of three is the “other”.  Files accessible on a webserver would use ‘other’ with file permissions set accordingly.

The letters represent specific file permissions. For example ‘w’ is write permission for that file and ‘r’ is read permission.

The ‘x’ is for executable.  Directories and scripts would have this set appropriately for each user group.

---------------------------------------

The following is a demonstration of file permissions using several Linux commands.

First some background on commands and programs.

The echo command is a program.

Programs take command line parameters.

Some command line parameters are switches.

In most cases switches start with a dash ‘-‘.

Some command line parameters are filenames.

Other command line parameters are strings.

 

 

To see a list of potential command line parameters for the echo command use the man-pages as follows, Note, to exit the man page press q to quit.


clipboard_e6bab19ac646b55481fe011a7c1689d46.png

Path and Permissions continued:

Permissions

clipboard_e1147938c19b9e3ea216ab320c052690a.png

Run the above commands on your RPi and notice the changes in file permissions.

clipboard_e32078ca3d7df7a10d140c2303cdad2d2.png

Why did you get the Permission denied error when you tried to write data to the file using the echo command?

Answer, using the chmod command you write protected the file.

 

clipboard_e8aa7c70a89956aa5d7db7b51c6f969fd.png

Here the ability to write to the file is restored.

Next is a demonstration of our ability to remove read permission for a file.

clipboard_e3842461d705774470c15babd4091fb6a.png

After read permission is restored the cat command can be used to see the content of the file.

Now the move command (mv) is used to rename the file.

After renaming the file the permissions are set to executable.

clipboard_e1d9aafda545f7723324bfcb3bc740f0e.png

Now the file containing the data “echo string” is a program.

Running the program produces results.

clipboard_eeb347d54fe1b6d67160bc2101008c248.png

In this example executable permission is remove and therefore it produces and error when the program is run.

Note however that the script can be run by providing the interpreter on the command line.

In the example above “bash” is a program.  Programs take command line parameters.  Here the parameter is a text file containing commands. Bash “interpreter” the text and runs the commands in the file.

clipboard_e478657a5ce4bbf4a4feb02e62807c959.png

Notice in the above set of commands that when the directory has data it cannot be removed.

The “rm” command is used to remove the populated directory with the force recursive switches “-fr”.

clipboard_e773d23ba76565a8916ca57e973ada722.png

Describing path as a tree structure helps to illustrate the idea of up and down for a directory structure.

Try the commands in the following demonstration and understand what is happening.

 

clipboard_ec2e14c50e47a19bbebccbd74a1ece8fe.png

In this example the “cd” command is used to move from one directory to another.

The user is changing the default location to a different location in the directory tree structure.

The next example starts by finding out where you are using the “pwd” command then moving to the Downloads directory.

clipboard_ee18a73f31bf1f37cd472c3105eb78db1.png

The difference between a full path and a relative path is described here;

A full path always starts with a root slash “/”, examples;

  • /
  • /home
  • /opt

In contrast the relative path always starts with a dot “.”, examples;

  • ./Downloads
  • ../../home
  • ./

clipboard_e65e4028085c9ba4ad11113715a2dbce1.png

Path, where are my programs?

clipboard_e75db8eef9a64463b2a81301fa048e4c8.png

For example, to run this program not in the search path would look like this;

Full path;

/home/pi/Downloads/autodocksuiteBuild.sh

Relative path;

./Downloads/autodocksuiteBuild.sh

If the program is in the search path the output would be the full path to the program as shown in the examples above for grep, echo and touch.

Knowing the full path to an application is needed for some situations like running from a cron job.

Preset Variables

The user can set variables in bash.  Here are some preset variables.

clipboard_e64719506242ae8f78a70a708e675b4e5.png

Finding files by type

clipboard_e97287c6b988eeda3dc55c51d21b3f3fc.png

In the above example “find” is used to locate files of a specific type (starting with a dot ‘.’) and including the string “bash”.

The full path to each file is returned in a list.

In this next example “find” is used to locate zero byte files.

clipboard_ea8dfd650b0bb49cefb12f21909a0e711.png

 

Working with files

wc – line count, char count, word count, max line length

mv – move or rename a file

cp – copy a file

unzip – unzip a zip file

gunzip – unzip a gz file

bunzip – unzip a bz file

tar – extract files from a tar archive, (tar file)

-----------------------------------------------------------------------

wc – line count, char count, word count, max line length

How many lines does the file have?

clipboard_e8648fd2756eab7d04d9883036c578d60.png

More tools of the Bash shell

awk -  text processing, extract columns

grep – find a string in a file or stream, extract rows

  •   grep and awk used together for slicing and dicing

sed – stream editor

  • head, tail, cat, more, less - look into a file

sort – sort content of a file

history – look at past commands

----------------------------------------------------------------

Question – what is the difference between these examples;

head .bashrc

head -10 .bashrc

head -1 .bashrc

tail -1 .bashrc

Hint, run each command and evaluate the output.

----------------------------------------------------------------

grep – find a string in a file or stream

grep case .bashrc

grep -v case .bashrc

Extract all lines that do not contain the string case

grep -c case .bashrc

Count the occurrences of a string

Help! How do I use these tools?

Man pages – the manuals

man find

man du

man test

  press q to quit man page

Sorting contents of a file

clipboard_e79f232314fd95c31fd49c419367a502c.png

 

The for loop

clipboard_ebd45008f4e013e164286ce7a1f55b023.png

The for-loop can be a very powerful tool especially when combined with HPC (high performance computing).

The trick is to understand the many possibilities of what is contained in the “set”.

The following are examples of different sets used in a for-loop.

clipboard_e34031595a81331cfb7161d90e2f8fcb1.png

-----------------------------------------------------------------------------------------------

clipboard_e6c21c3c4994f257041a2c47482246e0f.png

-----------------------------------------------------------------------------------------------

clipboard_e6f71657bfde31c26687a535c1538cf42.png

Things I need to know about the server

clipboard_ed0e85116d5df3f28281eb870ef9a5035.png

How much storage space have I used?

clipboard_e17ce7db128f145cd20b16f6a6793739a.png

What does the following command return?

du -h --max-depth=1 ./

Do I have space for my data?

clipboard_e758c66b4985a2a2ca9ec0cd4b1eaa014.png

Linux command specific to the Raspberry Pi

clipboard_ee150b492e1f25c491613b4aa643915d9.png

The above example demonstrates piping to the cut command.

Questions on the test;

In the above example using the “df –h” command how much storage has been used?  How much space is available for more data?

From “lscpu”, how many cores does your RPi have?

 

 


Introduction to Linux for the Raspberry Pi-command line is shared under a GNU General Public License 3.0 license and was authored, remixed, and/or curated by LibreTexts.

Support Center

How can we help?