In this video, I’ll provide an introduction to the shell and command-line syntax on Oracle
Linux. You’ll learn what a shell is, what information
the shell prompt provides, the structure of a command, how to view the manual pages, as
well as handy shortcuts like tab completion, recalling a command, and canceling a command.
Many of the applications we’re familiar with use a GUI, or graphical user interface,
to allow you to interact with the software. But not all programs require this and instead
they use a CLI, or command line interface, which accepts text-based input and provides
text-based output. In Linux, you’ll often be working from the
command line to execute commands and interface with the operating system and the environment
you’ll do this in, is called the shell. The default shell used with Oracle Linux is
Bash. You can use other shells, but Bash is one
of the most common and a good place to start. Let’s take a look at the structure of the
shell prompt by opening a terminal on your system.
If you’re working with a Linux system that has a GUI, you can access the terminal by
right-clicking on the desktop and selecting Open Terminal.
If your system doesn’t have a GUI, the first thing you’ll see when you log in, is the
terminal, so you’ll already be looking at the shell prompt.
The prompt shows us some important information. It has the user name that we’re logged in
as and the name of the system that we’re on.
Followed by the name of the directory we’re currently working in.
In this case, it shows a ~, which means we’re in the home directory of this user.
I won’t go into details about the home directory or Linux file structure in this video, and
don’t worry about it too much for now, but check out the video on working with files
and directories for more info. The prompt will also have a dollar sign if
you’re logged in as a normal user. Or, if I switch to a different terminal session,
it may have a hash sign if you’re logged in as an administrative user.
In Linux, the admin user is called root. This is important to note because there are
certain commands you can only run as root. In documentation, you’ll often see a command
prefixed with either the $ to indicate a normal user can run it or the # sign to indicate
that you must run the command as the root user.
So keep an eye out for these two symbols. That covers the basics of the prompt.
Now let’s take a look at the structure of a command.
The general structure you’ll see is: Command, Options, Arguments.
You’ll always have a command, this is what tells the program which function to perform.
Then depending on the command, you might also have one or more options, one or more arguments,
or you might have both options and arguments. It really depends on the command and what
you’re trying to accomplish. For example, I might use ls –l space Downloads.
Where ls is the command, the –l is the option, and Downloads is the argument.
And if I hit enter, it returns the results of that command.
But what if I don’t know what a particular command does or how to use it?
Turns out there’s built in documentation we can look at, called manual pages (or man
pages for short). It will show us more details about the structure
of the command, the options available, and the arguments that we can use.
To view the man pages, type man, space, and then the command.
In this case, let’s look at the ls command I just ran.
So I’ll type man, space, ls. In some cases a command may not have a man
page, so instead you can usually get similar information by entering the command, a space,
and then - - help. ls does have a man page so let’s take a
look at it by hitting enter. At the top here, it gives the command name
and a brief description, telling us it lists directory contents.
Then the synopsis section shows us the structure we can use to execute the command. So the
command ls can be followed by an option and then it can also take an argument, in this
case the name or path of a file. The syntax here is giving us a little more
information than might be obvious initially. The square brackets mean that this value is
optional. So I can run this command using an option,
but I don’t have to. Same with the argument, I can run it with
or without specifying a file. Since both these are optional, this is telling
us we can also just run the ls command by itself and it’ll work.
The ellipses tells us that you can use multiples of these.
So you can run it with multiple options and multiple files specified.
So what are the options we can use? Down here, you’ll see items with a dash
and a double dash. These are the options we can use.
The single dash followed by a letter, is a shorthand version of the option.
So you can use –a or –all and it would do the same thing.
I used –l in the demo earlier so let’s take a look at that one.
You can page down by hitting the f key. And real quick if you need to go back, hit
b and it will page up. So here’s the –l option, which says use
a long listing format. Normally ls just provides the name of the
files and directories and doesn’t give any more information.
But the –l option will provide details like the size of the file, permissions, the date
it was modified, and so on. If I page down, you’ll notice some of the
options have this = sign next to them. That means this option takes an argument.
So if I use the --sort option, then I’d need to also tell it how to sort, such as
by size or time. To exit out of the man pages, hit q.
Let’s try running the ls command now that we know a little more about it.
You can run ls alone and it'll just list the files and directories.
I can also run it with multiple options, so let’s try –l for long listing, and a for
all which will show me hidden files. Note here that I’ve grouped shorthand options
together, so I don’t have to do ls –l –a.
I can, and it’ll provide the same results, but it’s quicker to group them together.
However, you can’t group long versions together, so any dash dash option you have to listed
separately. For example, I can run ls –l –all –sort
space size, and I get results. But I can’t run ls –l –allsort space
size. It thinks I’m trying to use an option allsort
which doesn’t exist. So be sure to keep the dash dash options separate.
There are a couple more syntax things within the man pages I want to point out.
Let’s look at the man page for a command called fallocate.
The synopsis here is much more complex. This has some syntax we didn’t see with
the ls command. Note these options are in square brackets,
meaning they’re optional, but they’re also separated by pipes which are these vertical
lines. The pipe mean OR, so you can use –c or –p
or –z. You can pick one of these, but you can’t
use them all at the same time. This command also has options and arguments
that aren’t in brackets, meaning those are required, so you cannot run fallocate alone,
you have to add in these other values. If you remember, ls had just one version of
the command listed, this has three. Which is telling me I can run fallocate in
any of these three ways. Let’s exit with q and I’ll show you a
couple other useful things about the command line.
A handy trick is using tab completion. Which means if I start typing something and hit
tab, it will try to autocomplete it with something valid.
Let’s say I want to look at what’s in the Photos directory.
I can do ls, space, and a capital P, then hit tab and it’ll complete the rest.
This is case sensitive though, so if I do ls with a lower case p and hit tab, nothing
happens because there is no file or directory here that starts with a lowercase p.
Now maybe I want to look at what’s in the Documents directory.
So if I do ls, space, capital D, and then hit tab.
It’s only going to give me Do because we have two possibilities that both start with
D. And to see these possibilities, I can hit
tab twice. It shows the Documents and Downloads directories.
So if I type c and then tab, now it knows which one I want.
You can also use tab to look up commands you can’t quite remember.
Say I want to add a user but I don’t remember the exact command.
I’m pretty sure it starts with add. I can type a d d and then hit tab twice and
it will list all the possible commands. And here’s the one I’m looking for.
Another handy thing to know is using the arrows keys to recall a previous command that you
typed. So if I hit the up arrow, it’ll provide
the last command I used and I can keep hitting up to go farther back.
The down arrow cycles the other direction. And finally, let’s say I’ve started typing
a command, but I made a mistake or I don’t want to run it, instead of deleting, I can
use ctrl-c to cancel. It won’t run the command, it’ll just place
me into a new empty prompt. That covers the basics when using the command
line. I hope it gives you some confidence to run
commands and explore on your own. Thanks for watching