Linux 2
Linux 2
Linux 2
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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 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.
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.
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.
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.
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.
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.
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.
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.
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.
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
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.
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.
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
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.
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.
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.
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.
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.
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?
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.
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
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.
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.
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.
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.
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.
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?