Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 166

First steps in the terminal

Setting up your Terminal


Let's now have a look at the first steps in the terminal.
And for that, we first need to open our terminal app on CentOS.
We can just click here into our menu and then down there we can just launch a
terminal and then we can
see this is now here our terminal on Ubuntu.
It looks slightly different if I go here into my ubuntu.
We can see here.
I need to click or i can click here on activities but nothing happens here.
The way to launch the terminal is that I need to click here on show applications and
then I need to
look for our my terminal app and then I can just launch this terminal with a click.
One thing I want to emphasize here, those terminal apps might look slightly
differently.
So if I would go here, we can see this terminal app looks differently than this one.
This is completely normal.
Later here in this chapter, sorry, in this course, we will have a look at how we can
configure the
look and feel here of our terminal.
How, for example, this one here will look like which colors are used there and all of
that.
For now we can just leave it at how it is.
One thing I do want to emphasize here is that this is now just a normal text
based interface to our computer.
That means we can type in some text here, for example.
Hello world.
I'm not saying this is now like a super valid command.
We can see here there's even an error then printed.
But what we can see here is that we can print out that we can enter something here,
confirm this with
an enter key, and then it's trying to do something here.
And in this case with Hello World, it doesn't really know what to do.
And we can see there's an error message here.
We don't need to have a more in-depth look at this for now, we will have a look at a
more proper way
how to output something here pretty soon.
There's one more thing that I want to emphasize here.
Here in our terminal, we can go into the right click and then into the preferences.
This allows us to configure this terminal.
I highly recommend changing this a little bit.
So, for example, in my case, I like to set a custom font, and in my case I really like to
increase
the font size because then it will look better here in the video.

Then you can also change the colors.


And for example here, I might change the colors to like a more bright setting
because I feel like then
everything is better or is more readable here in the video.
So feel free to play around here and choose something that you like.
There are different ways you can use here.
Maybe you like this style of a terminal the most, then just use this one.
It doesn't really affect the actual stuff that we are executing in this terminal.
I will now choose another color scheme here because I really want to make sure that
it's well readable.
I think gnome light would be a good way to go here for me.
One more thing before we look into how proper commands work.
The one more thing that I want to do here in this lecture is that I want to confirm that
you're actually
using Bash.
The reason is that certain Linux distributions might have different default shells, or
maybe you are
running a different Linux distribution and maybe it's not having bash as a default
shell.
So we should confirm this and usually this way is the best way to confirm it that we
just say echo and
then double quotes, then a dollar, then those curly braces and then bash underscore
version, curly
brace, close and then another double quote here.
And this should output something if we are running in a bash.

Let's just have a look here.


I can now just say echo and then here, bash underscore version and then I just
execute this.

Then we can see here, as of now, I'm running here in this version, be aware,
everything above five
doesn't really matter.
So if I'm running five one here, that's all right.
If maybe in the future there's five eight released, this will be all right.
Or even if you're at some point running version six.
This is also all right.
Whenever there's a major update for Ubuntu or CentOS, I will make sure that this
course is up to date.
So this is this just happens to be the version that I'm running here right now by the
way.
Really make sure that everything is exactly identical.
How I've provided it here.
If you have struggle typing in those characters, be sure to go into your settings here
and then into
the keyboard tab and be sure to select the correct input source.
So if the keyboard doesn't work properly, then the keyboard isn't configured
properly and you need
to change to the language of your keyboard.
In my case, I'm used to the Mac German keyboard, so I need to select German
Macintosh here.
In your case it will probably be something else then.
Let's now go back to the slides.
If it doesn't output anything or just an empty line, then it's important that we launch
bash with the
following command by just typing in bash and then hitting enter.

And for now, you need to do this any time you open a new window or a new tab in
your terminal. So here in this case, it is.
Bash So I could just open a new tab here, and this is now.
Bash So I don't need to do anything here.
But let's say, for example, my default shell was something else here on Ubuntu.
If I run, I do not launch a bash.
At least in my configuration.
It's a different shell.
It's called dash with a D at the beginning.
And I could just launch this.
But let's say for example my terminal which would launch in this view here we can
see here it looks
different, but it would not necessarily need to look different depending on how this
dash is configured.
But if if my terminal would launch like this, I could find out, for example, here, if I run
this
command here, that I'm not running in a bash because this doesn't output anything.
In that case, I would then need to enter a bash and then hit enter to actually end up
in a bash.
So if your terminal launches in this view, you need to enter, bash and confirm this
with enter to
actually end up in a bash.
And now we can confirm that we are actually in a bash by outputting this variable.
And now everything works.

Be aware here.
Those are all independent terminal sessions here.
So that means if I open a new tab here and this was not in a bash, then you would
also need to enter
bash here and confirm this with an enter here.
In my case, the default is a bash, So technically I would not I do not have to do this
here.
Nonetheless, be aware that you do need to do this.
Then every time you open a new tab or a new terminal window, all of those are
completely independent.
What you do here doesn't influence what you do here, and vice versa.
Later here in this course, we will also have a look at how you can change the default.
But for now, if this is not a bash, then be sure to always launch a bash by typing in
bash and then
hitting enter.
So now we have confirmed that we are in a bash.
And I would say let's now start writing our first real bash commands.
Outputting Text: the Command `echo`
Let's now have a look at a first command that we execute in the terminal.
And for this, I want to introduce you to the command Echo.
This allows us to output text in the terminal.
We can test this by just saying echo and then bash is amazing.
Please note here for now, the string that we want to print should be wrapped in
single quotes.
In Bash, they don't necessarily mean that we define a string as another programming
language.
They mean something else.
Those single quotes.
But nonetheless, they are really important for now.
But don't confuse the quotes and bash with quotes from other programming
languages.
They work completely differently here in Bash, but we will learn more about that and
the reasoning
behind that in the chapter about expansions.
So for now, it's just important that we really write it exactly how I put it here on the
slide.
So if we just execute this here, echo bash is amazing and then I can just hit enter,
then we can see
here, this is the output that has now been generated.
The way it works here is that the first thing that we write here, this is considered the
command or
the program that we want to run.
And everything after it's considered that those are the so-called arguments to this
command or to this
program.

So Echo is our command and everything behind this are our arguments.
All in all, we can then also say that everything here is one bash command.
So this would be our whole bash command.
Let's have a look at this again.
So here, this would be our command that we want to do.
This is our argument here.

In this case, we only have one argument, which is bash is amazing, exclamation sign.
And if we execute this by hitting the enter key, then we can see here our bash
command has now been
executed.
There are more things that I want to emphasize here.
By default, Echo will output a line break at the end.
We can disable this with the option minus n.

So for example, if we would run something like this, then we would then it would not
print a line break after.
Let's just have a look at this.
Here I could now just type in everything again.
Bash is amazing.
And just press enter to execute it.
And then we can see here after this it has not printed a line break and this is now
why my terminal
here starts without a line break.
This is totally all right.
By the way, usually all the commands that we execute, they usually end output a line
break at the
end of them.
Here in this case, this didn't happen.
So our terminal now starts here in the middle of a line.
This isn't completely normal.
And this is to be expected.
By the way, instead of typing this again, I can also use the arrow key up to just go
through my history.
And here, for example, I can see the commands that I typed in previously and I could
just select one.
And then for example, edit this, add this minus N Here again, I navigate through this
command with
my arrow keys and then I can just add something here and then I can just re execute
the same command.
Or I could also now, for example, here go one letter to the left, add a few more
exclamation signs,
and then press enter.
And there we go.
This one here has worked, by the way.
Sometimes if we are starting here in the middle of a line, it might be that it didn't
show that I entered
multiple exclamation signs here.
In that case, the solution would be to just print and to just press enter again, that we
start in
a new line here and then go back.
And if I would now hit the exclamation sign multiple times, it will also show
appropriately here in
our command.

Just be aware if you are not starting here from the beginning of the line, it might not
show if you press this exclamation sign multiple times, but it will still count.
So here, for example, if I would go back here, back to this one, I would go one to the
left and I would now press the exclamation sign multiple times here.
In this case, it now did show everything properly.
But here, before you can see, it didn't show everything properly.
And this was because the command started within a line.
So those would be considered so-called options.
So we have our command. That's our echo. Then we have an option here.
The option is sometimes also considered to be an argument as well.
So we can see it in both ways here And then what we actually want to print that
would be bash is amazing.
But there's one more thing that I want to emphasize.
Usually a lot of those commands have different options available, and one additional
option is the
minus E option.
This enables backslash escapes.
Let's have a look at this.
So, for example, here we would output something that consists out of multiple lines
and we can indicate
that we want to put a line break here in between by using backslash n.
But in order for this to be converted into a normal line break, we need to also specify
the minus E
option.
Otherwise it will just stay at a backslash n.

Let's just have a quick look at this.


And here I'm in my terminal again and I want to just clear my screen to show you the
next example for
this one.
More command is the clear command that I can just enter here and then confirm
with Enter, and this
will just clear my screen so I can start from scratch again.
Now I can try Echo here and I could, for example, say here Echo Bash is backslash n
amazing, for
example.
And if I would now just execute this by pressing the enter key, then we can see here
it's now just
printing this one.

But if I go back with the arrow key up now I use the arrow key to go to my left.
And if I now enable this minus E and then a whitespace here, then I can press enter
again.
And then we can see how this backslash n this so-called backslash escape here is
converted into a line
break.

And this allows us now to print this here in multiple lines.


This is usually the structure here of such a shell command.
We have a command that we want to execute.
Then we have one or more of those options.
And then quite often we do have those arguments.
Of course, sometimes we only have a command that we want to execute.
Sometimes we don't have those options, sometimes we don't have here additional
arguments that we want
the our command to work with.
But this would be like the full structure here of a command in our terminal.
And we just confirmed this execution by pressing the enter key.
One thing I want to emphasize is that you can also combine those options.
So, for example, we want to combine this minus E and this minus N, then we can just
combine them.
So we could say minus E and minus N, and this would then for example here, minus E
would enable that we can use a backslash n here and minus n as we have seen
before, would disable the line break at the end.

Let's just have a quick look at this.


I go back here here in this case I will leave it at bash is backslash n amazing.
I want the backslash n to work and I also want to prevent that echo outputs a line
break after it so
I can just execute it like this.
Press enter and then we can see here a line break at the end.

Now here has not been printed here.


But I want to emphasize here that we have different ways to combine those.
We can also usually just write all of those options in one block.
This is just a shortcut here.
And of course, the order here of those options usually also doesn't matter.
Why, Usually because this is part of the responsibility of this echo command to
understand everything correctly.
And depending on how that is coded, it might not be supported.
So this depends on the command that you execute.

But most Unix command, if they support options, you can always combine them.
But again, it does depend on the command.
So every now and then you might find commands that require, for example, this
format here of options.
Let's have a look at this.
If Echo here allows us to combine it.
So I press the arrow key up, then I go back and now I just combine all of those
options.
And as you can see here, it's now still working exactly the same.
We are able to output batches of line break.
Amazing.
And after that it's not adding a line break.
This might seem like a simple example here.
Or maybe you're like, okay, why is this so important that I can add line breaks here in
between or
prevent the line break here?
And I need to tell you, this is sometimes important because later, for example, we
could take this
output and write this into a file.
But also the concepts here are extremely important that we have a command, that
we have options here,
and that we then have arguments here to our command and then everything is
executed here.
So this is extremely important also from like a concept here, and it's important that
we introduce
this now.
The reason is that we now want to start building on top of that.
And in the next video lecture, we will have a look at how you can navigate the file
system.
Displaying and Changing the Current Directory: the
Commands `pwd` & `cd`
Let's now have a look at how we can navigate the file system within the shell.
And the first command that I would like to introduce you to is the PWD command.
In our terminal, we are always in a directory.
This directory is called the so-called working directory.
It is the folder that the shell is currently operating at.
Commands and also programs can access this folder and they can act relative to this
directory.
If we want to find out our current directory, we can just use the PWD command and
this stands for print
working directory.

Let's just have a look at this.


Here I'm in my terminal and anytime I'm in a terminal, I'm also in a folder.
And to print out the path to that folder, I can just use PWD and then we can see the
folder.
Here right now is Home slash, and then my first name and my last name.
This is just my Unix username that I have here on my computer.
This will probably be different.
Also this one here, this slash home here might be different.
For example, on a mac, it might be slash users.
Um, this is the folder that I'm currently operating at.

Please note that on Linux systems we do not have drive letters.


Everything is in our root folder.
So I could also, for example, go into this the same folder if I'm here in my file
browser.
And I would then for example, navigate to my root directory through other locations
and then I navigate
to my root directory, then into my home folder, and then this would be my user
folder.
So this is the folder that I'm currently at here in my terminal.
By the way, if you're wondering how to external drives work, if we don't have drive
letters, well,
they end up in this hierarchy as well.
So, for example, external drives, they usually end up here on our root file system and
then either
in this folder or in this folder, it depends on how they are being made available and
how that is configured.
But here, for example, external drives might show up in this slash media folder just as
an example.
So everything here is in this directory structure here on our Linux systems, but don't
worry too much
about it.
There's a separate chapter about file, how files work on Linux, specifically on Linux,
how the file
structure works there.
So we do not need to get into that for now.
Another thing that I want to emphasize here is that certain shells might be
configured in a way that
they show us the current directory we are at.
We can see this here.
Here we can see that it's printing the tilde character.
And this means that we are right now in our home directory, in our user folder.
This is just the shortcut for our user folder.
Just be aware that this highly depends on your system configuration and this might
change with an update.
Or if you're, for example, on CentOS, this stuff here before the command looks also
different and
the shell might also be configured in a way that only a dollar is shown or that even
nothing is shown
here at all.

So this is something that can be shown here.


If it doesn't show again, there will be a separate chapter how to configure that.
But I want to say here, don't worry about that for now.
Right now, this is the way that always works to print our current directory.
But now that we've seen how we can see our current working directory, how can we
change our directory?
And for that we can use the CD command and this stands for change directory.
And just a few examples.
One way would, for example be CD space and then the name of the directory, and
then we would move
into that specified directory just as an example.
CD Space desktop, for example, would move our terminal into the desktop folder.
Just or let's just have a look at this.
So first I will open my file browser again.
It's now already here in my home directory in the home slash, and then this my first
and last name
as a username.
But this will probably be different on your system.
But here we can see we got a folder that's called desktop and I could move into that
by saying change
my directory into my desktop folder.
And here you can see that I kind of like magically typed it.
And the way I did this is that I used a Tab autocomplete, so I can just type the first 1
or 2 letters here and then I can just use Tab to let the system complete this for me.
And here in this case, I could just change into that folder.
This one here just means, I mean I could change into that folder like this or I could
add another slash here at the end.
It doesn't really matter. Now we are here on my desktop.

We can see the way my terminal is configured.


It shows me I'm in my home folder on my desktop and if I now press P or enter PWD,
we can also see
that this path here has changed.
Be aware items that are on the desktop depending on your system configuration
might be shown on your
actual desktop over here.
Here for example on Ubuntu, this is also completely separate.
What's shown here then?
What's in this folder here?
Just be aware of that.
This is now just a folder that we can access and it doesn't necessarily mean that
everything will show
up on your normal desktop wallpaper as well.
But what's important here is that this is just a folder that we can use, but again, we
might need
to use the file browser to list the contents here.
But there are also other ways how we can use the CD command here.
Let me go back to my slide and one way would, for example, also be that we could
provide an absolute
path and here we are just like, Hey, I want to change into my so-called root folder
where everything
starts.

If I navigate into that CD slash, then we can see if I now enter PWD.
We are now here in this folder.
Another way how I could also navigate into my desktop would be that I navigate with
a long path into
my desktop.
So slash home slash username slash desktop.
Also here I'm using extents or I make a lot of use of the tab autocompletion.
And if I now hit enter, we can see here we are now back on my desktop.
The next thing is that we can also just navigate a folder up.
So this would be just like a CD dot dot and this would move into the parent
directory.
So that means for example, here we are on my desktop right now, I could move into
my parent directory
and now we could see I'm back in my home folder or I could even navigate two
levels up by saying CD
dot, dot, dot, dot.
And now I'm in my root directory.
So you can see how I'm able to navigate around here.
Also, if I now want to go back into my home directory, I could then also just use CD
and then the
Tilde character or just CD without any arguments here.
And this would move me into my home directory.
Let's just have a look right now here I'm at my root directory, but I want to move into
my home directory
so I need to enter this character.

If I now execute this, I'm now in my home directory.


But another way to do the same.
I will now just navigate back into my root would be to just use the CD command
without any arguments,
and then I would also navigate into my home directory.
Or another way, of course, if I wanted to move into my desktop, I could also write it
like this.
This will resolve to my home directory and then I would move into my desktop of my
home directory so
you can see how you can combine all of those path names and you can just use
everything here.

So this would then move to the user's desktop.

So now you know how you can show the current directory that your terminal is
working at and you can
also or you also know how you can change this directory.
However, we now want to do something with those directories.
So in the next lecture we will have a look at how we can list the contents of those
directories as well.
Listing Content in Directories: the Command `ls`
Let's now have a look at how we can list the contents of a folder, and for that we can
use the LS command.
This prints out the contents of our current working directory.
Let's have a look at this here.
Right now I'm in my home directory.
My terminal here is configured in a way that it that it shows me my current working
directory.
But I could also use PWD and to list the contents here.
I could now just use LS and confirm this with enter and then we can see this are the
contents of my home directory.
Let's just confirm this.

For this I'll open my file browser and here this is my home directory and we can see
those are exactly the same folders that are now also shown here on my terminal.
Please note that depending on your system configuration, this might not be shown
in colors.
It might just be black and white.
This is completely normal.
This really depends on how your system is configured.
Again, as you probably expect by now, more on that, of course, later here in this
course.
We can also add additional options here to our LS command.
So the syntax is the following.
We can add additional options here and we can also say that a specific path should
be listed.
Let's first have a look at the options minus A for example, will list all entries,
including, for
example, hidden files.
Let's see how this works.
Here we are in my home directory and I can just say ls space minus or dash A and
confirm this with enter
and then we can see that there are actually quite a few more folders here.
Here I can see that it's a folder because it's colorful.
This is what the colors symbolize, the type, whether it's a normal file or a folder.
And we can see we do have those additional folders here and also additional files
here that are hidden
hidden files on Linux or on Unix systems.

By the way, they always start their file name with a dot.
So those are just hidden files.
This is the reason why they didn't show up here in our file browser, because here by
default, hidden
files are not shown.
But I could say here I want to show hidden files and then we can see those hidden
files here are now
shown as well.
In this output we can see here there are also those files or those folders that are now
now shown.
The dot folder is just the same folder.
Dot dot means just that we can go a level up and they are just included here in this
output as well.
But aside from that, those are now here, all the documents or all the files and folders
in my home
directory.
Of course, I can now also just change into a different folder, for example, in my root
directory
and there I could now just execute the same command again and then we can see
those are now the contents
of my root directory here of my file system.
The next thing is that we also have additional options here available.
For example, minus R to reverse the order.

Then it will no longer be scanning, but scanning alphabetically.


Or we could also, for example, sort by modification time.
Let's just have a look at this.
So here I will move back into my home directory.
I will clear my terminal so I can start over again.
And I will now use minus T because I want to sort by the time and then we can see
here now, well this is now sorted by time.

We can also reverse the order by adding an R here.


So I could say minus R space minus T or I could also, for example, just write it like
this.

This would be completely identical.


And then we are just reversing the order.
So you can see here how we are also using those options here and how those
options can be combined.
Another option that we have and I want to introduce this here as a separate option,
because this is
a pattern that we have as well.
A lot of options are just one single character, for example, A, R and T, but there are
also more
available there.
But we also have options that we can that are consistent out of a word here, for
example, dash, dash
color.

And this enables colorful output in case it's not enabled here.
In this case, for example, Ubuntu is configured in a way that by default it will show
colors here.
So this is enabled by default.
But we could set it, we could put an equals here and set this for example to never to
disable those
colors.
Let's just have a look at this.
We are right now in my home directory dash dash color equals never.

And then we can see right now we have turned off those colors on Ubuntu by default
it will be set to
auto so ls figures out from where it's being called and if it's called from a terminal
then it will
enable colorful output.
But here I have now specifically overridden this default behavior by setting this color
to never sometimes
those longer options with two dashes and then a word can also be accessed by just
one single letter.
Here in this case, this color option, we need to write it like this.
And we can either just write it like this without an equals behind.
Then we set it to the default value.
If we enable this, this the default, if we use minus, minus color here would be auto.
That means that ls will figure that out for us.
Or I could here also just specify this and then I could set it manually here.
The most important takeaway is that we do also have those additional options here
that we do need to
specify separately.
So this means if we also wanted, for example, to add something like this here, to
display all the
files here and disable color, we would need to for to use those options individually
and then we would
need to use those dash dash options here separately.
We cannot combine them into something like what we've seen here.
This doesn't work.

We can only combine those single letters, those longer words as an option.
They always need a dash dash or a minus minus in front.
Um, the next thing that I want to emphasize is that we can also specify a path that we
want to list.

You remember in the lecture before I said that our current working directory, the
commands that we
use, they will take this current working directory and do something with this.
This is what this ls command is doing by default.
By default.
Let's say here I was in my home directory and I would just execute this.
It would display the contents of my home directory.
But I'm also able to provide a path here.
And in this case, what I use here as an argument will override the path that otherwise
would have been
provided by my current working directory, meaning this one here, for example, will
now print out
the whole contents of my root folder here of my Linux system.
So as you can see, we are still here in our home directory.
Our current working directory has not changed, but if we do provide an argument
here, then it will
take this as a path instead of our current working directory.
This is common behavior that also applies to a lot of other utilities that we can use
here on the command
line.
So I want to make sure that I really emphasize this.
There's one more thing that I would like to make abundantly clear, and that's the
distinction between
relative paths that work on the basis of our current working directory and absolute
paths that provide
an absolute path to the file or folder that we want to work with.
And this is what we'll talk about in the next lecture.
Path Types: Absolute Paths vs. Relative Paths
Because we will be working a lot with files and their respective paths.
I want to add or I want to have this lecture here in this course that we dedicate to
absolute versus relative paths.
I know maybe if you've programmed already that you might have heard a little bit of
this, but I really
want to make this distinction here.
Absolute paths start with a slash and they define the complete path to a file or to a
folder.
Thus they work everywhere, no matter our current working directory.
Just as an example, if my username was was just Giannis, this would always be my
desktop or this would
also always be my desktop.
Why?
If I say here all the absolute paths start with a slash, why is this one here included in
the example?
The reason is if I use this in a bash, I don't want to talk too much about it yet, but
Bash takes
this command, rewrites it a little bit so that it ends up with this absolute path before
even the CD
is being called.
So here if we go go back and I wanted to move to my desktop or change my
directory, I could say either
this one here and I could then navigate into my desktop or I could type it also like
this.
And this would also be considered an absolute path because.
Bash, Just like under the hood, rewrites the command for me.
And this will then be my home directory with the full path to my home directory, and
then I navigate
into my desktop here.
So those paths work everywhere, no matter where I am.
Then, however, we also have relative paths and they are being resolved according to
our current working
directory.

Just as an example, this one here would take from my current folder and go into the
desktop folder
or this one would also start at my current working directory and go into the desktop
folder.
So they are exactly identical.
However, this one here would go one level up and then go into the desktop folder.
Let's also have a look at this here.
I'm already at my desktop and if I would move for example, one level up, I would
then be in my home
directory and then I could be like, okay, here I want to now navigate into my
documents folder.
This is relative because this path starts at my current working directory and works its
way through,
meaning we go one level up and then into the documents folder.
Why is this important?
Well, imagine, for example, I was in a different folder, let's say here, my root
directory, and
I would try to do something like this and then we would find out that there is no
such file or directory
that we are unable to navigate into this directory.
The reason behind is that now of course, this starts at my current working directory,
which is now
my root file system.
And then of course this change directory command cannot be fulfilled.

However, if I had provided an absolute path to this folder, for example like this, then
we can see
it works from everywhere.
We can always navigate here into that folder.
Be really careful here, especially if you navigate into a different folder and then your
you just use
the arrow keys, for example, to repeat a command that you executed before.
The current working directory will still be the new current working directory.
So if for example, you would use the arrow keys, I have now typed it, but if I had now
used the arrow
keys to repeat a command like this.
But in the meantime my current working directory has changed.
Then of course this command will fail and we are unable to navigate into that folder.
So this is the distinction here between relative paths that start on the basis of your
current working
directory versus absolute paths.

Executing Multiple Commands using `;`


Let's now have a look at how you can execute multiple commands one after another.
And for this you could of course, just use enter and then type in the next command.
But if you wanted to execute multiple commands at once, you can just add a
semicolon in between them.
And then first the first command will be executed.
And then the second you could end this with a semicolon here, but it would
technically not be required.

You can also just omit this, for example, here.


This would now execute two commands here, one after another.
Let's just have a look at this echo minus n hello, and a space and then a semicolon.
And then I say echo world.
And now if I just hit enter, we can see here this minus N stands for that.
We do not want to print out a line break after the first echo.
So this will just output hello and a white space.
And then after that we will just print out world.
So let's have a look.
This is now here.
Hello world that has been printed here.
Of course, we can also combine this or use this with other commands.
So for example, I could say I want to navigate one folder up, then I want to navigate
into my desktop
and then I want to execute the LS command on my desktop then.
And if I now hit enter, then we can see here this one here has now changed to my
desktop and it then
has outputted the contents of my desktop, the contents of my desktop or the
content.
There just happens to be completely there are no files there.
So this is why this hasn't printed anything here.
But we can now try this again.
We could, for example, change into our root folder, then we could change into our
home folder.
That would be this folder here, for example.
And then I could do an LS on that folder.
And as you can see here, this one here has worked.
We have now changed our directory here multiple times and then we have outputted
this and this can.
This just shows how we can combine those commands.
There's just one thing that I do feel like I should mention here.
I don't like to combine commands that often.
I mean, it might happen from time to time, but I but it's a little bit tricky here because
one thing
that I want to emphasize here, if I wanted to navigate, for example, onto my desktop
and I'm in my
user folder right now, I could just start typing desktop here and use tab to
autocomplete to just conveniently
navigate into my desktop.
This auto completion logic does not.
It only takes into account my current working directory and yes, this one will change
my current working
directory to my root file system.
But it has not been executed yet.
So for now, while I'm still typing here, my current working directory is my home
directory, which
means that I can get auto completion here to complete the path to my desktop.
But by the time if I now hit enter by the time this CD is executed, my home.
Sorry, my current working directory has changed to my root directory before already
and then I'm unable
to navigate here into my desktop.
So be really careful here if you really want to change the directory here.
But for something like this where we just change into a different folder and then just
list the contents
and I don't need any Tab auto completion to write my command here.
Combining those commands can be quite useful.
It's important that I do introduce this here because this might also be something that
I use on slides
every now and then.
In this case, this means you can either execute those commands individually with an
enter in between
or you could all put this into one line and execute them all at once.
And this is how you can execute multiple commands at once.

Getting Terminal Help: `man` and `--help`


Let's now have a look at how you can get help if some problems happen.
How do you get help?
How can you solve those problems?
And for that, you can first try to use a dash dash help or a minus H.
And for a lot of commands this is implemented.
We can just add a minus H or dash dash help or minus, minus help after that.
And this will then usually show us a list of possible options and arguments and how
we can use this
program.
Let's just have a look at this here.

I would be on my CentOS, but it doesn't really matter.


I can just say LS minus H and then we can see here for the LS command.
This doesn't work, but I can use ls space minus minus help.

And then we can see here we do get some documentation, even the color here it's
being written how we
can use that and we can scroll up here a little bit and then we can see there are a lot
of different
options here.

It may look a little bit congested here and difficult to read, but the reason is just that
I have configured
the font size to be so large that you can easily read it here in the video.
But then of course, the line breaks here.
A lot of lines would only be one line if I had zoomed out a little bit.
And if I zoom out a little bit here, then we can see it's actually quite a nice list.
And this allows us to see how all of those options can work or what options we got
available here.
We do not need to go through them.
It's just a nice way or this is just the way you can use to get help.
Be aware this is subject to the command so certain commands might implement that,
certain commands
might implement that properly.
For example, on certain distributions on Linux, maybe on another, there might be a
slightly different
version that's used or sometimes or actually sometimes this does happen.
The LS command, for example on a mac would not display such a nice help menu as
the compared to the
Linux version there.
So be aware that there are differences between commands and this is subject to the
command.
The next thing this is also a little bit depends on the program, but we do have a
special command to
get help.
This is man and this allows us to check the built in manuals.
And for example, man space LS would print out a manual for the ls command
important for this?
The man pages or the so-called documentation must be installed on your system.
Let's just have a look at this.

I can say man ls here on my CentOS and then we can see here it prints out a manual
that we can just
read.
And this usually also then here lists all of those entries if we try the same on or to
quit this, by
the way, we need to press Q if we try the same on an Ubuntu though, and I would do
an LS, then we
can see here that it's complaining the system has been minimized by removing
packages and to restore
contents we need to run a specific command that's called minimize.
And then we also need to install a package here.

The reason is that this is a desktop version of ubuntu and usually desktop users don't
need those man pages.
They don't use the command line that much.
So it's okay if they are not included here.
If we wanted to install them, we first need to run an minimize this command and
then we need to install this man minus package.
This unminimize package changes the configuration, for example, that those man
pages get installed and it will then also reinstall quite a bit of packages or software
because only when those packages are or software tools are reinstalled, the man
pages are then also installed.
Before that they were just thrown away.

So I will now confirm this with a why.


We can see here it didn't work.
We got a permission denied.
We do need to add a sudo here in front so the command would look the following
sudo unminimize.

We will have a look at sudo here also later pretty quickly actually.
But for now, please just add a sudo here in front and then type in your normal
username or your normal
password for the normal user here on your Ubuntu system and confirm this with
enter.
And now I will just confirm with a y.
Now we can see here it's doing something here.
What's doing now?
It's, it's refreshing what updates are available and all of that stuff and it's now
offering to install
all of this.
So I will now just confirm this with a Y as well.
And now I just need to wait until everything here has completed.
Once this is done, it might tell us a few messages here.
In this case, I will just confirm everything here with Enter.
There's nothing to do here.
Just press enter whenever it asks you something or answer everything with a yes.
If it asks you a yes no question.
Aside from that, we just need to wait until the install here has finished completely.
Once it's done, we just need one more sudo apt install man minus DB.
This will install those man pages on our system that our manuals work how we install
software.

We will have a look at this shortly.


For now, just please execute this command and then we will look into the details
here later.
This here now here has worked already.
It's already installed.
Nothing needed to be done after that.
It's best practice to just reboot the system.
We could do this here through the terminal.
Or I could also just go here into this menu, power off, log out, and then just press
restart to restart
my system.
So now I'm just waiting a little bit until my system has booted again and then I'm
ready to use it.
So now I'm back here and I've launched a terminal again.
And now I can just say, man, And as you can see here, I'm now also able to read
those manuals here
on my Ubuntu system.
In case neither help nor minus H nor man works.
You can also just try to find the online documentation for the corresponding tools
and otherwise communities
such as, for example, StackOverflow or Reddit's Linux communities can also be great
for help.
Be sure to then really search online for your issue.
If it's something that's related to this course here.
You can also, of course, ask a question in the question and answer area and I'm sure
other students
can help you or otherwise.
Also, we will try to do our best to answer as many questions there as possible.
Just be aware it might take a few days.
So this is how you can get help to specific Linux commands and yeah, how you can
keep going then and
how you can approach problems like this.
First steps with Linux
Intro

Information User Managment


In the upcoming videos, I will demonstrate how to temporarily elevate user rights to
execute/run programs or commands that require higher privileges. This is helpful and
sometimes even necessary - even for simple tasks such as installing additional
software.
Our goal for now is to just get going and to start using Linux. Thus, I'll now begin
explaining some basics about user management - and we'll then leave it at that (for
now).

But don't worry, we have an in-depth chapter about user management later on in
which we'll go through all the details! If you'd like to watch it now already, feel free
to skip ahead to here.
User Management Basics: System Accounts, Regular
Users, Superusers
Let's now have a look at how user management works.
And in Linux, users can be categorized into three general categories.
One are system accounts.
They are responsible for running background tasks on your system.
So, for example, if you were running a web server or a database, those would be
running under an
independent user.
They don't have a home directory.
We will look into that later.
For now, the other two categories are more important.
The next category is regular users.
They have access to their own files and directories in a sense that they have a home
directory.
They cannot perform administrative tasks or access other users files without
additional permissions.
So they are just normal users that are on our system.
Also, the user account that Ubuntu are sent to has created during install that was a
normal or a regular
user account.
They by default cannot perform administrative tasks or access other users files.
Then we also have one more user that's the so-called super user or root user, and
this user has unrestricted
access to the entire system, including files in the home directory of regular users.
This super user can add or remove.
Other users can install software and can do a lot of changes there.
The super user can also change the system configuration and can do pretty much
everything on your system.
They are pretty much no restrictions of what the super user can do.
Let's have a look at a regular user.
This is here, a regular user, as we can see here.

I can just log in or in this in this case, resume my session here and I can do normal
tasks here on
my operating system.
I can browse files, I could create files, I could edit files.
So I could do quite a bit here already.
But there's one thing this user cannot do, and this is anything that relates to the
system administration.
For this, we can see this.
For example, if I go here into the settings and then I select here, for example, I go to
users because
I want to manage the users that are here on the system.
Here you can see the system is configured that we have one more user here.
It doesn't really matter, but if I wanted to change the settings here, we can see here
I'm unable
to add a user or to change any settings.
For this, I would need to give this regular user temporarily additional privileges here.
I'm not going to do this now.
This mechanism we will explore actually in the next lecture, but we can see here, by
default I am
unable to change.
For example, another users, another user account here or I would not be able to also
access those
files.
So what we are seeing here is a great example of a regular user.
The super user, however, gets always unrestricted access to the entire system and the
super user can
always do everything on our system.
As you've seen before, there are situations in which we want a regular user to
temporarily gain additional
privileges.
For example, to manage additional users through this user interface menu that you
have seen just a
minute ago or so.
And for this, there's a mechanism implemented here that's called Sudo, which stands
for super user
Do and which allows a regular user to temporarily gain additional privileges.
Elevating Privileges: the Command `sudo`
Let's now have a look at how we can elevate our privileges, at least temporarily, as a
regular user.
This is extremely important because we need this as a foundation in order to keep
our system up to date
or to install additional tools.
But before we can have a look at this, we need to have a look at the sudo command
and we can do this
with a sudo command.
And this allows us to temporarily elevate our privileges.
So for example, this one here would execute the rest of the command here with
additional privileges.
Usually this directory would be the home directory of the superuser of the root user.
The root user's home directory is not in the folder slash home slash root.
It's directly under slash root.
So usually as a normal user, we are not allowed to access this directory.
Only the root user can access it.
However, if we put a sudo in front, we temporarily elevate our privileges and this
allows us to access
this directory.

Anyway, we can have a look at this.


Here I am on my system terminal.
I now want to have a look into the home directory of my root user.
We can see here I am not allowed to open this directory and to read its content.
We get a permission denied because I'm a regular user and a regular user is not
allowed to access this
directory.
However, if I put a sudo in front then we can see it's asking me for my password if I
enter my password
again, this is not the root user's password, it's this user's password on my Linux
machine.
So the password of the regular user.
I just need to repeat my normal password that I use all the time here.
If I just repeat this one, we can see here there's one folder here in this directory, by
the way,
depending on how you installed your ubuntu, it your regular user that you are using
by default might
not have this sudo enabled.
This is something that needs to be enabled for all users, especially also, for example
for CentOS.
There was an option during the install when we configured the regular user, a
checkbox that we could
tick that said something like make this user an administrative user or something like
this.
If you didn't take this, then the sudo command probably will not work and you will
get a permission
denied there.
Or it will say, Hey, sudo is not allowed.
If that's the case, don't worry too much about it.
In the next lecture I will provide you a quick troubleshoot for that so that you don't
need to reinstall
your whole system For now.
For the rest of this lecture, let's just assume that sudo works properly.
I want to show you how severe those additional privileges are.
Let's say, for example, I were to add another user here.
I will just unlock this.
This is also working on a similar mechanism, just that it's hidden here in this user
interface.
But if I click unlock here, we can see this is also just my normal account password
that I need to
repeat here and then I can click on Authenticate and now I can make changes here.
And let's say, for example, I would create another account here.
I would could use a standard account type.
Then, by the way, this user that I'm now adding will not have sudo privileges If I do
do this, this
user has this sudo command available.
So this might, for example, just be a standard user.
And then I could say for example, Iannos(work) or something like this that could be a
full name.
And I will also set a password.

I usually that should of course be a different password than I use for my other users.
If I now add this one, we can see here we now have an additional user here and this
user should now
also have a home directory.
We can see this by going here into our file browser, other locations, computer, then
home, and then
we can see this is now this home directory.
And as you can see here, we are not allowed to access this directory.
However, the user that we are running under right now and this user here, this user
has additional
privileges available.
Those sudo option or this administrator option from the menu before this was
enabled for my main regular
user here.
So my main regular user can temporarily elevate the privileges through, for example,
such a menu like
this.
And then I could just click authenticate.
And if I do this now multiple times, we can see we are now still able to list the
contents to do the
same here in our terminal.
We can also just go into the Home Directory folder, CD slash home, then we can do
an LS.
We can see those are the three users here.
This comes from the way I installed it here.

Probably this user doesn't exist on your system, but here this user, for example, if I
try to.
Change my directory into this folder.
We can see here we are not allowed to even change our working directory into that
folder because again,
we are this user here.
This is my username.
My shell is luckily configured in a way that it shows me my current, my username
here and we are not
allowed to access another user's home directory.
But if I would put a sudo in front then I could just do this.
Wait, I need an before and then I would be able to list the contents of my of the user
yana's work.
And as we can see, those contents are empty.

But if I would for example, use minus a here to show all files, then we can see there
indeed already
quite a few files in that home directory already.

So as you can see, sudo allows you to temporarily elevate your privileges as long as
your user is allowed
to do this.
And in that case, the rest of this command is being executed with additional
privileges or with root
or superuser privileges.
But you need to be careful with sudo.
You always need to make sure that you understand what a command does.
I mean, this applies to, in general to bash commands, but especially whenever a sudo
is involved.
So for example, important here, do not execute this, it will destroy your system.
This, for example, would remove a whole folder and it won't even ask for
confirmation there.
So if we would run this one, we would destroy our whole system.
Luckily here I'm in a virtual machine, so I don't need to worry about that.
And I have another spare virtual machine set up already.
It's ready for me to use.
By the way, I'm just showing this here on the CentOS, but I could also have used
ubuntu for this.
It doesn't really matter, so I will now just execute this command m minus r f of the
slash etsy folder.
I will now just confirm this.

The command has now been executed and if I now would for example try to reboot
my system, restart it,
let's just see what happens and then probably the boot procedure will fail.
So let's now wait until my system here is rebooting and there we go.
It's booting up the kernel has booted.
It's now trying to hand control over to the normal operating system to continue
boot.
We are getting a lot of failed messages here and yeah, I think my system here is
broken now.
Luckily it's a virtual machine, so I can just go back to my slides here and everything
works.
But if this was a real system, then it would be quite a bit of effort to repair this
damage.
So in general, you need to be careful what you execute on the command line, but be
especially careful
whenever a sudo is involved.
Optional [Troubleshooting]: When `sudo` does not work
Let's now have a quick look at what to do if Sudo doesn't work.
If sudo doesn't work, then maybe the system is configured in a way that your default regular
user does
not have this additional options to use sudo.
By the way, even a regular user that can use sudo is still not a super user.
It can just temporarily gain additional privileges by using Sudo and repeating the same
password that
the regular user used for the log in.
So if Sudo doesn't work, you must turn the user into a regular user.
But with administrative rights sudo will in that case not work properly and we might need to
then create
a new user and give that user additional privileges.

Let's just have a quick look at this.


And for this I have configured my CentOS machine here to have a regular user, but this
regular user
here does not have this administrative option.
We can see this if I now go here into the settings here, this is now my user here.
It doesn't show that this user does not have administrative privileges or temporarily the ability
to
temporarily elevate the privileges.
But we can see this if we click on unlock here, we can see here.
It's not asking for my from my regular user.
The password.
No, it's asking for the password of the administrator here and this would be the super user or
the
root user.
So during install we specified a password for my normal user, the regular user that would be
Yanase
Xamon, that would be my name.
And I also specified a password for the root user, the super user.
And here we can see it's not providing my name or not writing my name here, but it's writing
administrator
here.
So it's now asking for the password of the root user.
If my regular user meaning the user account Yanase Zaman had the permission to use sudo,
then it would
ask to repeat my normal my regular user's password here again.
But here in this case it's asking for the super user for the root password here.
So here in this case I will now enter the password of the user root.
And if I now click authenticate, we can see here, this is now here, this, this user here.
Unfortunately, I'm not able to change this user here directly.
I mean, I could do it on the terminal, but the reason I'm not doing this there is that there's a
separate
chapter just about user management.
And there we will have a way more in-depth look at how everything works here.
So I will now just add an additional user and I will call this Yana's admin as the full name.
And here it's important to select the account type to administrator here.
And then I can also set a password here.
Now already I can now just confirm this.
Oh, it doesn't allow a simple password here.
This is just the configuration here of my CentOS system that it doesn't allow simple
passwords.
The way to fix it is that I select a more complex password.
Asdf.
Four, three, two, one.
Let's see if this one works.
It's still too common here.
I need to come up with an actual password.
And there we go.
It didn't had those strict password requirements during the install procedure.
But if we later want to add a user, the way it's configured here is that it then enforces quite a
strict
password policy.
I can now just press add here and this user is now an administrator here of the system.
That means I can now go to Poweroff and log out.
I can now log out and I can now switch to the different user here.
It's now unfortunately a little bit small, but I can here now just enter my password again.
And if I now log in and I now open a terminal, I made everything a little bit bigger here.
And also the font, I've configured that from the terminal.
But aside from that, I haven't changed anything here.
If I would now go, for example, into the home folder, we could see there's this original user
here
and this is now my user's home folder here.
We can also, by the way.
Disable the colors here.
And if I would now try to go or list the contents of this folder.
Color equals never of this folder.
Here we can see we do get a permission denied because this user is still a regular user.

It's not allowed to access another user's home directory.


However, once I put a pseudo in front and I press enter, then we can see here that if I were to
provide
the correct password here that then I would be allowed to list another user's home directory.
So if for whatever reason during install your user did not gain administrative privileges, then
you
could just create a new user.
And this should work on Ubuntu as well as here on CentOS.
If we by the way, now go back to our users menu here.
We could now also unlock this menu again.
Now we can see here that it's not asking for administrator here, not for the password of the
root user,
but the password of this, of my regular user here, because this is now also just using a
something
like a sudo under the hood.

So I just need my regular user's password here, not my super user's password.
And now I could, by the way, also go back and turn my original user account into an
administrator,
and then this user would now also have pseudo permissions here.
And now I could log out again, log back in as this user, and then everything should work
here.
So this is how you can deal with pseudo not working.
And in case anything there wasn't configured correctly on your system.
What is Package Management and how does it work?
Let's now have a look at the concept of package management, which will allow us to install
software
on our system.
But here in this lecture we will only have a look at the concept and then in the next lectures
we will
then have a look at how this concept is implemented in Ubuntu and CentOS.
The implementation, they are slightly different, so I needed to split this a little bit, but let's
have a look at package management in general.
Package management is a process that's included in most Linux distributions.
They offer a centralized way to install and update the software that's installed.
This process is called package management.
This is an enormous benefit of many Linux systems because it really helps us to keep our
system up to
date.
Also, a lot of applications no longer need to include their own updater.
So for example, Firefox or Chrome on Linux usually doesn't update itself.
It's managed by our Linux distribution, by a centralized package management system.
How does it work?
The way it works is that our system connects to centralized repositories and they provide a
list of
available packages.
So it's like, Hey, these are the tools that I got available for install in those versions and those
are their dependencies.
And this, this list can then be used to install updates or to install additional tools.
So our system, our computer downloads a list of all the available packages.
And then it's like, Oh, we are trying to install this specific software.
Then I also need to install all of this other software in order to run it.
And then our package management software can calculate which tools need to be installed to
make this
everything running here on our system.
And this works extremely seamlessly and it also works with most applications that we want
to install.
Even things like Firefox or Chrome might be available through such a centralized system.

However, package management is something that's slightly different for each Linux
distribution.
The main idea, however, remains the same.
This is what I presented you here in this lecture, but the implementations are slightly
different.
Thus I needed to split this and in the next lecture we'll have a look at how it works on
Ubuntu.
And after that we will have a look at how it works on CentOS.

Updating and Installing Software in Ubuntu: apt


We now want to have a look at how we can manage and install software on Ubuntu systems.
And this lecture will be really cool because at the end of this lecture we will see how we can
install
a tool that allows you to create cool art.
But let's first get started with apt.
Why apt?
Well on Debian based distributions, ubuntu is one of them.
We can use the tool apt to keep our system up to date and to manage our software.
It provides several commands for us.
The first one that I would like to introduce you to is apt update.
This refreshes the list of available packages and we should run this before doing anything
else with
APT.

So if we want to do this we can just say apt update.


This will not work.
But I want to show you what happens now because what we can see here is that we are
getting a permission
denied error.

The reason is that as a normal user, we do not have the rights in order to change the software
here
from our system.
This is why we are getting an error.
Permission denied because APT is trying to access certain files here and it is not allowed to
do this.
To solve this, we need to get additional privileges, at least temporarily.
And luckily we had seen that before.
That was our sudo command.
So I can just say sudo apt update and if I now retype my users password here of my ubuntu
virtual machine,
then I can hit enter.

And now we can see how it's now downloading the updates and we can see here there are a
few updates
that can be installed, but this is now something we need to see how that one works.

If we have updated this list, this doesn't update our software yet, it just updates the list of
which
packages are available and in which version and where they can be downloaded from.
So if we now want to execute the upgrade, we have two different options for that.
The first one is apt upgrade.
This command runs a small upgrade of our system.
Small here means that existing packages are upgraded and when using APT, it also allows the
install
of additional packages.
Here.
In this case we are using APT because the command here is apt upgrade.
So in this case existing packages will be upgraded and also if it's necessary, additional
dependencies
will be installed.
Why there's this command here and when using APT, this will be more clear in on the next
slide.

Let's see what this upgrade does.

If I do an apt upgrade here and I just confirm this, then I got quite lucky because we can see
here
what's happening here and we can see here that it wants to upgrade quite a few packages.
So let's first go through the list here of the following packages will be upgraded here.
We can see, for example, that those packages here, it wants to upgrade them to a more recent
version.

But in order to install the updated version here, it needs to install additional new packages
that
haven't been installed here.
The reason is that those all those packages have so-called dependencies.
So for example, this package, if we install this in a more recent version, it will probably then
trigger the install of one of those packages here.
And those package names here just happen to also include a version number here at the end.
So if we are trying to upgrade this to a more recent version, then the more recent version will
be
like, Hey, but we also need to install this additional package.
So this is why it's here saying that a few new additional packages will be installed here on my
system.
This is completely normal.
This is part of an upgrade procedure.
So this is something that I can just confirm with Y.
And then all of those upgrades are being downloaded and after that installed.
So we can now just wait a little bit and then we can see here the progress.
It will take a while, but then everything should work.
One thing to note here, depending on how or what's the state of your system is, it might be
that there
are no more updates to install, and in that case it will not say anything.
But here I got lucky.
There were packets that were upgraded and in order to facilitate this upgrade, it even needed
to install
additional packages.
By the way, in case this procedure is showing you any messages, be sure to read those here.

For example, we can see that it's trying to upgrade our kernel.
That was our core of the operating system.
And here it suggests us to reboot the system, which is something that I will now do because it
suggested that.
So I will now just hit restart here and restart my system.
In the meantime, I go back to my slides and there's one more way to upgrade the software and
that would
be an apt full dash upgrade or apt dist upgrade its equivalent.
This runs a large upgrade of our system and large means that it tries to update or upgrade
existing
packages, but it's also allowed to remove packages if they are no longer required and if they
would
cause problems otherwise.
So for example, there are tools that can be like, Hey, I can only be installed if another tool is
not installed.

And if we wanted, for example, to upgrade something to a more recent version and suddenly
during this
upgrade procedure, the new version would be like, Hey, this other package needs to be
removed.
This would not happen during a normal apt upgrade.
This would only happen during an APT full upgrade.
And this allows or this then pretty much means that it runs a larger upgrade of our system.
Don't worry, nothing that you installed manually will ever be removed, but maybe something
that has
been installed automatically as a so-called dependency, meaning you installed something and
then it
was like, Oh, but I need this tool as well.
I need this tool as well.
One of those additional automatically installed tools might get removed during an APT full
upgrade.
There will be a separate chapter about Ubuntu and the package management there, so we will
not go too
much into the details here, but this pretty much just means that we want to run a large
upgrade of
our system.
My system is back up and I can now try to see if something happens here.

Sudo apt full upgrade and if I now execute this, we can see here it's trying to actually remove
now
older versions that are no longer needed here.
And yeah, this was not allowed during the normal upgrade.
For this we need a full upgrade.
What you what's your takeaway here?
Well, this is just a small upgrade.
Usually this should never destroy anything, but a full upgrade could theoretically break
something.
So only run this if you have enough time to potentially debug potential problems there.
So this is how we can keep our system up to date.
But how do we install or remove software?
And for that we can just use an apt install and then we can just specify the name of a package.
This will then install an additional package on our system.
And for example, we could do that with a program called Cowsay.
Let's just have a look at this.

So here I am, My update or my full upgrade here is complete and I can now just say sudo apt
install
cowsay and if I now execute this we can see here it has now installed this program.
If this program wanted to install anything, additionally, we might have had to confirm this
with a
y, but here in this case it was only the cowsay program, so it continued with the install
immediately.
Now we have an additional program available here and I could now for example say cowsay
hello from Ubuntu
and if I now execute this we can see we are now able to generate cool script here.
Before we installed this program we did not have this possibility.

Um, we can also remove a package again this through apt remove and this will just remove
this package.

Then let's also have a look at this.


So I go back here sudo apt remove cowsay and now this program will be removed if I
confirm this.

And now if I would try to go back here and now try to execute this program again, we could
see that
this program here now no longer exists.
By the way, just for troubleshooting purposes in case there's an error during an upgrade or
full upgrade
and it doesn't want to upgrade your system, you can try APT autoremove.

And this removes packages that are no longer needed and sometimes this solves potential
problems.
Again, there will be a separate chapter about package management where we will have a
more in-depth
look at this, but potentially sudo apt autoremove can or does remove packages that are not
required
anymore.
And here in this case it would want to remove this one and this could prevent potential
problems or
errors that happen when, for example, an upgrade refuses to install.

In a sense that APT can't figure out how to install this upgrade.
And then sometimes an apt autoremove solves those issues.
Again, we will have a more in-depth look at this later.
But sometimes problems might happen and I want to prepare you for this.
Um, important here.
There are additional commands for APT that we will have a look at later.
Also a quick heads up, there's another implementation of the apt tool that's the apt minus get
tool.
And sometimes in this course I might use it as well.
And then I could for example say apt minus get update and it works with the same files, but
it's a different implementation, but you can use it interchangeably.
So you could use an apt get update with an apt upgrade and it will work seamlessly.
Just be aware that the upgrade of APT minus get does not install additional dependencies,
whereas the upgrade of the APT tool does install additional dependencies.
So for example here sudo apt get upgrade.
This would only upgrade packages, whereas this one here would upgrade packages and install
potential additional dependencies on the system.
So those two commands are just slightly different.
So I had to add this little caveat before on our slide.
Nonetheless, you now know how to keep your system up to date and you also know how you
can now use this knowledge to install additional software here on your system.
Updating and Installing Software in CentOS Stream: dnf
Let us now have a look at how we can install software on CentOS.
And this will be really cool because at the end of this lecture you will be able to generate a
cool ASCII art like this.

So let's have a look at how we can get started.


And for this, let's first have a look at how we can keep our system up to date.
And the thing is that on Red Hat enterprise related distributions, which central stream is one
of them,
we can use the tool DNF to keep our system up to date.
With just one command, we can install all available updates and that's just the DNF upgrade
or DNF
update.
This fetches the latest version of the package list and also upgrades our system.
Why do we have two different commands here?
Well, they they are just an alias for for each other.
So DNF upgrade is the original command.
But if we type in DNF update, it will just execute exactly the same thing.
So in both cases we are fetching the latest version of the package lists and we are upgrading
our system.
Important DNF always keeps the local package list up to date and this is in stark contrast to
Ubuntu
here.
We never have to manually refresh the package lists.
DNF takes care of that.

Let's now just have a look at this and for this I'm here on my CentOS and I will now just open
a terminal
here and I will now say sudo DNF update or upgrade to install all available upgrades.
Then I enter my password and confirm this with Enter and we can see here it wants to
download and update
quite a few tools here.

We can see this is actually quite a bit that it now wants to upgrade here.
This is all right here in my case.
I will just confirm this with a y.
Be aware that it might be that if you execute this on your system, there are no updates shown
here
or even more here.
So this really depends on whether your system is up to date or not.
But here for me there are quite a few upgrades, so I will now just confirm this with a Y and
now we
need to wait a little bit until all the packages have been downloaded and installed.
We can see here it's now starting to download those packages and yeah, here, this will now
take a
little while.
By the way, in case DNF asks you to import a GPG key, this is completely all right.

You can confirm this with a y it's doing this.


Is that a lot of or those packages they are signed that they come from the official repositories
and
if this was a production repository we should then of course here confirm the fingerprint,
whether
this whether everything is all right here.
But for us, this is just a virtual machine.

So I will now just confirm this here with a Y.


Now I can just wait until everything is installed and we can see here it takes a while.
But now all of those packages are upgraded.
By the way, here.
In this case, a package called kernel is also being upgraded.
We can see this if I scroll up here again that we can see a package here that's just called
kernel or
at least kernel tools.
And if we keep scrolling up, we can also see the package kernel is being installed here.

And whenever something like this happens, it's best to do a reboot after install.
So now it's installing all of those available updates, but it's then up to us to trigger the reboot.
But here in this case I would really recommend a reboot.
If it's just updating normal stuff, you might get away without a reboot, but still it's best to do
a reboot just to be sure.
We can see here that it's taking quite a bit.
So this is now maybe 1 or 2 minutes later and it's still running.
This is completely normal.
The way it works is that it's downloading those packages and installing them, and some of
them contain
scripts that need to be executed and that need to run on your computer and they can take quite
a while.
So really just wait until everything is finished here.
Now we can see that everything is complete.
So I can now do the reboot and I will just hit restart here.
I will confirm this and I will go back to my slides.
In the meantime, let's now have a look at how we can install or remove software on CentOS.
And for that we can use either DNF install to install software and this will install additional
packages
on our system.
As an example, DNF install minus release will, for example, install this package on our
system or
DNF install Cowsay will look for a package with a name Cowsay and it will install that on
our system.
By the way, why I've chosen those two examples here will be more clear in just a second.
Let's first have a look though how we can remove software again.
And this works through DNF remove and then the name of the package.
And this will then just remove a package from our system.
So this is how we can install or remove a package.
Let's now try to just install cowsay here on our system.
So here I am.
I do need to log in again on my system here and wait a second until everything has loaded
and now I
can just launch the terminal here.
I need to trigger a resize so the resolution refreshes here.
In my case, I just had to quit fullscreen and enable fullscreen again.
And now the resolution has been adjusted.
Be aware this depends on your virtualization software.
Not all of them support this.
Nonetheless, it looks better now.
So if I now would do try for example DNF install cowsay.
Then we can see here.

I do need to enter my password again that it will not be able to install this package here on
my system.
So we can see here this one here.
It's now trying to update, do all the updates, updating the new package lists.
So this can also take a little bit, but we will see that this will eventually fail.
We can see here it's unable to find a match for this.

And the reason behind that one is that we need to install minus release on our system.
This are additional package lists that we need to install and they are required in order to
install
additional tools on our system.
So we can imagine it like this.

Our system connects to centralized repositories and to see which software is available and
certain packages
are behind another repository called minus release.
And in order to install this one, we can first update our system, then we can install our
release.
And then we should just do an update of our system.
Again, just to be sure, technically the first DNF update, I'm not entirely sure if we really need
it, but to prevent any potential problems, we can just run this one here.
Of course we need a pseudo in front of this because we want additional privileges here
because otherwise
we could not install or remove software here.
But I on on those slides.
Um, to simplify how the slides look, I quite often omit the pseudo here.
Nonetheless, just to show you this, by the way, here we do get a message that tells us what to
do.
Nonetheless, let's keep going.
So sudo dnf install epel minus, release those.

Or this installs additional repositories, so additional servers that we can pull packages from.
And we can see here it's not just installing this one, but it also has a dependency meaning.
It also adds another thing here called minus, next, minus release.
So this is totally all right.
Both of those tools will be installed.
This is package management doing its thing, analyzing that one package needs another
package and then
both will be installed.
So I will just confirm this with a Y, and now I need to wait a little bit until those two have
been
downloaded and installed.

Um, after that, I do need to now also run a DNF update.

But wait, we need to confirm the GPG key with a y first and now everything has worked
here.
The next thing that I then also should run is a DNF update to just ensure that my system is up
to date.
Sometimes it wants to install something there, sometimes everything is all right, so we can
just wait
until this one here has worked and yeah, actually let's just see what happens.
Now we can see here now, by the way, that it's now downloading additional package lists and
I'm not
sure, but the download seems to take relatively long.
But this is all right.
We can just wait until this download is completed.
There's one more thing that we should be aware of.
If we scroll up here a little bit, you can see here a message that has been shown during install.
Many packages require some additional repository and we should run a specific command to
enable this.
We don't need to understand this now because there will be a separate chapter about how
package management
works, but there's one more command that we should also run and that is just CRB space
enable to fully
enable that we can utilize those additional package lists.

So if we now go back here, we still need to wait here for this update.
Okay.
Then after this has finished, we can see here now nothing needed to happen in my case.
I should do my crb enable here sudo space.
CRB enable confirm this with a y and now everything here is prepared properly.

Now we can do yet another DNF update and probably this will now take a little while again.
But just to be sure we do yet another update so everything gets refreshed and potential
updates get
installed and then we can install cowsay.
So now I can say sudo DNF install cowsay and we can see that now it can find this program
and we can
now finally install it.

The reason is that this comes from this additional repository, this additional source of
packages that
we have installed through this relatively long procedure.
But this procedure also has been necessary actually for quite a lot of other tools that we will
install
during this course.
And this is why it's important that we do enable this.
And we do go through all of the procedure.
We are not just doing this here for Cowsay.

I will now confirm this with a Y because it wants to import yet another GPG key.
This is totally all right.
And now this tool has been installed.
If I now want to use this tool, I will now just issue a clear command first and then I can say
Cowsay
hello from send OS stream and I can now confirm this by pressing the enter key and then we
can see we
are now able to generate cool Ascii art here from our CentOS system there now just a few
heads ups that
I want still want to give you.
The first one is that there are additional commands for DNF.
I'm fully aware of that.

We will have a more in-depth look at package management on CentOS in a separate chapter.
And we will talk more than an hour about package management, how you deal with problems
there, how
you solve problems, how you identify them, how you manage this, What why is this had to be
something
that we installed separately.
So this is topic in a different chapter.
For now, it's just important that we get going.
Also, another quick heads up.
Maybe you've used that one before.
So I feel like mentioning this Yum used to be a package manager on CentOS previously and
nowadays Yum
is being replaced by DNF.
Nonetheless, if you've worked with Yum previously, those commands still work perfectly.
Yum.
Install cowsay would also install cowsay.
The way it works is that under the hood it will still call DNF.
In that case here, which is the more the modern implementation for package management on
CentOS or
in the Red Hat family.
But nonetheless the older command still works.
So if you find this in tutorials and people still use this, this is totally all right.
It's just the previous syntax, but the previous syntax is still supported.
So just a quick heads up in case you find people still using Yum.
This is also totally all right for now.
All right.
Now you know how you can keep your central system up to date?
We have also configured everything that during the course of this course, you can install
additional
tools and also install all the additional tools that we need here for this course.
And yeah, you are now able to manage the software on your system.

File management(Part one) – organize Files and


Directories
Intro
Welcome to this chapter about file management.
In this chapter, you will learn how you can create folders and also files in Bash.
You will learn how you can copy and move files and also rename that It's actually the same
command.
Then what we would also use for moving files and you will learn how you can delete files
and especially
how dangerous bash can be and how you can mitigate that.
This can prevent you from a lot of hassle because you know, when you have to be really
careful when
you're using Bash.
In addition, you will solve some real world problems.
So, for example, you will extract photos from a folder structure that could be useful.
For example, if you have an SD card and you want to extract all the photos from that one and
you will
extract all excellent PDF files from a nested folder structure with a more specific matching
there.
So this chapter is really, really interesting and you will learn everything you need to know
about
file management in Bash.
So let's now get started and let's start with our lectures.
Creating Files and Directories: the Commands `touch` &
`mkdir`
In this lecture, I want to introduce you to the important commands.
Touch and mkdir.
Let's have a look at how they work.
So the first command is the touch command.
This one allows you or the typical use case for that one is that it allows us to create an empty
file
or even multiple files.

Let's just have a look at how it works.


So, for example, right now I'm in a folder here.
I've navigated to that folder already with change directory, with the CD command, and right
now this
folder here is completely empty.
Let's say I want to create some letters because I want to invite my friends.
Then I could just create those files for the invitations.
So, for example, I could create one file here.
And if I now list the contents of my folder, we can see that this file has been created here.
Touch also allows us to create multiple files at the same time.
So for example, here I could create multiple files, for example, like this and if I now hit enter
and I now list the contents, you can see that those two files have been created now.
So as you can see here, the touch command allows us to create multiple files at the same time
as well.

But why is it called touch and not create if we are using it to create files?
Well, the actual use case for touch is a little bit different.
Different?
It still includes that we can create an empty file and this is what we usually use touch for.
But the actual use case for touch, what it actually does is that it modifies the timestamp of a
file.
So it checks whether the file exists and if the file exists, its timestamp is updated to the
current
date, and otherwise a new and empty file is created, of course with the current timestamp.

So let's see how we can check those timestamps.


And luckily this is built in already into the LS command.
We just need to enable this output by adding a flag.
So we say ls space and then minus and then lowercase l and this tells the ls command that we
are not
just interested in the files files, but also at the timestamps and additional information.

We will have a look at all this additional information later in this course.
But for now, this is listing our files.
So as you can see, this is the time when I had created those files initially.
Actually, it's not the creation date, but it's the last modified date that's being outputted here.
However, in this case it's the same because I've only created those files.
But let's just see what touch actually does.
So here you can see I'm touching this file again, and if I now hit enter and I now run LS
minus lowercase
L again, you can see that this timestamp here has been updated now and it's now the current
date,
the current timestamp again, like two minutes later.
So you can see that touch not just creates files but also updates the timestamp.
This was the first command.
The next command is the mkdir command.
This command allows you to create a new directory or a new folder.
So let's have a look at how that one works.

So let's say I'm in my folder here.


I just enter clear here to clear my screen and then I output the contents again.
Let's say I want to create a folder because I want to collect everything that's ready, for
example,
because I want to print it.
Then I could create a new folder with the name, for example, ready, and then I could hit
enter.
And now this folder has been created.
If I now list the contents of my folder again, you can see here that we now have three files
and this
one folder, well, we can't really see whether it's a file or a folder on my system here.
It's displayed in the same way.

There are ways to get this into color.


On some systems, it's in color by default.
If we want to enable colors, otherwise we might have to add minus minus color here and then
hit enter
and then we get a colorful output and then we can see that this one here is a folder and those
three
here are files.
Please note that depending on your terminal, it may also not work, but I would say 90% plus
of the
cases.
This one should display colors.
If it doesn't work, you might need to change the app you're using for your terminal and install
and try out another one.
But usually, again, it should work.
We were now able to create those files and we are we were able to create this folder, but now
how could
we, for example, move those files into that folder?
Moving and Copying Files: the Commands `mv` & `cp`
Let's now have a look at how we can move files and copy files.
So the first command is the move command.
That one allows you to move an existing file to another location, but you can also use it to
rename an existing file and it can even do both at once.
Let's just have a look at how everything works.
So I go back into my terminal and let's say I'm here again in my folder.

So we have a few invitations and we want to move all of them into the ready folder.
Let's start with the first one.
So that would be that we want to move the endy.txt file into the ready folder so I could just
write this one, then I could hit enter.
And now this file has been moved and if I now output this you can see this file is gone.
I could now that would be the one way I could use change directory to change into my ready
folder and then I could list the contents of my ready folder.
So right now I'm in my ready folder.
If I now output the contents, we can see that the file is there and I could now go back one
level up again.
So I'm back in my previous folder.
You can see here, I'm back in my previous folder so you can see we have now confirmed that
we have
actually moved that file.
However, you have seen this was a bit tedious in the sense that we had to navigate into that
folder,
use this command, move, uh, navigate back.
So we needed quite a few commands just to look into our ready folder.
The easier way would be that if I'm in one folder and I want to for example, look into this
folder here, I could just use LS with an argument and I would just enter the folder name that I
want to look into.
So for example, here it would be the ready folder.
This doesn't change my current folder, it just tells Bash or the LS command that we don't
want to look into our current folder, but that we want to look into this folder here.
So we are now looking into the ready folder.
We can see my file has moved, but I'm still at my previous folder.
We can also confirm this and this is the path, for example, of my of my current directory that
I'm
in right now.
So you can see we have successfully moved one file.
Let's have another look and let's have a look at how we can move or how we can rename a
file.
So let me just clear my screen.
So let's say here, for example, we have realized, okay, Max is has a different name.
For example, he could be called Maximilian and we just made a mistake here.
So let's correct this mistake.
So I say move max dot txt and now I instead of moving that file into a folder, I can just
provide a
new file name.
So for example, I could provide Maximilian dot txt and now my file name has changed.
So if I now list the contents here again you can see that I've renamed that file.
I could also do both.
By the way, if I now realize okay, I want to move the Maximilian dot txt file into my ready
folder
and rename it at the same time.
For example, back to max dot txt.
I could also do this here with one command.
So this one would take this file and move it into this folder.
Into this file.
So this would be now a move and rename in one command.
And if I now hit enter we can see this file has moved into the ready folder and if I now output
my ready
folder, you can see the file is called Max again.
All right.
So this is how we can move files.

Let's now have a look at how we can copy files.


And for copying files we can use the CP command.
And this allows us to create a copy of a file.
If we just want to copy a file, we can just use the CP command as you.
We've seen it with the Move command before, but if we want to copy a whole folder, we
must provide
the option minus uppercase R, so a whole folder is being copied.
Y r r stands for recursive, and this means that we want to go into the folder and copy the
whole folder.

Let's have a look at how that one works.


So for example here, let's say we want to copy a file.
So for example, the invitation to Laura.
We want to copy, for example, because we want to invite one more person here so I could
just copy
that invitation.
And if I now output the contents of my directory, you can see that we have now just copied
that file.
Of course we could also copy and rename at the same time.
So for example, I could copy the Laura dot txt file into my ready folder and for example, this
would
now copy that into that folder.
But if I keep writing here or I could also provide a file name here.
So for example, let's say I spelled that name incorrectly and I want to copy that file into that
folder
and I want to have it provided as Lauren dot txt.
Then I could say or then I could enter this command.
And now if I look into my ready folder, we can see that the Laura dot txt has been copied to
ready
slash Lauren dot txt.
So you can see how.
Also with copying we can do multiple things at the same time.
Let's now also copy the dot txt file into our ready folder.
So I could just say eva dot txt into my ready folder and I hit enter and we can see now if we
look into
our ready folder that those are the invitations that we have written.
Now let's say we have all those invitations and we want to create a backup.
So we want to copy the whole folder.
If I now try to copy a folder.
So for example, here I create a new folder or I want to copy this folder to this folder.
This one will be then created by the way, best practice to avoid whitespace in file names.
They then usually bash.
It's a little bit more difficult to to address those file names.
If I need a whitespace, I need to wrap everything in quotes and it's a bit more tedious.
So it's actually best practice to avoid those white spaces here in folder names.
So that's why my folder is called Ready Underscore Buck.
So let's see, I want to copy my folder already to that folder.
If I now hit enter we can see ready is a directory and it has not been copied.
We can also see it has not even been created here.
So copy command or the CP command hasn't even touched or cleared it.
So let's see how we do that.
Um, I now need to repeat my this command and I need to add the flag minus uppercase R
Luckily I don't
have to type everything again.
I can use the arrow up key to navigate back to my previous command and I can go through
that list of
my previous commands.
So I go here and then I use the left arrow key to navigate to the correct position.
And I just add the flag minus uppercase R, This tells the copy command that it should work
recursively,
so it should copy a whole folder.
And now we can see that a whole folder has been copied and we have a new folder here
called Ready Underscore
Buck.

So this is how you can move files and how you can copy files and also how you can copy
directories.
However, we what happens if we want to delete files?
So for example, let's say we want to delete our folder here.
Ready underscore buck because we no longer need the backup or we want to delete this file
because we
have copied that into the ready folder already and we don't need the original anymore.
How do we do this?
And how you delete files and how you can delete folders and everything like that we will
look into in
the next lecture.
Deleting Files and Directories: the Commands `rm` &
`rmdir`
Let's now have a look at how you can delete files and folders.
So if you want to delete a file, you can use the remove command written to remove a file or
multiple files at once.
For deleting a directory, we also need to pass in an option that's here.
The option minus lowercase r, and this tells the command that we want to recursively delete
the folder.
Um.
This then deletes empty and also non-empty directories.
Let's have a look at how the command works.
So let's say here I'm in my folder here and let's say all my invitations that I had written in my,
in this chapter before, they have already been moved or copied into this folder here.
So I don't need those invitations here anymore and I want to remove them.
How do I do this?
I can use the command to delete this file here, for example.
Or I can use also the command to delete multiple files at once by just providing multiple file
names
here and now those two files will be deleted with just one command.
If I now hit enter, we can see here everything has worked properly and those files have been
removed.
We need to be really, really careful here.
This command is extremely dangerous.
Let me show you an example.
So, for example, on my desktop, I just now navigate to my desktop and then I have a folder
there
called Presentation.
So this one here stands for my home folder.
Then I go into my desktop folder and then there I go into my presentation folder.
And if I would now for example, list the contents.
We can see that we got some PowerPoint slides here.
If I now, for example, would delete this PowerPoint slides with the command, I would just hit
enter.
Now this presentation is gone.
Oops, this was the slides from my presentation.
No worries.

Let's look at how we can restore them.


So I just opened my bin and we can see.
As we can see, we can see nothing.
This is because this command has actually deleted this file permanently.
So I'm very sorry to tell you that there will no longer be any slides here in this course.
Now, don't worry.
I got a backup.
This was intentional here.
But please be really, really careful when you move, when you use the command, because this
will permanently
delete those files, usually without any confirmation, depending on your configuration.
So this is extremely dangerous and you really need to be careful here.
This then also explains the behavior why we need some special parameters to be allowed to
remove a folder.
Let me go back to my previous folder that was placed on my desktop.
And then in the folder called Folder Management, I use tab to just autocomplete all those
paths.
So I don't need to type everything completely myself.
I move back or I change my directory to that folder and then we can see I'm now back in my
folder here
with the files we had worked with previously.
I enter clear to just clear my screen here.
So now we have a folder or we are in a folder where we have this folder and we have those
backups here.
If I try to remove a folder here, for example, the ready underscore Bak folder, then we can
see that
the command will not delete it because it's a directory.
This is because there are, as we've seen before, can be really dangerous and permanently
delete our
files and it's worse if it's deleting just a few files, but it's even worse if it would just remove
all the folders in the folder structure.
So this is why we need to enable that can actually delete folders.
So here in this case, for example, we could say RM minus and then we provide the flag R
here and this
tells the command that we want to this to be applied recursively.
So now this folder was deleted and we can see here all those or all my backup is gone now
and we only
have this one ready folder here.
This gives us a nice reason to look into another command.

The other command is the Rmdir command.


This stands for remove directory and it only deletes empty directories.
You can imagine that this can be really, really useful because it might prevent some serious
data loss.
So whenever you try to delete an empty folder, please use this command.

Let's see why.


So for example here, let's say we have this ready folder, but there are files inside so we can
see this.
If we list the contents of that folder, there are quite a few files inside and we really want to
keep them.
Let's say we would try to remove this one, then we would see here it's a directory.
And if I would now for example, accidentally type in this one, it would permanently delete all
my
invitations and right now I've already deleted my backup.
So this would actually be serious data loss if I would execute this command.
However, this command here is way safer if there are files in that folder, it will just tell us
that
this directory is not empty, but it will not actually delete this directory.
So you can imagine this can prevent serious data loss.
I want to tell you about a few caveats though.
So imagine we have a folder here, let's call it for example images, mkdir images and we have
a file
in that folder.
So for example, we have a file in our images folder.
I use the touch command and I tell touch in our images folder I want to create a file.
So for example, I want to create a file that should be hidden.
So the file name starts with a dot and I call that file Thumbs.db.
Then this file will not be created in my current folder, but in the images folder.
And this is the file name.
And because it starts with a dot, it's a hidden file.
So I just create that one.
And if I now list the images folder, we can see that it looks that it's empty because by default
it
doesn't show hidden files.
However, Rmdir will not delete that folder because of course it's not hidden because there's a
hidden
file in that images folder.
If I want to list the hidden files as well, I can use LS minus a and then we can see that there's
actually
this dot thumbs.db file in my images folder.
So just be wary or just be aware that if there are hidden files in a folder, they also count as
files
and Rmdir will then not work on that one.
By the way, the other output here, this is the dot just stands for the current directory.
So this would stand for the images directory because we've printed that one and this one
would stand
that we want to go one level up.
But those are not actually folders.
It's just something that minus a prints.
This is the only file in our folder images that exists here.
So just be aware that whenever you use Rmdir, the folder must be completely empty,
including empty
of hidden files.
So how could we work right now?
The way to go would be.
For example, I would first delete the dot thumbs.db file and then I could use the Rmdir
command to
delete the images folder.
And now you can see that I've combined those two to delete it and this would be a little bit
safer than
the minus M minus R because it's more verbose or it's more explicit in a sense that we really
make,
we really declare which files we want to lead or delete or which directories we want to be
removed.
All right.
This is how you delete files.
You now know a lot about basic file management, so I think it's now time for a short quiz and
then
we'll have a short exercise for you and then we'll continue here with this chapter.
Exercise Intro: Website File Management
In the next lecture, I will give you a text lecture for an exercise about file management.
And I want to give you a short heads up for this exercise.
This exercise is pretty much a play along to do some file management.
The goal in that exercise is that you have the ability to practice file management commands
for Bash.
The idea or that would be my recommendation, is that you use a file browser to see what each
command
does and how those commands work.
So the idea would be you have a bash open a shell where you type in your commands, but at
the same
time you have the file Explorer or Finder or whatever it's called on your computer, the normal
file
program open.
So you can always look and see what each of those programs do.
I will not do this in my sample solution.
I will just stick to the terminal.
But if you solve this, I would advise you to always have a look at how everything works and
how those
files are being moved and how all of that structure works.
So please don't just execute the commands or translate the instructions that I give you two
commands
also at the same time, have a look at what they do and how they change the files, how they
move,
and really, really try to play along because those file management commands are really
essential when
you are working with Bash.
The outlook is that later exercises in this course will be more practical and will solve more
problems.
And it will not just be a play along, but we really need to practice the basics first.
And that's the reason why we have here this exercise.
But really play along and always have a look at what those commands do.
So let's now switch to the text lecture and let me present you the exercise.

Exercise: Website File Management

We now want to practice your file management skills. Please just follow along these
instructions, where we create the folder and file structure for an imaginary example website.

You don’t need to download any files for this exercise.

1. Navigate to your Desktop making it your new current working directory


a. If you’re using Windows Subsystem for Linux, feel free to navigate to your
actual desktop
2. Create a new directory called tmp_website
3. Inside the tmp_website directory: Create 3 (empty)
files: index.html , style.css and script.js
4. Create a new subdirectory styles and move the file style.css to this new directory
5. Create a new sub-directory scripts
6. Rename the file script.js to index.js and move it to a new sub-
directory scripts (in one command)
7. Create a new (empty) file page1.html in a new sub-directory called pages (you
will have to create this folder first, before creating the file)
8. Copy the file page1.html two times and call the
copies page2.html and page3.html , respectively. Those copies should be in
the pages directory as well
9. Move the file page2.html up one level (in the directory tmp_website)
10. Delete the files index.html , page1.html and page3.html
11. Rename the file page2.html to index.html
12. Delete the (empty) directory pages
13. Delete the (non-empty) directory tmp_website

Solution: Website File Management


Let's now have a look at the solution, how the exercise for file management works.
So the first part of the exercise was that we should navigate to our desktop and make that our
current
working directory.
So I could check before.
Right now my current working directory is my home folder.
I'm on a mac so slash users and I should navigate to my desktop.
So I say here change directory to my desktop, which is a subfolder of my home directory.
Or the other way would be that I could say okay, we start at my home directory and then I
want to navigate
into my desktop.
So this one here would work as well.
I use tab to autocomplete, so I don't need to type everything myself.
It's a little bit faster that way.
So now let's check and you can see here that right now I'm on my desktop.
Now I should create a new directory that's called temp website.
So I create that directory, tmp underscore website and I then should navigate into that folder.
So I say here, navigate to that folder with change directory.
If I now list my current working directory, you can see that right now I'm in that folder here.
The third part was that inside this folder I should create new three files.
I should create the index.html file, the style.css file and the script.js file so I can just touch
them.
index.HTML styles.css and script.js.
So those files are created and we can confirm that with the LS command.
You can see those three files exists here.
We should now create a new sub directory that is called styles and we should then move the
file style.css
into that directory.
So I move the file style.css into my styles directory and yeah, that's the command that I just
need.
This will move this file into my styles directory.
Let's just confirm it.
You can see here the Style.css file is gone, but you can see that we now have a folder called
Styles
here and I could also list that folder here.
And in that folder you can see that we have style dot CSS, we have that file.

The next part, and that's part five right now is that I should create a new sub directory scripts
in
my folder temp website.
Let's just have a look at where we are.
PWD We are still in our temp website folder and in that folder I should create a new folder
called Scripts.
So I just create that one.
I have not changed my current working directory.
I've now just created this one additional folder in this folder here we now have this one more
folder
and now we should rename the script dot JS file to index.js.
But at the same time we should also move it to the sub directory scripts.
So the way to go is that I specify the path here.
So I take this file and I move it and rename it at the same time.
Moving and rename on the Linux command line.
It's exactly the same.
So it's one command here and I can just yeah, do this.
And now you can see my script file has been moved to scripts and you can see my index file
is in my
scripts folder.
The next that's now number seven is that we should create a new empty file called page one
dot HTML
in a new sub directory called pages.
So the first thing we need to do, I just use the clear command here to clear my screen.

So we are still in our temp website folder.


What we now need to do is that we first create a new sub directory called Pages.
And in that folder I now want to create page one dot HTML so I can just touch that file.
And I've not changed my working directory by creating a new folder.
I'm still in my old working directory, so I have to provide the path here and I can just create
the
the HTML file page one dot HTML.
This is now an empty HTML file without any contents.
The file is completely empty.
So this file now exists.
And now I should copy that to page two dot HTML and page three dot HTML respectively.
How do I do this?
Well, I could either copy it for example, like this.
Page one dot HTML to pages.
Page two dot HTML.
That would totally work and it would copy that file.
Or another way would be that I would go into my pages folder and then I would copy the
page one dot
HTML to page three dot HTML.
So that would also work.
And now I've used the combination of those two commands.
One I've copied with the paths, one I've navigated into that folder and then copied that file
again.
Um, now I go back to my temp website directory, so I'm now back here.
And now what I should do is that I move the page page to point one level up.
So the page two was could be found in pages page two dot HTML and I should move that
one level up.
The way to go here is that we say from where to where we want to move that.
And I can just add a dot here to say I want to move this to my current working directory
because right
now I'm in this folder here, so this is my current working directory and I'm moving that page
one level
up.
We can see now here that we have moved this page one level up.
The next task was that we should delete index.html page one and page three dot HTML.
This one here is the first page I should delete and then pages page one dot HTML I should
delete and
the page three dot HTML.
So I'm now just deleting deleting that.

The next task was that I should rename page two dot HTML to index.html.
So I need a move fail here.
Again.
I've now just renamed that one and what I now should do is that I should delete the empty
directory
pages so I can use the rmdir command to delete my pages folder.
And now I should delete the the whole directory here of my whole temp website.
With all those files I should delete the whole directory.
So the way to go is that I go out of that directory.
Now I'm back in my desktop or on my desktop and now I can say minus r recursive, delete
my folder temp
website please.
And I can just hit enter.
And now my folder has been deleted again.
So this was the whole exercise.
It was just a play along with those commands.
But this is really, really important in order to practice those commands because they are
essential
for the whole rest of this course.
I hope you had fun with this.
The next exercises will be more, more practice oriented and less abstract, but this is a nice
first
exercise for the beginning.

File Name Selection by Pattern Matching: Globbing with


`*`
We are now ready to talk about the file name expansion or the so called Globbing in Bash.
This is an example.
You will learn more about the power of Basil during this course, but this is an example about
the power
of Bash and why Bash is so incredibly compact, but still incredibly powerful.
So let's just have a look at how this works.
So the file name expansion is the process in which Bash rewrites our commands before they
are even executed.
Globbing recognizes and expands predefined wildcard characters.
We can imagine those wildcard characters as a way to enable us to search for specific
patterns, and
it will then search for files that match this pattern and expand this command.
Expanding is just another word for rewriting our command.
This allows us to easily access multiple files.
Let's have a look at how this works.

So here I'm in my terminal and in this case we are in a folder where we have a few images,
but we also
have a movie and some additional information about that movie.
Let's say we want to access all those images and move them to a folder.
So I could, for example, create a new folder here that I call images.
And now I would want to move all those images into my images folder.
I could do this by using the Move command and the Move command actually supports that.
We are able to move multiple files at the same time.
It works like this that we first specify all the files that we want to be moved.
So I just specify all those files.
My shell or my terminal here supports that.
I do copy and paste with keyboard shortcuts, and this might be different depending on the
shell you
might use.
Maybe you have to type it manually.
So for example, here I could say, okay, I want to move all those files for example, into my
images
folder and I just hit tab to autocomplete, that one.
And then I hit Enter.
And now all of those three files have been moved.
If I now would look into my current folder, you can see that those images are gone.
And if I now look into my images folder, you can see that those images are in that folder.
You can see this works, but we had to specify this for all of those files individually.
Also, the command now to move everything back is also really long.
So for example, here I would have to list all my images individually.
Right now I'm still in my parent folder, so I have to specify the path here to all of my images
here.
And then I say that I want to move them back into my current folder.
So the last parameter here is my current folder and this tells the move command that all of
those files
that I specified before should be moved to this folder.
If I now hit enter, we can see all those images are back in my original folder.

This was the process without globbing.


Now we want to use Globbing to simplify this.

Let's have a look at that.


We first need a wildcard character and the first wildcard character that I want to introduce
you to
is the star or the asterisk sign.
This allows us to match zero to any number of characters in a file or in a path, and Bash will
expand
this command in that case, or rewrite the command for us.
Let's just have a look at that.
So how can we access all the images and move them into our images folder?
I could say move from my current directory.
Everything that starts like I want to match zero to any number of characters in our file name,
but
at the end it should be written dot jpeg and I want to move that into my images folder and
then I can
just hit enter.
And now I have moved all of my three files.
Imagine if that folder would contain 100 files.
It would be almost impossible to list all of those files manually.
However, this one move command would have done all of that for us.
So this just shows how incredibly efficient Bash is.
But it's better to check on on bash or on on what actually happened.
So you can see here those images have actually been moved into our images folder.
Of course, we can also use this wildcard character, for example, in something like this.
We want those images to be in my images folder and then we want to access all the files there
and we
want to move them back to my current folder here.
You can see that I use this one to match the whole file name.
But of course I could also say that at some point there should be a dot and then there should
be zero
to any number of characters here again, or that the extension should be Jpeg.
So whatever I use here, this will move all the files from this folder back to here.
My current folder, except by the way, hidden files.
This one here by default does not match hidden files.
Hidden files I would need to access like this, but this would move all the files that are in the
images
folder back to my current folder.
We can see now that those images are now back again in my previous folder.
Why did I move them back?
Well, at the end I want those images of course, to be in the images folder, but I moved them
back
because I want to show you something really, really important.
This happens before Bash is executing the command.
Why is this so important?
This is important because this works with all the commands that we can use in Bash.
So, for example, I could also use the Echo command, for example, and provide this one here.
And then Bash sees, okay, there's an asterisk sign here.
Let's just expand or rewrite the command.
And we can see here this has been rewritten to our parameters or to to this one.
What you are seeing here right now and this has just been outputted.
So as you can see here, this one has nothing to do with our move commands or with our copy
commands
or anything like this.
It's a shell feature that happens before our command is being executed.
And in this case, it just expands to all of those files.
So to put it in other words, our Move command doesn't even know that we've been using
globbing here
because our shell, our bash is rewriting the command to provide all the individual file names
here
and it's expanding it.
This just means it's rewriting it.
And then we are moving all of those images into our actual images folder.
And as you can see here now, this has now worked.
All the images are now in the images folder.
If clubbing fails.
So in a sense, for example, there are no images here.
So if I would now try to move this Jpeg to the images folder and this globbing fails, in that
case,
Bash will not process this Globbing And the move command will understand this as if it was
a file name
so it will understand.
Okay.
Star sign dot jpeg as a file name kind of.
And it will tell move to move that file to the images folder.
But you can see here this one now could not work because a file with this file name, of course
doesn't
exist.
So move is now complaining that it could not move this file.
So if Globbing fails in that case, if we are using a bash, then this will just be the file name
and
it will be understood as a normal file name.
So if it can.
If Bash can find images.
Then it's perfect.
This will be expanded and rewritten.
And if it can't find anything here that would match that one, it will just pass.
What we have typed here before to our actual program.
Be aware if you are using other shells.
I don't want to go too much into the details here, but this one for example, would work
different
in the zsh shell if I would run the same command in a zsh shell, even though those two shells
are quite
similar bash and zsh this would be an example where the command would be a little bit
different.
I personally, by the way, prefer the way that H is set up here in a sense that H will then just
complain
that Globbing could not be applied and it will not even call the move command with this here
as a file
name.
So I personally prefer what that h is doing, but this is just an example about the differences
between
those shells.
Luckily, the differences between Bash and Zsh are quite minor, so I'm able to point those
minor differences
out here.
During the course of this course.
I now just enter exit to go back into my bash and yeah, this is now pretty much how Globbing
works.
I want to end this lecture with a few important hints.
The globbing or file name expansion uses wildcard characters and they must not be quoted,
so neither
in single quotes nor double quotes if they are quoted in any way.
So, for example, if I would say something like this, this would probably the way to go if we
use
an echo.
This is also one of the reasons why we were also always using single quotes, because they
pretty much
disable all the rewriting features that the bash might apply.
File name expansion or Globbing is just one of those.
Then we can see that Globbing is not even applied here.
Well, okay, right now there is no image in that folder, so Bash would not apply the globbing
globbing
anyway because it couldn't find any pictures.
But if I would do something like this, you can see this one here.
It's now not being rewritten.
But if I leave out those quotations or the quotation marks, you can see that this one here has
now
been expanded to all the files and folders in my current working directory.
So you can see the difference here.
This one is just a string and it will not be changed.
This one here will have globbing or file name expansions applied to.

Why is it important that we can disable Globbing sometimes?


Well, let's say we want the file name of a file to be star dot jpeg.
So we want the file name to be something like this for example.
And then I could just touch this file and I could put it into those single quotes here and then
Globbing
would not be applied to this one here.
And if I would now output this, you could see that this one here is now an actual file name.
If I now want to do something with that file, I should disable Globbing again.
Of course.
So for example, if I want to move that file, the easiest way would be to, for example, put it
into
single quotes and then I could, for example, rename it new Jpeg or something like this.
And here I've now disabled Globbing again.
So this is now just considered as a file name with a star inside or asterisk.
And this file is now moved to this file here if I execute it.
And now you can see how this one here has been executed.
This one here might have worked also with Globbing, because technically, then this one here
would
have resolved to the file name star dot Jpeg because there's only one Jpeg file in that folder
and that
would then also have been renamed.
So here in this case, both would have worked because even if Globbing would have been
applied here,
it would still have found this file here.
But if we want to disable Globbing, this would be one of the ways to do it.
We will have a separate chapter about the so-called Bash expansions and Globbing and how
all of this
works, where we will look more into the details here.
But for now it's more important that we get a better overview about what Bash can do,
because only
then we will be able to actually utilize the full potential of the so-called shell expansions and
globbing
and how all of that relates together.
So for now, we will just have a basic overview here.

And later we will look into all those details.


The next thing I want to tell you is we haven't looked into regular expressions.
We will have a separate chapter about them later in this course.

But because I know that this course will be watched by quite a few programmers or people
who have programming
experience before, and you may have used regular expressions before.
I just want to point out that Globbing does not use regular expressions.
It looks a little bit the same that we have wildcard characters and stuff like this, but it's not
regular
expressions.
It's a different syntax, so please do not confuse it with regular expressions.
It's something completely different from a syntactical level.
Um, so just be aware of that.
So these are the basics about Globbing.
However, Globbing also supports a few more wildcard characters and I want to introduce you
to them
in the next lecture.
Advanced Globbing Wildcards: `?`, `[0-9]`, `**`
In this lecture, I want to show you additional wild cards that you can use for Globbing.
So, for example, we could use the single question mark to match any single character.
So we had the asterisk sign that would match zero to unlimited number of characters.
However, the question mark only matches exactly one single character.
We can also specify ranges using the square brackets, and then we define the range.
So for example, here we would match, we would specify one character out of the range of all
numbers.
So that would mean we would exactly look for one digit here.
Let's just have a look at those two first.

So for example, here you can see that I'm in a folder.


And for example, here we have those two different files.
How could I match those, for example, using the additional tools?
So one thing I could do, I use the echo command here because I don't want to move those
files for now.
So I could say, okay, my file names, they start with M or IMG, then I would want, for
example,
one character that could be anything.
And then I want those file names to end with six, six, seven, seven.
And then I could say that I would accept every file extension here if we can see this.
Globbing has still worked and it has found those two files.
The advantage is, for example, if one of those files would be called slightly different, for
example,
this file here would have a slightly different file name, for example, something like this.
And then maybe I just add, uh, I just add a letter now.
Um.
If we had those two file names here, for example, one with an underscore here and one with
an A here.
If I now go back to my previous command, this one here would still match both of them
because this
one accepts any Exactly.
One character here.
So you can see here this one here.
Has now not found this one here because I had written it in the wrong way.
Um, the file name is wrong.
It needs to be like this.
I had forgotten one seven here, so I just repeat my echo here again.
And now you can see that it finds the movie and the SRT file.
So you can see how the question mark here works.
Let's have a look at how the numbers or how the square brackets work.
So, for example, let's look into our images folder and you can see here that we have different
files
here.
Let's say we want to have, uh, we want to find those two images, the images that starts with a
six
here.
And then we want to have exactly three, for example, digits here.
Then I could say echo and I want to go into my images folder.
I could either say images slash or I could indicate that it starts at my current folder.
This one here is a little bit more verbose.
Uh, and yeah, and then I could say, okay, it should start with IMG, then I could say here.
Character nine No.
Then I say, okay, it should continue with six and then I use the character range here, for
example,
like this to indicate that it should be followed by exactly three digits.
Then I would have a dot.
And then for example, I want all the extension to be to contain exactly three, um, lowercase
characters.
So I've typed those character ranges in here.
I'm repeating myself here because I want to have three of those lowercase letters.
Um, it does not like the normal Globbing does not support that.
We can just say here, this should be repeated three times.
There's a feature called extended Globbing that does support that.
But the normal globbing doesn't support that.
We can say here something should repeat a few times, so we need to repeat it ourselves.
So if I now hit enter, you can see how those two images have been found.
However, the first image has not been found.
One thing why we are not going into the detail here about how we could use extended
globbing and all
of that stuff is because I have almost never used this one like this one here I have used in
practice.
But if I'm completely honest, I actually use the asterisk most often.
So if I would just find if I would want to find all those images that start with this one, I would
usually write it like this.
Images slash img underscore six, and then just the asterisk sign.
And then I would also find those images.
So as you can see, there are multiple ways to find our files with globbing and you can really
pick
whatever works best for you in your current use case.
But this is something I want to show you.

But everything more than that, we are going to leave out of this course.
Um, I want to show you one more thing how you can use Globbing.
That's the double asterisk sign.
This matches zero up to arbitrarily many characters, including the slash, so it looks into the
subfolders.
However, please note it's only supported in Bash 4.0 or higher and it might be necessary to
enable
this through the opt command minus s glob star, and we might have to enable that feature.
Let's just have a look whether this works in my terminal.

So right now you can see here I'm in my terminal and let's say I want to find all the JPEGs.
But as you can see, the JPEGs, they are in my images folder here.
So how do I find them in my nested folder structure?
The way to go is that I use the Glob star and the glob star.
I usually combine with a slash and then the actual file name that I want to use.
This is important because otherwise if I would add something or if I would write it like this, it
would
look for a folder that would be called Images dot Jpeg.
And this is not what we want.
We want to have zero to unlimited number of folders in between.
And then we are looking for files that match exactly this pattern.
So this is why we use glob star, then a slash and then how we want those files that we
actually look
for.
How they should be formatted.
If I now hit enter, we can see here it's expanding because shopped was enabled on my
computer here.
So as you can see, this can be really, really useful if you want to find all the files in a specific
folder structure.
And of course you could combine it Now, for example, you could now copy all of those files
into,
for example, the current folder.
So you can could really combine it or you could also look for multiple file types.
So for example, something like this would copy the JPEGs and all the mod file with
lowercase, uh,
um, uh, file ending into my current folder for example.
So this would expand this one here would expand and this here would then be the destination
where we
want to copy everything to.
So as you can see, one command here in Bash can be extremely powerful and can do a lot of
stuff at
the same time.
And this is why.
Bash Why I like Bash so much, because we have so much power in just one simple
command.
However, we need to be a little bit careful with Globbing.
And I will show you that in the next lecture.

Pitfalls of Globbing: Avoiding Traps in Pattern Matching


In this lecture, I want to emphasize that you have to be really careful when you use Globbing.
The problem with Globbing is that it's quite broad and this can lead to some issues sometimes
or it
can actually be dangerous.
Sometimes I use the word dangerous here because if we use Globbing in a wrong way, it can
lead to permanent
data loss.
If we combine it with a command, for example.
The problem is that Bash doesn't differentiate between a folder and a parameter.
So that's exactly the same thing for the bash.
It doesn't differentiate between.
So it means that the name of a file may be interpreted as a parameter.
This is especially dangerous if a file has a name, for example, such as minus RF, that's a
totally
valid file name.
But then for example, if we would execute our asterisk in that folder, then we can see that the
star
sign, it's expanded.
So minus RF will appear in the command and then the program will think that minus RF is a
parameter,
and minus R means that it's recursive.
Minus F means that we do not want to ask.
Sometimes RM might actually ask us, depending on the configuration, whether we actually
want to delete
a file.
But even if it would be configured in a way that it would ask us, it means that we want to
suppress
that and we don't want to be asked at all.
And then it means that everything will be deleted recursively.

Let's just have a look at how all of that works in our shell.
So I'm here in in a new folder and I just want to create a few files.
So for example, here I create one file called Important dot txt and another one I could for
example,
create for example here letter dot txt.
So you can see that here are a few important files.
Then maybe we also have some documents.
So I create a documents folder and in that folder I also create one file.
Now for example here.
Um, um, what do I create here?
For example presentation dot txt.
So as you can see, we now have here a documents folder and we have two documents here
and another document
inside that folder.
So you can see this is a normal file structure that we might have.
Now, if I use this sign here, Dash will just expand this for me.
So this means that this is now being expanded before our command is being executed.
And it just means that all of this stuff is passed as a parameter to our Echo program.
It's no problem in this case.
It can just output that one.
The same would happen if I would do this one here.
Then it would also calculate all of this expansion would be calculated, it would be set here
and the
program would run and everything.
Like it would remove the files, but not my folder because the minus R flag is missing.
I don't want to execute this program right now.

However, the the problems start happening.


If we have a file that would be called, for example, minus RF, just as an example, we could
also
have a file called minus R and another one minus F, but let's put both into one file.
So we create a file minus RF.
You can see that I'm using the single quotes here.
However, the single quotes and bash, they don't have that much of a specific meaning.
They only have the meaning whether, for example, the globbing should be applied or not.
So this is actually well, it's a valid file name, but Bash understands this as a parameter for our
touch command.
At least this part of what I'm highlighting here, and it's trying to think that we want to create a
file, uh, called F and somehow it can't even create that.
So maybe this one here has even changed the behavior of our touch command.
How or what this parameter does in this case doesn't really matter.
All that matters is that touch doesn't understand this as a file name.
There's no differentiation between a parameter and a file name in bash that we use as an
argument.
So here this is understood as a parameter that should modify the behavior of touch.
And this one then, of course, doesn't work to get rid of that behavior here.
Actually, we would not necessarily need those single quotes, but we can add the hint that it
should
be in our current folder with the dot slash here.
And then for touch, it's clear that this is not a parameter that should change the behavior of
touch,
but that this should refer to a file that's in our current working directory and that we want to
create
this file.
Now you can see that we have created a file with a file name minus RF.
What's now the problem?
Let me do this one here first.
If I now just use the asterisk here and the file name expansion is being applied here, you can
see
all of those.
Or this is the result of the file name extension expansion, if I would now call them with all of
those
parameters.
Again, Bash is just rewriting the command for us before it's even executing our command.
So what I'm highlighting here will this one will be replaced with all of this one, and this one
will
no longer be a file name, but a parameter because this is how our M will see this file.
And if I now hit enter, you can see it has not ask.
But it has deleted all the files and folders in my important documents got lost.
So you can see that there are M command now has not.
Um, work the way we thought it would, because this one file name here has changed the
behavior of
our command to, for example, include directories.
And even if our system was configured in a way that it would ask that it would not ask us
before it
deletes.
So everything has been deleted, at least all of those documents here or this folder and those
two documents
that were listed here.
This file here, of course, has not been deleted because has understood this as a parameter that
should
change the behavior of this is why this file is still here.
You can see that this can be really, really dangerous and we might accidentally delete files
that we
don't want to delete.
Let me just create a few files here again.
Um, so how do we protect us from that?
The way to go is that we use a slightly different way whenever we use the asterisk sign.
And it's actually better to always try to write it in that way.
The way to go is that we add a dot slash here, which just means that we want to go into our
current
folder, into our current working directory, and we want to list all the files here.
Look at what this expansion does differently than before.
Let me show you before it was expanding to this one, but if I would use this one here, our
paths are
slightly different.
You can see that they all start with a dot slash.
And this means, for example, for our command, that this is no longer a parameter, but this is
actually
a file name that it should delete.
So if I would now say RM Dot slash star.
This file name cannot be passed as a parameter, so it will not change the behavior of our
command,
but it will be parsed as an actual file that we want to delete.
So if I now hit enter again, it has just deleted everything here.
But if there was now a directory or whatever, it would have asked us whether we actually
want to delete
this directory.
We can see this for example.
Here, let me just create a directory again.
Documents and touch letter dot txt touch.
Minus RF.
So now we have all of those files again.
And now, for example, if I would do this, the documents folder will not be deleted because
the minus
R flag is not active because this one here has now been understood as a file name because we
have added
a dot slash before.
So this is best practice that whenever you are dealing with files that are in the current folder,
that
you just add this one here before because every now and then it might prevent a lot of
problems and
it will make sure that your program or your script or whatever you type here will always act
in a really,
really predictable way.
And then this feature is really, really powerful.
Don't get me wrong, this is not necessarily bad.
This is just the way Bash works.
And it kind of makes Bash so extremely powerful, all of those features.
But with great power comes great responsibility.
So we need to be aware of all of this stuff.
And this is why I'm what I really also try to emphasize here in this course.
On the one hand, I try to show you how powerful Bash is and how much you can do with just
a line or
two of code.
But then I also want to point out, okay, these are the things that you need to pay attention to.
And this, for example, is one of them.
All right.
This is what you need to pay attention to when it comes to globbing or file name expansion.

Download the materials for the next exercise

In the next exercise, we'll use a pre-made folder structure. Pease download this .zip file, and
unzip it into any new directory. Usually, you can just double click on the .zip file, and an
Archive manager will open, which will allow you to unzip the file.

If you want to use wget to download this directly into the VM, you must ensure that wget is
installed first ( sudo apt install wget / sudo dnf install wget ), and then
you can download it with the following command:
1. wget https://downloads.codingcoursestv.eu/055%20-%20bash/globbing/companyshare.zip

If you want to unzip this file in the Terminal, you can also use the Terminal to create a new
folder in which you unzip the .zip file. Please note, that you must ensure that unzip is
installed first ( sudo apt install unzip / sudo dnf install unzip ):

1. mkdir companyshare
2. unzip companyshare.zip -d companyshare
3. cd companyshare

Exercise: Navigate a nested folder structure using


Globbing
In this lecture, I want to give you a small exercise to allow you to practice Globbing.
So what's the exercise?
Imagine we are running a company and we need to urgently provide documents, for example,
for a court
hearing.
But it doesn't really matter.
We just need to urgently provide certain documents for our exercises.
Right now we have the month march and we need to provide all the Excel and PDF files for
January and
February.
How do we do this?
Can we use Globbing for this?

And I've created this folder structure for you.


You find the screenshot here.
Please note all the files are completely empty so don't try to open them.
It will probably not work.
You can see here that our month are well.
They have an they start with a number here which might be useful for our globbing and then
you can see
that there.
For example, we have Excel files, PDF files.
We have the same for February, but we have files that we are not interested in that our
lawyers don't
need in our for our exercise here.

So our exercises that we need to extract those files.


However, our company has multiple departments, for example, the department purchasing
and another
department sales and we need to find all the departments or we need to go into all the
departments,
go into the month, January and February and, and extract all the Excel and PDF files out of
this folder
structure.
You can imagine this through a user interface would still be possible, but only because we
don't have
that many files.
But just imagine we had maybe 1000 or 5000 files.
Then it would start to become really, really complicated.
Before you start the exercise, I want to give you a few tips.
You can use custom ranges.
This might help you with selecting the proper month.
So, for example, this range would would match one of the characters of the following.
It would match a zero, a one, a two, a three and a four.
So you can use those to help you select the month.
In addition, you can combine custom ranges with wildcards and also a fixed things.
So for example, here we would have an uppercase A followed by one character of this range
and then
zero to unlimited more characters through this wildcard here.
So you can combine this and you might need this depending on how you solve this exercise.
In addition, you can combine multiple patterns into a single copy command.
So for example, this one here would look for this one here and for this one here and copy all
into
the destination.
So this would expand.
This one here would expand and the copy command, the last parameter that we pass to our
copy command,
that's our destination.
So this one can expand to whatever five, ten, 20 images.
This one here could also expand to, for example, 20 or maybe even 100 images.
And as long as the last parameter, as long as we have one thing at the end, our last parameter
after
all the expansions, that's the folder where all the documents are being copied to or the images
in
this case, of course, those tips, you have to adapt to the problem or to the exercise.
So the folder structure that you need for this project is attached to this lecture.
It's a zip file and you need to extract it somewhere and then navigate to that folder.
You could, for example, extract it, then go to your terminal, type in CD, then a space, and
then
drag and drop the folder.
And then usually you should be able to navigate to that folder, try to first solve this exercise
on
your own.
You will see how easy this is with Bash and how compact all the commands are.
If you're struggling or if you want to see how I solve this exercise, just watch the next lecture.
That's the sample solution.
Solution: Navigate a nested folder structure using
Globbing
Let's now have a look at the solution, how we collect those files.
And one thing I want to point out here first is that I've listed my directory structure in my
terminal.
I've used the tree command here and I've run that command on my current working directory.
Please note that you may or may not have to install this program.
It really depends on your system configuration.
This just lists this here as a nice tree.
It's pretty much the same as you've seen in the file browser before.
So what do we want to extract?
We want to go into one subfolder and then we it's difficult here to select those folders.
We can't select them by name, but luckily we can select them here by their number because
we are only
interested in January and February.
So we can say, okay, we want to go into one subfolder, then we want to have a zero and then
we want
to have a one or a two because we are only interested in January and February and then we
want to copy
or we want to collect all those Excel files at first, for example.
So the way to go, for example, would be I just zoom out a little bit.
I could say, okay, here I'm interested in purchasing and sales.
So all the folders, then I want the subfolder name to start with a zero.
Then I want to have a one or a two.
So I just create this range here.
Then I accept any zero to any character, so I just match it with the asterisk and then I say,
okay,
I'm interested in anything that ends with XLS x and let me see, or let me see what happens.
If I output this, then you can see how my files here have been found and those are all the
Excel files.
So how would I copy those?

I just clear and start again.


The way to go would be, for example, that I would create a new directory.
For example, I call that export.
I now clear my screen again and now we can see this is the folder where I want to copy that
to.
And now what I could do is that I now just go back here and I now copy all of my Excel files
into my
export folder by writing it like this.
If I now hit enter, all those files have been copied and if I now list my contents here again,
you
can see here those are the files in my export folder.
However, I needed to copy all the Excel files and all the PDF files.
How do I do this?
Luckily I have a simple way to do this, and the way to go is I just take my existing copy
command and
I just add something here.
I add, I repeat myself here that I'm looking for all of those files and all of those files that end
with dot PDF and I want to copy all of them into my export folder folder here.

If I now just hit enter, it has now copied all of them and if I now list my directory again, you
can
see that here in the export folder we have now collected all the XLS X files and all the PDF
files.
So this now here looks pretty good.
So you can see how this task of collecting documents became super short.
It pretty much became a one liner here in batch.
And you can imagine if we had more files, um, it would be really difficult to do this through
user
interface.
Another thing that I can also reveal by now already is that later in this course you will learn
even
more expansions that would allow you to combine this into an even shorter way to write it
that you could
say, okay, the file name should end with XLS, X or PDF, and you could combine those two
into just
one command or into just one thing here instead of two separate things like this would be the
second
one.
This would be the first one.
You could combine those two into one and say, okay, the file name should end with XLS, x
or PDF.
Actually I'll just reveal it.
Um, dot pdf xls x.
So this would even be shorter.
But we will get to that later in the chapter about expansions.
Um, one thing you probably noticed is just how powerful Bash is.
And this is really the reason why Bash is so incredibly powerful and useful.
All right.

This was the sample solution for our exercise, actually, not this one, of course, but the
command
we've used before.
This is the sample solution to our exercise.

Extra lecture (optional): Sophisticated File Searching: the


Program `find`
In this lecture, I want to present you the Find program.
Please note this is a bonus lecture.
A bonus lecture in this course means that there's a that I present you something that is nice to
know, but the knowledge of this lecture is not really required for the rest of this course.
So this is a bonus lecture, but I still would recommend you to watch it.
So the find program, instead of searching by name with the wildcards through bash Globbing,
we can also invoke the Or call the find program to start a more sophisticated search.
The find program I say program here because it's an executable file on your Unix
environment.
Works that we can just say find.
And then we need to provide a path as a first parameter, for example the dot for the current
working directory.

Imagine for example here I would be in a folder with like several subfolders.
And in those folders there were quite a few of files.
And if I want to list all the files I can just say find.
And then I provide the directory in which I want to find files.
And that would be my current working directory.
If I now hit enter, then we can see that we get a list of all the files and folders that are in this
directory.

Here we see that there are folders here, but also all the files are being listed here.
And we also see that there are some additional files that my operating system likes to create.
The store are like meta data files from Mac OS, X, and Mac likes to put them into all the
folders,
but they are hidden files, but they are listed here as well.
This output here contains now all the files and also folders that are in this folder.
However, if we would run the find command again, for example, on my whole hard drive, the
output
here would take forever.

We see it still working and this would take now really, really long.
If you want to stop something like this, you can hit Ctrl and C and then a program that's still
running,
it's being terminated.
So we can go back to our bash.
So please be aware if you use the find command on a folder with many sub folders such as
my root directory,
it may take quite a while actually.

However, if we just use it on our current working directory, it's usually pretty fast because
they're
not that many files inside.
This find command allows us to filter the files.
For example, if we want to limit the file type, we could say find dot and then add the
parameter minus
type f.

And this means that we want to find all the files in our current working directory that are of
type
file.
There's also, by the way a type directory.
If we only want to list the folders.
Let's have a look how this works.
So for example here find all files in my current working directory of type file.
And then we see we get a list of all the files that are in our current working directory.
Or if I would want to list all the folders I could just say here directory hit enter.
And we see those are all the folders that are in my current working directory.
But there are also other ways how we can use the find program.
For example, we could also find all the files that have been modified within the last seven
days by
providing the directory.
Then we say we only want to list files, and we want to further filter all those files that they
need
to have been modified within the last seven days with the m time.
M time stands for modification timestamp.
Or another way we could, for example, also filter our files could be that we want to find all
the
files where the size is more than ten megabytes.
Let's just try this one.

So for example in my current folder, this is the folder in which I want to find files.
I want to find all the files where the size is more than one m, for example.
The images here are not that big, so I need to be a bit more relaxed here about the file size
requirement.
And then we get a list about all the files that are bigger than one megabyte.
You could imagine this might be sometimes quite convenient.
However, be aware that fine can also actually modify the file system.
For example, we could also delete all files.
So here we would get all the files that are empty and with the minus delete we would then
just delete them.
Or another example could be for example here with this command I could just say minus
delete.

And if I would now hit enter then all those files would be deleted.
Let's have a look at how this works.
I just hit enter, those files are deleted and if I would now list all the files again.
You see, the list is way shorter than it was before because all the bigger images have been
deleted.
I want to emphasize here.
I know we have looked at how we get help and how we can look at the manuals before, but I
want to emphasize
this here again.
We could say find space, dash, dash, help.
And then we would just get a short help here.

That, to be honest, is not that helpful here.


In this case we would just say I need to say, man, we want to look at the man pages for the
find command.
And if the man pages are not installed, we might need to just Google for that or install them.

But here we can see, for example, that there are way more parameters that we can use, and
they are
all documented here.
And then we can of course close that one with the button Q.
And then of course, we can close this with the button again.
I just want to emphasize this, that there are way more options here, because in the next
lecture we
want to practice that a little bit.
And there'll be a short quiz about using find.
And the task for you will be to find out those parameters on your own.
File management (Part 2) – Handle the text file
Intro

Viewing (Text) File Content: the Command `cat`, `head`


and `tail`
In this lecture, I want to show you how you can read files with the command line.
The easiest way to read files is the cat command.
This just prints out the contents of a file.
For example, we could just say cut data dot txt, and then this command would read the data
from that
text file and print it to our terminal.

Let's just have a look at how this works.


For example, imagine I'm here in a folder and there's a file called Bash Dot TXT in that
folder.
Let's say I want to output this file directly to the command line.
Then I could use the command cut, put in a space and then as a parameter I just pass in the
file name
for example bash dot txt and then I hit enter and then we can see the contents of that file.
Be aware that we can use all the batch features we have learned before.
So for example, I could also use Globbing and let batch calculate the file name for me and we
can see
that this file is now also being outputted in case this would match to multiple files.
All of those files would be printed to the terminal, the Cut Command.

There's one nuance that I want to show you.


I frequently have problems if I accidentally cut binary data.
Let's say, for example, I would accidentally cut a Jpeg file.
Of course we can't print this jpeg onto our terminal.
Well, actually we can.

We can see all the binary data that is being displayed here.
I frequently had the problem that after cutting binary data, that after that my terminal doesn't
work
properly anymore.
Right now here, this terminal seems to be relatively reliable, but some of the terminals do not
work
the same way before.
It seems to be me that maybe in those binary data there might be special commands that are
interpreted
by our terminal and they change the behavior of our terminal in unpredictable ways.
Thus, I would highly recommend you that if you do experience problems in your terminal
after having
cut it binary data, it's probably because special commands are being sent to your terminal and
the
terminal is doing something that it shouldn't be doing and it may act in a weird way.
After that you might have to restart your terminal and then everything should work.
However, the cut command is not enough to display files here in our terminal.

Why?
For example, if we look at the folder here and I've provided you the this folder as a download
so you
can also reproduce exactly the same as I'm doing here.
We see that there's this text file Romeo dot txt and this text file contains the whole book
Romeo and
Juliet.
Luckily it's public domain, so I'm able to use it in this course.
If I just try to cut this file, everything will work.
However, this book is about 5500 or even a little bit more lines long, so I might have to scroll
quite
a bit up in order to be able to see the whole book and the terminal that is that I'm using here.

They all have to buffer the previous lines and depending depending on the configuration of
my terminal,
I may or may not be able to see all the lines of this book.
Let's see how far I can scroll up here and then we see I can't even scroll up enough to see the
whole
book.
This is because there's a buffer of the last lines that have been outputted by our shell.
And if this buffer is exceeded, the first lines of our file are lost and we can't read them.
So in my case, I'm not even able to read the whole book with the Cut command.
This is just because the file is way too long and it doesn't fit into the buffer.
Of course, I could try to see if my terminal offers any settings and maybe I could increase this
buffer,
but at some point this will also slow down our terminal.
Isn't there a better way to work with larger files?
And there they are.
And one command is the head command.
This allows us to show us the start of a text file.
We can also specify the parameter minus N to show for the number of lines.
In addition, we can use the tail command to show us the end of a file.

Let's see how this works.


Let's say I have a really long book, the Romeo dot txt and I just want to output the first ten
lines.
Then I could use the head command with this parameter to just output the first ten lines.
And if I now hit enter then we see.
Okay, we're still not in the book yet.
We are still at the the license declaration and all that stuff.
But at some point the book would start.
But the head command, it just outputs the first few lines of our text file.

If we do not specify any parameters here, then by default it actually prints out exactly ten
lines.
But there's not just the head command.

There's also the tail command.


And this allows us to output the end of a book.
So here in this case, this is the end of the book.

And while this text file, we can see it's still another license declaration.
But this is the end of our text file that is now being outputted here.
As you can see, this might be really, really useful if we work with like long files.
However, we weren't actually able to read the book, so isn't there a way to actually read those
large text files?
And luckily there is.
Reading Large Text Files: the Command `less`
Let's now have a look at how we can read large files using the less command, the less
command or the less program allows us to read large files and it allows us to not run into the
same problems as with the Cat program.
When we open a file with less, we can use the arrow keys to then navigate through the file.
Or we can use special keys such as F for forward or B for backward to navigate a whole page
ahead or back.

Let's just have a look at how this works.


So imagine I want to read the whole Romeo and Juliet book on my command line, or it would
be a long log file if I'm the admin of a web server.
But in my case, I want to entertain myself and read the book so I can say less Romeo dot txt
and just
press enter.

And now I'm able to read the book and the nice thing is I can now just use the arrow keys to
actually
scroll through the book.
And now you can see that I'm now actually starting to get to the prologue.
Actually, no, I'm still in the table of contents, but I'm actually able to read the book and to go
through I'm using the arrow keys to go through the book here.
However, if I want to go a full page ahead, I can press the F key and it will jump a whole
page ahead.
And this can be useful because then I don't need to scroll through the book and every time I
reach the
end, I can just press F and continue reading from over here.
As you can see, I'm thus able to read the book quite comfortably.
If I know.
I've already read about half of the book and I want to navigate to a percentage of our content.
I can just enter £0.50 and navigate, for example, to 50% of our content.
Let's have a look.
Here I can just enter £0.50 and then we see that it has scrolled to 50% of our book.
And this is right now exactly the middle here of our book.
However, can you trust me?
How do I know this?
Well, there's another command with less, and we can use the equal sign to show info about
our current
position.
So let me just go back and I press equal.

And then we see here, this is our current position.


And right now we are exactly at 50%.
To hide this again, we can just press enter and we are back in our reading mode.
There are more options here for less.

Another option would, for example, be.


Minus uppercase n, and this would tell us that we want to display row numbers.
So let's try this out.

So here I just type in minus big N or uppercase N, and then I confirm this with Enter.
And now we can see that when I scroll through the book, we can see the row numbers here
and this can
give us some orientation where we are in our book.
If we want to find something in this book, then we have also the ability to search.
We can do a forward search with a slash or we can do a backwards search with a question
mark.
Forward search means starting from our current position.

Let's find something.


Backward search means let's find something that's before our current position.

For example, let's say I'm looking for the word important here in this book, and then we see,
okay,
here we are.

I guess we are back in some legal paragraphs, but at least we found the book.
Uh, at least we found the word.
Or I can try a backward search for important.
And then we see, okay, there was this one, and before that one, there is no other occurrence
of
important here in this book.
Okay, let's search for something else.
For example, food.
And then we find here the word food in line 4846.
As you can see, this might be quite useful.
And we can go through our book here.
If we want to quit, then we can just hit the key and then let's will quit itself.

So if I now go back and I just hit the key, then we can see less has quitted itself.
As you can see, the last command may be quite useful for long files.
It supports way more that I shown you here in this lecture, but I would say I've shown you the
most
important features of less.
But yeah, there are way, way more.
But you can imagine it can be quite useful if you do work with long text files through a
command line.
Counting Words and Measuring Disk Usage: the
Programs `wc` & `du`
When we cut a big file or we output that to the terminal, it can kind of overwhelm our
terminal or
it can take quite long.
So let's now have a look at how you can get the size of a file.
Before doing that.
And one way is that we can use the word count program.
That's the WC program.
And there we could just say, okay, word count space file dot txt, and then word count would
open
the file and tell us some information about it.
This command would then print out the number of lines, the number of words and the
number of bytes
of our file.
We can see that we have two ways of writing that we can say WC file dot txt, or we can
specify a few
parameters which would be here in this case.
WC To be a bit more specific about what we are looking for.
The thing is that word count supports various parameters.
One for example would be minus L, that would just count the number of lines.
Or we could also use minus W, which would count the number of words or minus C, which
would count
the number of bytes.
It's minus C because C stands for character.
Back in the days, one character used to be usually eight bit long one byte.
Nowadays, characters can be way more complex.
We have emojis and all of that kind of stuff, but it's the flag is still called minus C because
back
in the days it used to stand for one character.
So those flags allow us to specify what we are looking for.
But if we leave it out, then it will print the number of lines, the number of words and the
number
of bytes.
By the way, depending on your system, there may also be more flags that you could use, but
those
are the three most important ones and the one the ones that should work on any Unix system.

Let's now have a look at how we can use the word count program.
So, for example, let's say we are in a folder and here we have the book, Romeo and Juliet.
Let's find out what word count tells us about this book.
Please note it's a program.
It's not a bash feature.
It may be that you might have to install this depending on how you use or where you use the
bash.
But usually this is one of the default programs that you should usually exist on all computers
or on
all Unix systems.
So if I just invoke the program, word count and I pass in the file that I want to access, for
example,
Romeo dot txt, then we can see, okay, this file contains 5600 and something lines.
This is the number of words and this is the number of bytes that this file contains.
One common parameter is that we want to specify which thing of those are we interested in.
And the most common way would be minus L, which means that we are only interested in the
number of
lines.
I could now say Romeo dot txt and we see it only outputs the number of lines.
Technically there are also other parameters.
For example, I could also just output the number of bytes, but the most common way to use
this is
minus L for the number of lines.
As we can see, we are able to inspect this file and see that, okay, maybe this is a large file and
we should not cut this into our terminal directly, but it would be better to use less for this one.

However, there is also another way to see whether a file is big or not.
And we can do this with a program disk usage or do or actually just do.
That's the program name.
And the program name stands for disk usage.
This will calculate the size of all items in this folder, or we can also pass in a file name as a
parameter
and then it will only get the disk usage for that one file.
We can also specify the parameter minus s to just get a summary.
Otherwise disk usage will tell us the size of each folder in that exists in our current working
directory.
So if we have subfolders there then it will tell us the size of each of those subfolders.
Let's have a look how it works.
So I'm here in this folder.
There is no sub folder here.
I can just start the program by just starting it and it uses my current working directory and
then we
can see how big my current working directory is.
I could also specify a file.
For example, I could say, okay, I'm only interested in the disk usage of Romeo dot txt and
then we
get the size which is here.
336.
What does this 336 mean?
Well, it depends.
It means the, the file size of this file is 336 unit.
How big is a unit?
That depends on the operating system.
There's one configuration option.
That one unit is 1024 bytes.
That would be one kilobyte.
But here I'm on a mac and one unit is 512 bytes, so we need to divide it by two to get the
actual file
size.
So the actual file size would be about 168kB.
What happens if I run exactly the same code on a linux machine?

Here we are on an ubuntu.

I put this somewhere so I can access it easily from my ubuntu machine.


And as you can see here, do I just provide an absolute path to my file?
This is allowed as well.
And we can see here.
Ubuntu tells us the size in kilobytes, which is pretty much what we would usually expect
from a disk
usage command.
It's pretty weird that mac does it in a different way.
Well is it?
There's the so-called Posix standards, which is a standard that defines how a Unix system
should act
and Mac OS follows this standard.
And by or within that standard, the block size is 512 bytes.
So actually Mac is just following the standard for Unix, which has been defined and thus we
get this
result here.
However, I also do agree that what Ubuntu is displaying or printing actually makes more
sense for me
to um.
To change this, we could add a parameter for example minus h to get the size in a more
human readable
format.
And then also mac prints it in a proper way.
For example, here k stands for kilobytes.
Um, if the file size would be bigger at some point, there would be an M which would stand
for megabytes
here.
Or we could force Mac to use the normal kilobyte block size and then it would also work
properly.
That would be this option here.
Just be aware that by default, the disk usage on a mac uses a different block size of just 512
bytes.
So this output actually it's quite confusing, but we can't change it.
It's just how it is.
So you now know how you can output the size of a file and how you can use the WC
command to count the
lines or also the number of words in a file.

Text Editing in the Terminal: the Command-Line-Editor


nano
Editing files directly with, like a nice text editor is not possible in Bash.
There is no built in text editor for Bash.
However, we can install additional software to also add a text files on the command line for
quite
popular options are Pico or Nano.
That's a simple text editor for text files and batch.
Those are actually two different editors.
We can imagine that Pico was the original editor, and Nano is a complete rewrite with more a
few more
features than Pico.
The goal here for this editor is to have a simple text editor that's quite easy to use in the sense
that there's almost no learning curve for this editor.
This is the logo of Nano.
And then there's, for example, also V or Vim.
Vim is also a complete rewrite of V, and it's been more extended with more features.
And that's a more advanced text editor with a quite unique way to edit files, which may make
you extremely
efficient when editing files, though there's a steep learning curve involved.
So you would need to learn this text editor before you are actually really fast with it.
I'm fully aware that there are also more text editors, and I'm also aware that this discussion
may
be quite heated online.
But please, let's leave this out of this course here.
In this course, we will just use Nano because it's a simpler learning curve and I do not want to
get
into the details of how to be fast with Vim.
Those text editors may already come pre-installed with our operating system.
For example, on my Mac Pico is installed by default and if I want to start nano by default, it
will
just start Pico.
So some of those editors may already be installed on your system if you want to install them
afterwards.
For example, you want to use the real Nano on a mac.
Then you would just use the homebrew and then brew install nano or for example on Ubuntu
Linux or Windows
subsystem for Linux with an Ubuntu inside you could just install nano with the usual apt get
update
and apt get install procedure.
Let's now have a look how we can actually use this editor.
Okay, so I'm here in a batch and let's say I just want to edit a text file, for example.
I want to edit this text file.
Then if none is installed on my computer, which here in my case I've installed with a brew
command,
otherwise it would just start Pico.
But they act really, really similar.
So I could just launch the program with nano space and then I just say which file I would like
to open.
In my case that would be the bash dot txt file I use tab to autocomplete the name of the file.
If I open a file that doesn't exist, it would be created once I then save the file later from my
editor.

Now I hit enter and now I can see that I have a basic text editor here and I'm able to do some
changes here.
For example, instead of bash is cool.
Maybe I want to emphasize this a little bit more and add a few exclamation signs here. Then
you can see that here below we can have or we have some commands that we can use.
For example, if we want to write this file, I could hit this O key.
However, this one here means that we need to press the control key first so I can just hit
control, keep it pressed and then hit O, and then I can choose the file name and I can just say,
okay, this is the file that I want to save.
Then I can hit enter.

And now this file has been saved to then quit this editor.
I could hit control and X at the same time and my editor has been closed again to just open
the file
again.
I could just use the arrow keys to get my last command and then hit enter.
And now we are here.
Back in our editor.
There are also more things we could do.
For example, we could navigate the file or we could hit control and C to get the current
position in
our file.
Or you can see here there are also a bit more commands, for example, for search or for cut
and paste.
Those do not share the clipboard from your user interface.
They are a separate clipboard to cut and paste a line, but you can see we have some basic file
operations
here.
It's not super advanced, but it's enough to get most of the job done.
And I feel this editor is quite all right to do some small changes to some text files or some
configuration
files.
Why would I use this if I could also use Visual Studio code?
Well, sometimes there's a little bit of overhead involved for to use an advanced text editor
such as
Visual Studio Code.
Imagine, for example, I would not be working on my own Mac here, but this would run on a
remote server
because I want to have a server to host some downloads or to host my website.
So I'm a server admin and then I connect to my server through SSH.
We'll get more into that later.
How we can connect to a remote server.
But in that case I already have a shell open and I can just then just use a nano to change a
configuration
file on my server.
If I would want to use Visual Studio code, I would have to download this configuration file,
open
it with Visual Studio code, then do my changes, then upload it again.
Then I see.
Oh, I have a small typo in my configuration or something.
Doesn't work.
Oh, I need to do the whole process again and then upload it again manually to my server.
So that would be quite tedious.
And just having a small text editor that's running inside our terminal is extremely useful,
especially
when working with remote servers or we do if we do not have a GUI.
For larger files.
However, I do prefer to use a graphical GUI and proper IDE or like an advanced text editor
such as,
for example, Visual Studio code.
However, I also know people that actually prefer coding inside a bash based text editor, but
those
people usually prefer vim over nano.
However, nano is the way to go if we want to have an easy way with a simple learning curve
to edit
text files in our terminal.
Exercise Intro: Analyze a Log File
In this lecture, I want to give you a small exercise to analyze a real world log file.
So in the next lecture I will provide you the download link for that one.
But in this lecture I want to give you the exercise.
The exercise is to have a look at this log file and then answer the following questions.
This log file that I will provide you is based on some real world data due to data protection.
Reason.
I could not provide real data.
I had to generate it myself.
But it's really, really close to real world data and how an actual program would log that one.
So the format is pretty much the same.
Your job will be or your exercise will be to have a look at this log file and then think about
what
could this log file be?
What could be written in there.
The next question is how large is this file in megabytes or kilobytes or gigabytes or
whatever?
The file size of that file is.
Please do not look at your browser.
Please look at how you can analyze the file size completely through the shell.
And the third question is how many lines does this log file contain?
A few hints or tips.
One thing that I want to emphasize, please try to answer all the questions through the shell.
So even if you could just open the file in like a editor or anything like this, try to avoid this,
take your time and analyze it through the shell.
Because, for example, if this log file would have been generated on a remote server, then you
might
not have a user interface available and you would only access that computer through the
shell.
So it's important that you try to answer those questions through the shell.
The next thing is that the size of this file is still manageable for our shell.
The reason behind that one is that I want to keep the download rather small.
So even people with slow internet connections can easily download that file.
This limits the number of files, that number of lines that I can put into this log file.
Um, that leads to this file still being manageable.
Small but this is a imagine this was a real world log file.
Um.
And the program would look way more into our log file than at some point.
The size would no longer be manageable for our shell.
So what I'm trying to say here is, yes, you could theoretically still cut the log file or using the
cut command in our shell, but please do not try to do this or try to avoid that one.
Try to use different commands to browse the file or have to have a sneak peek into the file to
see what's
actually written in there.
So in the next lecture I will provide you the download link to the log file.
Then after that I will provide you a quiz so you can confirm whether you solve that exercise
correctly
or not.
They will only be those three questions in this quiz.
And then there will also be a sample solution in case you, for example, got stuck.
Or you want to see how I solved that exercise, then you can also have a look at the sample
solution.
All right, enjoy this exercise and in the next lecture you'll find the download link.
Solution: Analyze a Logfile
Let's now have a look at the solution about how we can analyze the log file.
So let's now have a look at how we can view that file.
The easiest way would be, for example, with the head command.
And I could just say that I want to output, let's say the first four lines of my log file.
The log file is called access dot log.
So I can just output the first four lines.
Then we can see that those lines are rather long.

This is why they wrap here.


So you can see this is the first line.
This is the second line.
This is the third line and the fourth line.
Let's look at the end.
And there, for example, I just use the tail command and we can see it continues kind of like
this.
So the whole file consists out of some logs and we can see some dates.
They were between 11th of July and 12th of July and the year is a little bit in the future.
Well, maybe a bit more but then a little bit.
But we can see those files are log files.

What could be logged here?


Um, we can see here that there is an IP address here.
Sometimes it's IP version four, sometimes version six, IPV six.
So we can see there's an IP address.
We got a date.
We got it.
This is could be a time zone offset.
This looks like a file.
Maybe a file that was requested.
Something like this.
There's something with Http here.
Um, which would be the protocol and the version here.
1.1, there's this one here, but sometimes in this column there might also be a 404.
Let's see if I output a few more lines.

Let's say I output the last 40 lines.


Then sometimes you might see a 302 we can find here and we can see here it's a little bit
wrapped,
but you can see it's 404.

Um, that could not be found.


So sometimes there's 404 um, here, which stands for that this file could not be found.
Then we find a number here and then a URL.
By the way, all those URL names are randomly generated.
So if those exist, it's purely accidental.
But we see there's like a domain or like a link here and then we can see something like this
here,
um, which is something, uh, it's somehow talking about Mozilla.
Um, so something about the browser.
So to reveal what this log file is, this is the log file format from, for example, an Apache web
server.
It's the so-called combined log file format.
You would not need to know that for the quiz.
Um, but it's a log file of a web server.
This is why you.
We have the IP of our of our visitor.
Uh, sometimes, uh, those IP addresses might be, um, randomized in a sense, for example, that
the last few digits might be randomized in order to preserve privacy.
Then we have a date here we can see which has been requested from our server.
So for example, get would mean that we just want to request data post would mean that we
would want
to submit a form to our server.
Then this is the so-called response code.
Whether everything has worked or the file could not be found.
So for example, 200 means everything.
Okay, 404 means file could not be found.
Then we have the so-called referrer.
This is where the user comes from.
So this website has sent the user to this file here.
Um, that would mean this referrer here again.
Again, they are all randomly generated.
So if they do exist is pure accident.
Just try not to click on them and not open them because again, it's completely random, the
domain
names.
And then you can see here the so called user agents.
This is what the browser tells our server, what he is.
And in this case I've checked that actually it's quite an old system.
It's a Chrome 96 on Windows 8.1, the corresponding user agent.
It's putting this Mozilla here because Firefox once was a really popular browser and back in
the days
they wanted the server to enable the more advanced features.
So that's why Mozilla is here, even though it's not a Mozilla and it's also adding this one for
servers.
Um that that might check for that one but at the end it's a chrome 96 at least it looks like that.
Um, it's sometimes a little bit difficult to parse those uh, user agents.
Um, but you can see that this is a log file from a web server and all the requests here have
been logged.
Let's now have a look at how we can analyze that one.
So for example, one thing that we were interested where the number of lines so I can say um,
w C minus
L access dot log and we can see it's 10,000 lines.
This is because I've generated and I was like, okay, I'll just generate 10,000 lines here.

The next thing was the file size that we were interested in.
That was the Du disk usage command.
But I'm here on the Mac and it doesn't really display, uh, it's a little bit weird how it displays
that one here.

So the way to go is that I need the minus H flag here for my disk usage and then we can see
that the
file is three megabytes in size or about three megabytes in size.
So this is how you can solve this problem.
And you have seen that we are we were able to display the log file without outputting all of
them and
this would be more efficient, especially imagine the file would be like 300 or 3000MB in
size.
Redirection – Manage data streams
Intro

Writing and Appending to (Text) Files: the Redirection


Operators `>` & `>>`
Let's now have a look at how we can write output to a file.
Let's say we have a command that outputs something.
It could be anything.
And how do we get this output into a file?

One way to do this would be that we just create our output and then I could just try to
highlight this
and then I could hit the copy key and then I could create a text file, output the TXT, and then
use
the paste function to paste it in and then save it.

And now we have the output in a file.

This works.
If I now cut this file, we see the output is in this file.
However, this was a terminal feature, so this depends on my terminal, whether I can highlight
something
and whether I can hit the copy key or command C to copy everything or control C, depending
on the operation
operating system.
This depends on my operating system, on my terminal, and it would also not work if there's a
lot of
output here.
So at some point this is no longer convenient and it's definitely not the way to go to copy this
and
then just manually paste it into a file.
Okay.
So I delete my output text and let's start from scratch.
Isn't there a better way?
The question is, can we can we just if we have, for example, a command such as the LS
command, can we just redirect the output of this command directly into a file?
Luckily, we can do this with the operator greater than.
For example, I could use any command this in this case, this is an echo command, but it
could also be an LS command.
And then I could just use this operator and then I specify a file name and this file is being
created with the contents of the command that stands or that that's written there before.
If the file doesn't exist, it will be created, and otherwise the file will be overwritten.

Let's have a look at how this works.


I could use any command here.
In my case, this is now just an echo.
And for example, I could say here, let's stick to the common example.
Hello, Bash.
And we want to use this output of this command.
We see this is just a normal output.
I want to use the output of this command, and I want to write this into a file.
Then I could use this operator and then I could say in which file this should be written.
The file could just have be like a file.
It would be like a text file then.
So the extension here, it doesn't really matter.
But because it's a text file, I might as well just indicate this weather dot txt at the end.
If I now hit enter.
We see there's no output here anymore and all the output was or is now in this file.
If I now output this file we can see.
Hello bash.
That's the content of this file.
If I now do another command and I write it into the same file and now I output my this file,
we can
see this is the output of my LS command and the output has been redirected into a file.
However, as we can see, every time we redirect output into this file, the file is completely
being
overwritten.
So the old content of this file has been lost.
Isn't there another way?
Luckily there is.

We can also use the double greater than sign.


And with this command we can append to a file.
So this means if the file doesn't exist, it will be created with the content.
But if the file already exists, the content will just be appended.

So let's just try it out.


So for example, here I just create a few dashes and I use this operator now to to append it to a
file.
So in my case that would be the output.txt.
If I now hit enter and now I output my or I cut my output.txt, we can see that this line here
has been
created.
If I would now, for example, add a few more characters to this file.
For example, like this I hit enter and now I output this again.
We can see those show up now here in this file as well.
As you can see, this one does not overwrite the file, but it just appends to the end.
Let me give you another example.
For example, I could also say, okay, give me the disk usage, for example, of my image file.
And then we see, okay, this file has a file size of 2.8MB.
Here.
In this case, let's say I want to use this and append this to our output.txt file.
Then I could just use this operator here with the double greater than sign and just hit enter.
And we see the output of this command has been appended to this file which we could see if,
for example,
I would access my output txt either with a cut or here.
In this case I just want to get the end of the file and we can see this has been written over
here.
However, is all the output being written to this file?
Let's have another look.
This one took the file size.
And the output of this, the disk usage program has been appended to this file.
However, let's say something happens that should not happen.
For example, here we try to access a file that doesn't exist.
In this case here, if I now hit enter, we see nothing has been written to my output.
TXT.
Well, we don't see that yet, but we could see that if I cut the output to see nothing has been
written into this file.
However, the error is still being displayed here.
So we see some of the output.
Or this operator works with some of the output.
So some of the output, if everything works perfectly, all the output is being written to or
being
appended to the output dot txt file.
But the one small error occurs.
This error is no longer being appended to this file.
Why is that?
Why does the normal output end up in the file here?
But the error output here does not end up in the file.
And to have to understand this, we need to understand the output channels and how bash
works.
And then this will be or we can understand why certain outputs ends up being appended to
this file here
and certain other outputs is being displayed here, even though we are using this operator here.
So let's get into the details in the next lecture so we can understand this behavior here.
The Standard Streams: stdin, stdout, stderr
Let's now just have a look at why Bash was acting in that way in order to understand this.
We need to look into the standard streams and yeah, let's just have a look at that.
So imagine we have a program that could be any program.
It could be our LRS program, it could be a cat command, it could be anything.
By default, there are three communication channels.
How this program could interact with the terminal.
The one is the standard input.
It is also called STD in.
It means or if the program wants to access our keyboard, then it could start listening to the
standard
input.
And then this program could receive input from our keyboard.
The next communication channel is the Channel one or the standard output.
This is called STD out, and this just means that our program wants to display something on
our screen.
So everything our program outputs, it just sends it to the stream, STD out, and then our
terminal
displays that in our terminal.
There's another standard communication channel if our program wants to output errors.
There's also the standard error output.
By default, this also gets printed to the screen.
And this standard error output is called STD error.
So what does this greater than operator do?

Well, by using greater than or the double one, if we want to append something, we can
redirect the
STD out to a file.
So if we, for example, have any program here, here it's just an echo and we want to redirect
this.

We can say or we can tell the shell that the STD out should no longer be printed to our
screen, but
that the STD out should be sent to a file.
And then depending on which one we used, it will either overwrite the file or it will be
appended to
the file.
Let's have a look at how this works.
So for example, I'm now using the do command.
You will see that later instead of an echo.
But for example, I could ask for the disk usage of a file and I use the parameter minus H so
that we
get it in a in a quite readable format here.
So we see this is the size of this file.
Everything has worked here.
So this output is being sent to our STD out and while our terminal prints that one.
If I now redirect this to a file for example here do dot txt.
We can see here this file exists and this has been written in that file or to that file.
This program.
That's why we are using that one here to show it.
We can pass in parameters that causes our program to have an STD out and an STD error as
well.
The way to go is, for example, if I would pass in a file that does not exist here, it will try to
calculate for all the files that we pass in here, the file size.
This one will work.
This one will fail.
So if I now hit enter, we see here both are being printed here in our terminal.
This is because by default our STD out and our STD both get printed in our terminal.
So we see here this file has a file size of 2.8MB and this one is the error message.
And this has been sent to STD R and it also gets printed in our terminal.
If I now would redirect this output to our dot txt, I overwrite this file in this case and then I'm
only redirecting the STD out the normal std out to our dot txt file.
So we see the normal std out has been redirected.
But what the program do has sent to std r still gets printed here in our terminal.
We can also check here.
The dot txt contains the normal output.
However the error we have not redirected the STD error because this one by default only
redirects our
STD out.
The next question now, of course, is if we have a program that creates an STD error, is there
any
way we could also redirect that one?
And luckily there is, and we'll get into that in the next lecture.

Managing Error Messages: Redirecting stderr (and


stdout)
In this lecture, we want to have a look at how we can handle errors or how we can redirect
stderr.
Why would we want to do this?
Sometimes we want to redirect the standard error.
The reason is that, for example, we may have a program that may print errors and we want to
ignore
them because they might not be relevant for us.
So we would like to redirect the STD error, the error output to nothing, that it just gets
ignored.
We will look into that in this lecture actually how we do this.
Another reason would be that we are only interested in the errors and we want to redirect
them into
a file and to just save it and to have a look at them at a later date.
So imagine we have a program that we want to run every day and it just prints out a lot, a lot
of output.
We want to ignore all of that output.
However, the errors that occur, we are interested in them and we would like to look at them at
a later
date.

So how do we do that?
So so far we have only redirected STD out.
This is a repetition of what we have been doing before.
So imagine, for example, we have invoked a program here or we have called a program here,
and then
we use this operator to redirect the output and to put the output into a file.
However, this was the short version of this command.
There's also a more a more verbose way to do exactly the same.
And we could say here that we use the number one and we redirect the number one to
output.txt y.
The number one.
Well, we had different standard streams.
Stream zero was STD in stream, one was STD out and stream two was STD air.
In this case, so far we have only looked into STD out, which is stream one.
And this is the default stream for like an output redirection that we are using here.
I could also explicitly tell that I want to redirect stream one into output.
TXT though those two commands are equivalent.
The second way I can use to redirect STD STD is output stream number two or standard
stream number two.
So I could just use the same command here.
And with this, by writing this I could redirect stream two into the file error dot txt.
Or I could also combine those.
For example, here I redirect the default stream which is STD out into the output txt file and
the error
stream I redirect into the error dot txt file.
Let's now have a look at this in our terminal.
So here we are in our terminal.
And what I'm using here now is the Du command, because I want to make sure that we have
one command
that creates an STD out and an STD error.
And Du does this if we pass in one correct file name and it will print out the file size and if
we pass
as a parameter one file that doesn't exist, then it will print out an STD error.
So I'm just using this program that we have both here like an STD out and an STD error.
So the way to go is that we could say here, for example, the normal output I want to put in or
I want
this to be written into a file, for example, output dot txt and the STD error.
I would like this to be written into the file called error dot txt.
If I now hit enter.
We see there's no output being generated here on the terminal because the output has been
redirected
into one of those files.
For example, if I print out the contents of output.txt, we can see this one here and we can see
that
the error has ended up in the other file.
This always overwrites our files because the one greater than signs overrides the file, but we
could
also append it by using this by using the double greater than sign.
And now it will be appended to our corresponding files.
So for example, here we now have those two errors in the one file here.
Another thing that we may have done here, but usually you wouldn't do this.
But we could also specify that we want to redirect standard stream one to output text so that
we want
to specify that we want to redirect the STD out into the output.txt file.
This is the default.
And usually you just we would not write this down in this way, but this would be the more
verbose way
to write this command.
There's also another way how we could use this.
For example, imagine we have a program such as this one that sometimes creates some useful
output that
we would want to use maybe later in our shell script or in our shell.
But we do not care about this output here.
If we just enter the command here on the command line, we could say, okay, whatever.
We can just ignore the second line and everything would be all right.
However, sometimes there are ways in Bash that we could have an output here and we want
to parse this
output with the next utility.
We haven't looked into this yet, but it's possible to chain commands and combine them
together and
to create completely new use cases for them.
And then if we would have an error output here, this could affect our whole script.
So even though right now, if we just use a command here, it doesn't really matter whether it's
generating
an extra STD error or not, later it will matter.
And we need to sometimes get rid of this error output so that our whole script can just
continue running.
So we need to find a way to ignore those errors.
And the way to go is that we use our error stream and we redirect this into a special device.
We can find it in the folder slash dev and the device name is Null.
This is just a null device, which means from a file system, from a program point of view, this
is
a file that we can open and write into it and it will just be discarded by the operating system.
So basically in this command here we are using the error output and we are writing this error
output
into a file slash dev null.
And this just means that everything will be discarded.
But let's have a look at this now.
So I just hit enter and we can see that the error output has been discarded.
We could, by the way, also if we wanted to discard the STD out by this and then we only get
the error
output, this would then allow us to use this output as as the input for the next step and then do
something
with that one more on that later, how we can combine those commands.
So you see, we can just take the error and redirect it to, for example, this special file to ignore
it.
Or we could also say, okay, we want to ignore the normal output and we only care about the
errors.
Or we could do something completely different, for example, something like this or
whatever, where
the errors get discarded and the actual output gets redirected into a file.
So we can really combine this, those two operators.
Is there also a way to redirect the errors so they do not show up as errors anymore in the
sense that,
okay, if a program creates an error, can we just take this error and put it on our STD out so
we can
just work with it as a normal STD out?
And there is and we are getting into that in the next lecture and why we would want to do that
in some
cases.

Redirecting stdout to stderr (Part 1)


In this lecture we will redirect STD to STD out.
Why would we want to do this?
The thing is, this allows us to easily store both outputs in the same file.
So, for example, if we invoke a program that outputs something to STD R and as well to STD
out, we
might want to redirect both outputs to the same file.
Right now, for example, if we want to start a program, a program, and we would like to
redirect
the STD out and the STD to the same file, we could do this, for example, here and we redirect
the
STD out and also the STD R into a file.
We could do this.
This would work, actually.
However, you can see we have to list the same file multiple times and that's not really
convenient.
By the way, the double greater than sign means that we want to append something to a file.
But also if we would just use the single greater than sign, then this would also work.
It would not overwrite the file twice.
It would only overwrite the file once, so both outputs would end up in the same file at least.
So right now we could achieve this if we want to have both outputs in the same file by just
listing
those two redirects here and that would work.
But you can see the file name that's like a little bit inconvenient that we have to list listed
multiple
times.
However, we could argue, okay, this works and we can cover this use case, even though the
syntax
is a little bit expressive.
However, there's one thing that we can only solve if we are able to redirect stderr to stdout.
Later I will show you how we can chain commands together that will actually be the next
chapter in this
course and you will learn about a new concept, the so-called pipes in bash and we can only
pipe std out into the next command.
So once we learn how we can combine multiple commands to do something more, then it's
really important
that the output is all on stdout at least the output that we want to pipe into the next command
if we want to combine them together.
So it's really important that we learn now how we can redirect stderr to stdout.
So how can we do this?
So if we have a command we can just use this construct here to redirect to the current std out
what the current exactly means.
We will dig into that later in this lecture so I can provide a command and here I say the STD
should be redirected into the current STD out.

Let's just have a look at the first command and how this works.
So for example, imagine here I'm in my bash and let's say I have a command here that, for
example,
here, the first image, this one exists.
So this would be a normal STD out.
And then, for example, this image here, this doesn't exist.
So this would be printed to the STD error.
We can now see both appear here in our terminal.
However, this one is STD out.
This one is STD R, The terminal doesn't show this exactly, but we know this, by the way,
how the
program works.
However, I could say, okay, let's take our STD and redirect this without this one and redirect
this
into our STD out so I could just hit enter now and we can see it's still printing exactly the
same.
However, at this point, both are being printed through the STD out channel.
This allows us the following construct.

Let me go back to my slides here.


We can have a program we can set.
Okay.
We want to redirect the program into a text file, at least the output of this program, and then
we
can say, Oh, and by the way, as well, we want to redirect the error also to our stdout file.
So here in this case we get all the output in one file because here we redirect the STD out into
that
file and here we say okay, the STD error.
We also want to be redirected into that file.
Let's try that one out.
So here I say, okay, this is my program.
Then I say, okay, this I want to be redirected into a file.
For example out dot txt here.
In this case I don't want to append it.
This one would be append here.
In this case I want to overwrite this file if it exists.
It doesn't exist as of now, but whatever.
I want to overwrite this file if it would exist and I want the STD out to be redirected into that
file.
And I also want to the std error to be redirected into that file.
So right now you see nothing is being printed here in on my command line because both are
being redirected
into the file.
And if I now just output my text file, we can see std error and STD out.

Both are here in this file and both have been redirected there.
However, I want to emphasize a small difference because I feel like if we are here in my
course, I
really like to go into the details because I'm wondering about this stuff and then I end up
researching
this for like a long time until I find something.
I was wondering, imagine, or you see this command that we have here and we see that first
this is
the STD, the normal STD out.
And then comes my error message that there is no such file or directory.
However, here in this case we have redirected both into the output.txt file, so technically we
expect
that we have exactly the same output like before.
However, if I output my file now we see that the error message is being printed here and only
then
we see the actual file size.
So I was wondering why do we have a different order here?
Why do we get the contents here first and then we get the error and here we get the error first
and
then we get the content.
I was confused because I was expecting at first that both would behave exactly the same way.
Well, it turns out that this is an implementation detail.
It may be different on different computers, but it turns out that STD out is buffered, which
means
that if a program wants to write something to STD out, it's being buffered and it's waiting for
a little
bit more data until it's then actually being displayed or being written into a file.
STD.
STD is at least on my system.
It's not buffered.
So whatever happens, it's being printed out immediately or it's being redirected immediately
into the
file.
Now the thing is the STD out.
So if something if something works, the STD out is buffered.
If we are in a terminal, it's line buffered.
So this means, for example, here, if I take this command, this is running in a terminal and the
output is being displayed in a terminal.
So our program realizes, okay, this file has a file size of 2.8MB.
So it, it, it prints out this this line and it's line buffered.
So whenever the line is complete, this line is being shown to our terminal.
So this is how this line happens to be first.
Then our disk usage program realizes, okay, this file doesn't exist and it sends this to std r and
in my on my system, std r is completely unbuffered.
So this one immediately shows up here in my terminal.
Everything works and everything is perfect.
However, if we send the output into a file because this is usually meant or we can use this for
large
amounts of data, the buffering is changing here right now because we are sending the STD
out to a file.
STD out is now no longer line buffered, but if the STD out of a program is being sent into a
file,
it's buffered.
And in my case it should be about four kilobytes of data.
So it's buffering.
It's waiting for this buffer to fill up until it's eventually written in those data.
So our disk usage program here finds our first item.
It sent into the buffer because we are not writing every line into the file directly.
We are waiting for more lines to arrive.
So this is still in our buffer.
The buffer isn't full yet.
We are still waiting for maybe like ten, 100 or 1000 more lines that we could write into our
file all
at once.
Then our error occurs and the error is completely unbuffered, so it's directly written into our
output
txt file and then our shell realizes, okay, this program is now completely or the program
realizes,
okay, I'm done now, so let's clear our buffer and actually write this data into our file.
So if I now hit enter and we output this file cat out dot txt we can see here the error appears
before
because this output has been held back because it was in a buffer waiting for more data to be
outputted.
But then no more data came.
So it was written at this point.
So the different order of the contents here in this file are actually a performance feature of my
system,
and it's nothing that we need to worry about.
I want to go a little bit more into the detail of this command here.
Why is this ordering here so important?

And because this lecture is already quite long.


We will dig into that in the next lecture.
And I will tell you or I will show you why the order of those commands is extremely relevant
here in
this case.
Redirecting stdout to stderr (Part 2)
Why is the order so extremely important when we are trying to redirect stderr?
So let's compare the following commands.
For example here, this is the first command.
This is the one that we've used before successfully.
Everything worked.
But let's just change the order and see what's happening.
If we just change the order of our redirect.
Let's have a look in our terminal.
So this did work properly.
Everything ended up in our output txt file we can open or we can list this file and we can see
here
our stderr is in this file and also our stdout.
However, I use the arrow keys up to get the last command or the the command before and I
just change
the order here and I'm like here to greater and one.
So I press enter, I call the file differently.
I call this out two dot txt.
Now we can see our stderr has been printed here to our terminal and if we open our file out
two dot
txt we can see only our stdout has been written to our out file.
Why has the stderr been written here to our terminal?
And why is this text file only containing our normal output and not our error message as
well?

This is because the order of those redirects is extremely important.


Let's have a look at our cases.
So this was our first case.
In this case, we had our command, which may produce some STD error and some STD out.
And the way it works as a first step stdout is redirected to our file out dot txt and then as a
second
step stderr is redirected to the same destination as our current stdout when this is being
parsed.
So as of this point the std out points to the file out dot txt.
So also our std r points to our file or to our out dot txt file.
So at the end with this, if we use this command, both outputs redirects their content into the
file
out dot txt.
I've prepared a small visualization for you, so at the beginning our command both outputs go
to our
terminal one STD out to our terminal and the other std r also goes to our terminal.
Then this is being parsed and our STD out is being changed to our file.
And then when the full command is being parsed, then it's like this.
It checks what's the destination of our STD out.
And this will then also be the destination of our STD.
R So if we are using this command, both will end up in our same or in the same file.
Let's have a look at the other case.
So this was our command.
And in this case, at first stderr is redirected to the same destination as our current stdout.
So meaning stderr is being redirected to the terminal.
All errors will show up there and then as a second step stdout is being redirected to the file
stdout.
But this only affects the stdout of our program so it doesn't affect the redirect that has
happened
here before.
So only the stdout from our program will then be redirected to output.txt.
So we can imagine it a little bit like this.
We declare some mappings here that should happen and then the command is being executed
and those mappings
are just being applied.
It's not like it's first doing this and then doing this.
That's not the way it works.
We are we are just declaring those mappings in a specific order and then they are being
executed.
So if we want to visualize this, I've also prepared a visualization for this.
So at the beginning our command both prints stdout and stderr to our terminal.
Then we say okay, if we include this here, this creates a mapping.
So the stderr now goes to our stdout terminal.
So we have changed our stderr and then if we parse the command completely then we also
get to this redirection
here and our stdout is being redirected to our file but our stderr still points to the stdout in our
terminal.
So this is why we have seen the following behavior here when we had this program here that
creates an
stdout and an stderr and we use the wrong order here.
Then we see.
Okay, the stderr ends up in our terminal and only the normal output ends up in our file.
So when you use this kind this way to redirect stuff or to redirect outputs in our terminal, then
you
need to make sure that this is always the last command or the last redirect that you apply
here.
Otherwise it may not work as you would expect.
We have now seen a lot about redirects.
However, there was one more thing.
Do you remember?
There was also this standard communication stream with the number zero.
In and we can also use this for user input.
And in order to show you that I've prepared the next lecture for you where we will dig into
the details
for that one.

You might also like