Introduction to Linux for the Raspberry Pi-command line
- Page ID
- 23231
\( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \)
\( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)
\( \newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\)
( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\)
\( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\)
\( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\)
\( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\)
\( \newcommand{\Span}{\mathrm{span}}\)
\( \newcommand{\id}{\mathrm{id}}\)
\( \newcommand{\Span}{\mathrm{span}}\)
\( \newcommand{\kernel}{\mathrm{null}\,}\)
\( \newcommand{\range}{\mathrm{range}\,}\)
\( \newcommand{\RealPart}{\mathrm{Re}}\)
\( \newcommand{\ImaginaryPart}{\mathrm{Im}}\)
\( \newcommand{\Argument}{\mathrm{Arg}}\)
\( \newcommand{\norm}[1]{\| #1 \|}\)
\( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\)
\( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\AA}{\unicode[.8,0]{x212B}}\)
\( \newcommand{\vectorA}[1]{\vec{#1}} % arrow\)
\( \newcommand{\vectorAt}[1]{\vec{\text{#1}}} % arrow\)
\( \newcommand{\vectorB}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \)
\( \newcommand{\vectorC}[1]{\textbf{#1}} \)
\( \newcommand{\vectorD}[1]{\overrightarrow{#1}} \)
\( \newcommand{\vectorDt}[1]{\overrightarrow{\text{#1}}} \)
\( \newcommand{\vectE}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash{\mathbf {#1}}}} \)
\( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \)
\( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)
\(\newcommand{\avec}{\mathbf a}\) \(\newcommand{\bvec}{\mathbf b}\) \(\newcommand{\cvec}{\mathbf c}\) \(\newcommand{\dvec}{\mathbf d}\) \(\newcommand{\dtil}{\widetilde{\mathbf d}}\) \(\newcommand{\evec}{\mathbf e}\) \(\newcommand{\fvec}{\mathbf f}\) \(\newcommand{\nvec}{\mathbf n}\) \(\newcommand{\pvec}{\mathbf p}\) \(\newcommand{\qvec}{\mathbf q}\) \(\newcommand{\svec}{\mathbf s}\) \(\newcommand{\tvec}{\mathbf t}\) \(\newcommand{\uvec}{\mathbf u}\) \(\newcommand{\vvec}{\mathbf v}\) \(\newcommand{\wvec}{\mathbf w}\) \(\newcommand{\xvec}{\mathbf x}\) \(\newcommand{\yvec}{\mathbf y}\) \(\newcommand{\zvec}{\mathbf z}\) \(\newcommand{\rvec}{\mathbf r}\) \(\newcommand{\mvec}{\mathbf m}\) \(\newcommand{\zerovec}{\mathbf 0}\) \(\newcommand{\onevec}{\mathbf 1}\) \(\newcommand{\real}{\mathbb R}\) \(\newcommand{\twovec}[2]{\left[\begin{array}{r}#1 \\ #2 \end{array}\right]}\) \(\newcommand{\ctwovec}[2]{\left[\begin{array}{c}#1 \\ #2 \end{array}\right]}\) \(\newcommand{\threevec}[3]{\left[\begin{array}{r}#1 \\ #2 \\ #3 \end{array}\right]}\) \(\newcommand{\cthreevec}[3]{\left[\begin{array}{c}#1 \\ #2 \\ #3 \end{array}\right]}\) \(\newcommand{\fourvec}[4]{\left[\begin{array}{r}#1 \\ #2 \\ #3 \\ #4 \end{array}\right]}\) \(\newcommand{\cfourvec}[4]{\left[\begin{array}{c}#1 \\ #2 \\ #3 \\ #4 \end{array}\right]}\) \(\newcommand{\fivevec}[5]{\left[\begin{array}{r}#1 \\ #2 \\ #3 \\ #4 \\ #5 \\ \end{array}\right]}\) \(\newcommand{\cfivevec}[5]{\left[\begin{array}{c}#1 \\ #2 \\ #3 \\ #4 \\ #5 \\ \end{array}\right]}\) \(\newcommand{\mattwo}[4]{\left[\begin{array}{rr}#1 \amp #2 \\ #3 \amp #4 \\ \end{array}\right]}\) \(\newcommand{\laspan}[1]{\text{Span}\{#1\}}\) \(\newcommand{\bcal}{\cal B}\) \(\newcommand{\ccal}{\cal C}\) \(\newcommand{\scal}{\cal S}\) \(\newcommand{\wcal}{\cal W}\) \(\newcommand{\ecal}{\cal E}\) \(\newcommand{\coords}[2]{\left\{#1\right\}_{#2}}\) \(\newcommand{\gray}[1]{\color{gray}{#1}}\) \(\newcommand{\lgray}[1]{\color{lightgray}{#1}}\) \(\newcommand{\rank}{\operatorname{rank}}\) \(\newcommand{\row}{\text{Row}}\) \(\newcommand{\col}{\text{Col}}\) \(\renewcommand{\row}{\text{Row}}\) \(\newcommand{\nul}{\text{Nul}}\) \(\newcommand{\var}{\text{Var}}\) \(\newcommand{\corr}{\text{corr}}\) \(\newcommand{\len}[1]{\left|#1\right|}\) \(\newcommand{\bbar}{\overline{\bvec}}\) \(\newcommand{\bhat}{\widehat{\bvec}}\) \(\newcommand{\bperp}{\bvec^\perp}\) \(\newcommand{\xhat}{\widehat{\xvec}}\) \(\newcommand{\vhat}{\widehat{\vvec}}\) \(\newcommand{\uhat}{\widehat{\uvec}}\) \(\newcommand{\what}{\widehat{\wvec}}\) \(\newcommand{\Sighat}{\widehat{\Sigma}}\) \(\newcommand{\lt}{<}\) \(\newcommand{\gt}{>}\) \(\newcommand{\amp}{&}\) \(\definecolor{fillinmathshade}{gray}{0.9}\)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).
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.
Path and Permissions continued:
Permissions
Run the above commands on your RPi and notice the changes in file permissions.
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.
Here the ability to write to the file is restored.
Next is a demonstration of our ability to remove read permission for a file.
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.
Now the file containing the data “echo string” is a program.
Running the program produces results.
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.
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”.
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.
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.
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
- ./
Path, where are my programs?
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.
Finding files by type
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.
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?
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
The for loop
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.
-----------------------------------------------------------------------------------------------
-----------------------------------------------------------------------------------------------
Things I need to know about the server
How much storage space have I used?
What does the following command return?
du -h --max-depth=1 ./
Do I have space for my data?
Linux command specific to the Raspberry Pi
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?