Now, let's get this straight right from the start,
shall we? The very,
very best way for you to learn is to you to get the systems up and running
so you can run through these demonstrations as well.
We're going to be taking a look at working with both files and directories.
We'll take a quick look at the Filesystem Hierarchy Standard, so why most
Linux systems will look very similar when we look at their directory
structure. We'll see how we can change directories,
create directories, list directory and file metadata,
delete directories. And here we are,
a command line environment and a command line
environment builds upon Alma Linux 8.5.
Now rather than using Red Hat Enterprise Linux 8.5, we're
using Alma Linux 8 as it's a Red Hat rebuild.
It really takes over from where CentOS left off, and that becomes a free‑to‑use
alternative to Red Hat Enterprise Linux 8. Where we had previously used CentOS,
CentOS now tracks in front of or ahead of Red Hat Enterprise Linux 8, so it
works really as a development platform.
Alma Linux 8 is a free download and is free to use without a subscription.
You can see I've also got tabs open for openSUSE, and
that's openSUSE 15.2, and Ubuntu 20.04.
Now one of the very first things that we might try out is the use of the
tty command to understand the terminal that we've connected to. Typing the
tty command and then Enter, we can see that we're presented with a file,
name, and path, so /dev, for device directory, pts for pseudo terminal, and
0 for pseudo terminal 1.
Now this is just a file within our filesystem.
We can use the ls, or list, command with the option ‑l to be able to go
through and take a look at our file, ‑l will give me the metadata, as we
can see as we print this. Now we should also all be about shortcuts and
making the command line work for us.
If I use ls ‑l and then use the $(, I can look at the output of the tty
command and pass that through to ls ‑l. So I can do everything in one
command rather than two separate commands.
Now as we take a look at this metadata, in column 1, we can see our
file type, as a c means it's a character device,
a terminal.
Next, in column 2, we have 9 bits that represent our file
permissions or what's known as our file mode.
When we look at column 3, we see the number 1 here.
Column 3 is our hard link count, so how many names we have
pointing through to our files. And again, there'll be more
details on that as we work our way through.
When we take a look at column 4, we're looking at the user owner.
That's the user, vagrant, in this case. Now in column 5, we see
the group owner, tty, in this case.
Next, we then go through and take a look at the file
size. File size here is 136 bytes.
Column 7 will display the last modified time, and then we look
in column 8 at the actual file name itself.
Now these terminals are device files, hence they're in the dev directory, /dev.
Now every directory in Linux does have a purpose, and these directories
and the structure that we look at are going to be consistent
irrespective of the distribution that we look at.
To help demonstrate the Filesystem Hierarchy Structure,
the consistency that we have across Linux, I can use the tree command.
Now you might run it as I've done here, and we're just getting a
report that the tree command is not found,
so it's not installed. We can install it, and we're quite lucky that
the tree command actually comes within a package called tree. So I
can elevate my privileges using sudo, and then dnf is my software
installer that we have on Alma Linux 8, dnf = Dandified YUM, and then
install ‑y and tree.
Now we're not always going to be as lucky of having the
same package name as the command name,
but it's an easy illustration, in this case. But there'll be more on software
management as we go through different modules and courses within this track.
And now that I do have the tree command installed,
I can show you the root of our filesystem. ‑L will allow me to
specify the level. I only want to go down one level. And taking
a look at the root filesystem,
the top of our filesystem hierarchy, we can then go through
and see the directories that we have.
I could run the same command across openSUSE and
Ubuntu, and it would look very similar.
The idea is that we should be able to recognize the
directories and their purpose.
So the bin directory is user binaries, so it's not a trash can.
It's a binary directory, user programs.
The boot directory is where we are going to find the kernel. The dev
directories, we've already talked about. We're going to find device files.
Our etc directory is our server configuration directory.
We have things like the home directory where user home directories are
going to be. We can see as we move further down,
we have directories like the sbin, which is our
supervisory binary, our root programs.
And moving further down,
we can see directories like var where we would have variable
data like mail spool files and log files.
We did see earlier our device file for our terminal, but we can look
inside of our dev directory, and let's ‑l and take a look inside of
/dev. It's going to list all of the device files located inside the
directory, dev. If I use the tree command, we're going to be able to
navigate into the dev directory, as well as all of the subdirectories
inside of dev.
So many of our device files are organized in subdirectories.
If I want to look at the directory itself,
I can use ls ‑ld for directory and /dev. Here, when we look at our metadata,
we see the file type is d for directory.
We then can see our permissions. When we take a look at the hard link
count, this is set to 18. This is an indication of how many
subdirectories we have within the dev directory structure.
The ownership is root and root.
We can go through and see the file size, and we can
look at the date it was created.
Where we have the execute permission to a directory, we can
move into the directory with the cd command.
Pwd will print our working directory.
Cd by itself actually takes us to our home directory. Again,
we can see that with the pwd, print working directory.
We've also got shortcuts that are built in. If I use cd ‑, we
actually move to our old working directory, and that's
maintained by a variable in the system.
We can read our variables using the echo command, and $OLDPWD in uppercase
will show me the directory that I was previously in. I can use cd ‑, or I
could cd to the value of the variable, so cd $OLDPWD.
But it just gives you an idea, again, of some of those shortcuts and how we
can make life easy for ourselves at the command line.
And working in my home directory,
I can go through and create a new directory with the mkdir command. Again,
concentrating on the shortcut, if I want to then list that directory,
I can use ls ‑ld, and escape and dot brings up the last argument.
When we go through and take a look at this,
we're, again, seeing our metadata, d for directory,
we see our permissions.
We've got a hard link count of 2, representing, then that we've got the
directory and no additional subdirectories. In each directory, we also
have a file called dot that points to the current directory. So test/.
is pointing back to the test directory, hence then that we have a hard
link count of 2, the number of names.
We've got the test directory and test/. all pointing to exactly the same file.
If I want to create parent directories as well,
I can use mkdir ‑p and then test/d1/d2.
Returning to my valuable tree command, we can now list the contents of the
test directory recursively with tree and see, then that we've got d2 created
inside of d1. But we're not stuck with just this.
If I wanted to go through and create many directories,
I could create mkdir test and then go through and put dir, and
then decide we could have 1 through to, well,
I've already got a directory 1,
so why don't we have directory 10 through to directory 19? And we're
using the brace brackets there to encapsulate it.
Now as we go through and take a look at our tree command, tree and test,
we can see, then that I've got the directories d10,
d11, d12, and so forth.
Now having created those directories,
I can delete them in one go by deleting the top‑level directory test with rm ‑rf.