Lpic Lab
Lpic Lab
4 Step 4
The next command displays information about the current system. To be able to
see the name of the kernel you are using, type the following command into the
terminal:
uname
sysadmin@localhost:~$ uname
Linux
Many commands that are executed produce text output like this. You can
change what output is produced by a command by using options after the name
of the command.
Execute the uname command again twice in the terminal, once with the option -
n and again with the option --nodename. This will display the network node
hostname, also found in the prompt.
uname -n
uname --nodename
sysadmin@localhost:~$ uname -n
localhost
sysadmin@localhost:~$ uname --nodename
localhost
5.2.4 Step 4
The next command displays information about the current system. To be able to
see the name of the kernel you are using, type the following command into the
terminal:
uname
sysadmin@localhost:~$ uname
Linux
Many commands that are executed produce text output like this. You can
change what output is produced by a command by using options after the name
of the command.
Execute the uname command again twice in the terminal, once with the option -
n and again with the option --nodename. This will display the network node
hostname, also found in the prompt.
uname -n
uname --nodename
sysadmin@localhost:~$ uname -n
localhost
sysadmin@localhost:~$ uname --nodename
localhost
5.2.5 Step 5
The pwd command is used to display your current "location" or current
"working" directory. Type the following command to display the working
directory:
pwd
sysadmin@localhost:~$ pwd
/home/sysadmin
sysadmin@localhost:~$
As you can see from the output of the command, /home/sysadmin, Linux uses the
forward slash / to separate directories to make what is called a path. The
initial forward slash represents the top-level directory, known as the root
directory. More information regarding files, directories and paths will be
presented in later labs.
The tilde ~ character that you see in your prompt is also indicating what the
current directory is. This character is a "shortcut" way to represent your home.
Consider This
pwd stands for "print working directory". While it doesn't actually "print" in
modern versions, older UNIX machines didn't have monitors so the output of
commands went to a printer, hence the somewhat misleading name of pwd.
5.3.1 Step 1
Execute a new command and then execute the history command:
echo Hi
history
Remember
The date command will print the time and date on the system.
The clear command clears the screen.
Your output should be similar to the following:
sysadmin@localhost:~$ history
1 ls
2 ls -l
3 ls -l /tmp
4 whoami
5 uname
6 uname -n
7 uname --nodename
8 pwd
9 echo Hi
10 history
sysadmin@localhost:~$
Your command numbers may differ from those provided above. This is because
you may have executed a different number of commands since opening the
virtual terminal.
5.3.2 Step 2
To view a limited number of commands, the history command can take a
number as a parameter to display exactly that many recent entries. Type the
following command to display the last five commands from your history:
history 5
sysadmin@localhost:~$ history 5
7 uname --nodename
8 pwd
9 echo Hi
10 history
11 history 5
5.3.3 Step 3
To execute a command again, type the exclamation point and the history list
number. For example, to execute the 9th command in your history list, you would
execute the following:
!9
sysadmin@localhost:~$ !9
echo Hi
Hi
The focus of this section is to learn how to display the values of shell variables.
5.4.1 Step 1
The echo command can be used to print text and the value of a variable, and to
show how the shell environment expands metacharacters (more on
metacharacters later in this lab). Type the following command to have it output
literal text:
5.4.2 Step 2
Environment variables are available system-wide. The system automatically
recreates environment variables when a new shell is opened. Examples include
the PATH, HOME, and HISTSIZE variables. The HISTSIZE variable defines how
many previous commands to store in the history list. In the example below, the
command will display the value of the HISTSIZE variable:
sysadmin@localhost:~$ echo $HISTSIZE
1000
sysadmin@localhost:~$
5.4.3 Step 3
Type the following command to display the value of the PATH variable:
echo $PATH
This variable is used to find the location of commands. Each of the directories
listed above are searched when you run a command. For example, if you try to
run the date command, the shell will first look for the command in
the /home/sysadmin/bin directory and then in the /usr/local/sbin directory
and so on. Once the date command is found, the shell "runs it”
5.4.4 Step 4
Use the which command to determine if there is an executable file, in this case
named date, that is located within a directory listed in the PATH value:
which date
type vi
cd /bin
type vlc
cd
sysadmin@localhost:~$ type vi
vi is /usr/bin/vi
sysadmin@localhost:~$ cd /bin
sysadmin@localhost:/bin$ type vlc
-bash: type: vlc: not found
sysadmin@localhost:/bin$ cd
sysadmin@localhost:~$
5.5.4 Step 4
The final command type is the executable program. These commands invoke
programs installed on the system which perform specific tasks. When a user
types the vi command, the shell uses the PATH file to locate and execute the
program. Programs like vi are available on just about every Linux distribution;
other programs, like vlc (an open source media player often used on Linux
desktops), are installed by users or administrators for a specific purpose and will
not be listed in the PATH unless they have been installed separately.
type vi
cd /bin
type vlc
cd
sysadmin@localhost:~$ type vi
vi is /usr/bin/vi
sysadmin@localhost:~$ cd /bin
sysadmin@localhost:/bin$ type vlc
-bash: type: vlc: not found
sysadmin@localhost:/bin$ cd
sysadmin@localhost:~$
5.6 Quoting
There are three types of quotes used by the Bash shell: single quotes ('), double
quotes (") and back quotes (`). These quotes have special features in the Bash
shell as described below.
To understand single and double quotes, consider that there are times that you
don't want the shell to treat some characters as "special". For example, as you
learned earlier in this lab, the * character is used as a wildcard. What if you
wanted the * character to just mean an asterisk?
Single quotes prevent the shell from "interpreting" or expanding all special
characters. Often single quotes are used to protect a string (a sequence of
characters) from being changed by the shell, so that the string can be
interpreted by a command as a parameter to affect the way the command is
executed.
Double quotes stop the expansion of glob characters like the asterisk (*),
question mark (?), and square brackets ( [ ] ). Double quotes do allow for both
variable expansion and command substitution (see back quotes) to take place.
When using quotes, they must be entered in pairs or else the shell will not
consider the command complete.
While single quotes are useful for blocking the shell from interpreting one or
more characters, the shell also provides a way to block the interpretation of just
a single character called "escaping" the character. To "escape" the special
meaning of a shell metacharacter, the backslash \ character is used as a prefix
to that one character.
5.6.1 Step 1
Execute the following command to use back quotes ` (found under
the ~ character on some keyboards) to execute the date command within the
line of the echo command:
echo Today is `date`
5.6.2 Step 2
You can also place $( before the command and ) after the command to
accomplish command substitution:
Why two different methods that accomplish the same thing? Backquotes look
very similar to single quotes, making it harder to "see" what a command is
supposed to do. Originally, shells used backquotes; the $(command) format was
added in a later version of the Bash shell to make the statement more visually
clear.
5.6.3 Step 3
If you don't want the backquotes to be used to execute a command, place single
quotes around them. Execute the following:
5.6.5 Step 5
Double quote characters don't have any effect on backquote characters. The
shell will still use them as command substitution. Execute the following to see a
demonstration:
5.6.6 Step 6
Double quote characters will have an effect on wildcard characters, disabling
their special meaning. Execute the following:
echo D*
echo "D*"
sysadmin@localhost:~$ echo D*
Desktop Documents Downloads
sysadmin@localhost:~$ echo "D*"
D*
sysadmin@localhost:~$
Important
Quoting may seem trivial and weird at the moment, but as you gain more
experience working in the command shell, you will discover that having a good
understanding of how different quotes work is critical to using the shell.
The simplest separator is the semicolon (;). Using the semicolon between
multiple commands allows for them to be executed one right after another,
sequentially from left to right.
To see how these control statements work, you will be using two special
executables: true and false. The true executable always succeeds when it
executes, whereas, the false executable always fails. While this may not
provide you with realistic examples of how && and || work, it does provide a
means to demonstrate how they work without having to introduce new
commands.
5.7.1 Step 1
Execute the following three commands together separated by semicolons:
As you can see the output shows all three commands executed sequentially:
sysadmin@localhost:~$ echo Hello; echo Linux; echo Student
Hello
Linux
Student
sysadmin@localhost:~$
5.7.2 Step 2
Now, put three commands together separated by semicolons, where the first
command executes with a failure result:
Note that in the previous example, all three commands still executed even
though the first one failed. While you can't see from the output of
the false command, it did execute. However, when commands are separated by
the ; character, they are completely independent of each other.
5.7.3 Step 3
Next, use logical "and" to separate the commands:
5.7.4 Step 4
Use logical "and" with a command that fails as shown below:
5.7.5 Step 5
The "or" characters separating the following commands demonstrates how the
failure before the "or" statement causes the command after it to execute;
however, a successful first statement causes the command to not execute:
7.2.1 Step 1
Type the following command to print the working directory:
pwd
sysadmin@localhost:~$ pwd
/home/sysadmin
sysadmin@localhost:~$
When you first open a terminal window, you will be placed in your home
directory. This is a directory where you have full access and other users
normally have no access by default. To see the path to your home directory, you
can execute the following command to view the value of the HOME variable:
echo $HOME
sysadmin@localhost:~$ echo $HOME
/home/sysadmin
sysadmin@localhost:~$
7.2.2 Step 2
You can use the cd command with a path to a directory to change your current
directory. Type the following command to make the root directory your current
working directory and verify with the pwd command:
cd /
pwd
sysadmin@localhost:~$ cd /
sysadmin@localhost:/$ pwd
/
sysadmin@localhost:/$
7.2.3 Step 3
To change back to your home directory, the cd command can be executed
without a path. Change back to your home directory and verify by typing the
following commands:
cd
pwd
sysadmin@localhost:/$ cd
sysadmin@localhost:~$ pwd
/home/sysadmin
sysadmin@localhost:~$
7.2.4 Step 4
The cd command may be entered with a path to a directory specified as
an argument. Execute the cd command with the /home directory as an
argument by typing the following:
cd /home
pwd
sysadmin@localhost:~$ cd /home
sysadmin@localhost:/home$ pwd
/home
sysadmin@localhost:/home$
7.2.5 Step 5
Change back to your home directory, using the cd command with the tilde ~ as
an argument:
cd ~
pwd
sysadmin@localhost:/home$ cd ~
sysadmin@localhost:~$ pwd
/home/sysadmin
sysadmin@localhost:~$
If either no other characters or a forward slash follows the tilde, then it will
expand to the home directory of the user currently active in the shell.
If a user name immediately follows the tilde character, then the shell will expand
the tilde and user name to the home directory of that user name. For
example, ~bob would be expanded to /home/bob.
Paths that start with a tilde are considered absolute paths because after the
shell expands the tilde path, an absolute path is formed.
7.2.6 Step 6
Use the echo command below to display some other examples of using the tilde
as part of the path:
7.2.7 Step 7
Attempt to change to the home directory of the root user by typing the
following command:
cd ~root
sysadmin@localhost:~$ cd ~root
-bash: cd: /root: Permission denied
sysadmin@localhost:~$
cd /usr/bin
pwd
sysadmin@localhost:~$ cd /usr/bin
sysadmin@localhost:/usr/bin$ pwd
/usr/bin
sysadmin@localhost:/usr/bin$
7.2.9 Step 9
Use an absolute path to change to the /usr directory and display the working
directory by issuing the following commands:
cd /usr
pwd
sysadmin@localhost:/usr/bin$ cd /usr
sysadmin@localhost:/usr$ pwd
/usr
sysadmin@localhost:/usr$
7.2.10 Step 10
Use an absolute path to change to the /usr/share/doc directory and display the
working directory by issuing the following commands:
cd /usr/share/doc
pwd
sysadmin@localhost:/usr$ cd /usr/share/doc
sysadmin@localhost:/usr/share/doc$ pwd
/usr/share/doc
sysadmin@localhost:/usr/share/doc$
With relative pathnames you provide "directions" of where you want to go from
the current directory. The following examples will illustrate using relative
pathnames.
7.2.11 Step 11
Using a relative path, change to the /usr/share/doc/bash directory and display
the working directory by issuing the following commands:
cd bash
pwd
sysadmin@localhost:/usr/share/doc$ cd bash
sysadmin@localhost:/usr/share/doc/bash$ pwd
/usr/share/doc/bash
sysadmin@localhost:/usr/share/doc/bash$
Note
If there wasn't a bash directory under the current directory, the previous
command would fail.
7.2.12 Step 12
Use a relative path to change to the directory above the current directory:
cd ..
pwd
sysadmin@localhost:/usr/share/doc/bash$ cd ..
sysadmin@localhost:/usr/share/doc$ pwd
/usr/share/doc
sysadmin@localhost:/usr/share/doc$
7.2.13 Step 13
Use a relative path to change up one level from the current directory and then
down into the dict directory:
cd ../dict
pwd
sysadmin@localhost:/usr/share/doc$ cd ../dict
sysadmin@localhost:/usr/share/dict$ pwd
/usr/share/dict
sysadmin@localhost:/usr/share/dict$
7.3.1 Step 1
To list the contents of the current directory, use the ls command:
cd
ls
sysadmin@localhost:/usr/share/dict$ cd
sysadmin@localhost:~$ ls
Desktop Documents Downloads Music Pictures Public Templates Videos
sysadmin@localhost:~$
In the output of the previous ls command, the file names were placed in a light
blue color. This is a feature that many distributions of Linux automatically
provide through a feature called an alias (more on this feature in a later lab).
The color indicates what type the item is. The following table describes some of
the more common colors:
7.3.2 Step 2
Not all files are displayed by default. There are files, called hidden files, that are
not displayed by default. To display all files, including hidden files, use the -
a option to the ls command:
ls -a
sysadmin@localhost:~$ ls -a
. .bashrc .selected_editor Downloads Public
.. .cache Desktop Music Templates
.bash_logout .profile Documents Pictures Videos
sysadmin@localhost:~$
The names of hidden files begin with a period (a dot character). Typically these
files and often directories are hidden because they are not files you normally
want to see.
7.3.3 Step 3
By itself, the ls command just provided the names of the files and directories
within the specified (or current) directory. Execute the following command to
see how the -l option provides more information about a file:
ls -l /etc/hosts
So, what does all of this extra output mean? The following table provides a brief
breakdown of what each part of the output of ls -l means:
7.3.4 Step 4
Sometimes you want to see not only the contents of a directory, but also the
contents of the subdirectories. You can use the -R option to accomplish this:
ls -R /etc/udev
sysadmin@localhost:~$ ls -R /etc/udev
/etc/udev:
rules.d udev.conf
/etc/udev/rules.d:
70-persistent-cd.rules README
sysadmin@localhost:~$
7.3.5 Step 5
You can use file globbing (wildcards) to limit which files or directories you see.
For example, the * character can match "zero or more of any characters" in a
filename. Execute the following command to display only the files that begin
with the letter s in the /etc directory:
ls -d /etc/s*
sysadmin@localhost:~$ ls -d /etc/s*
/etc/securetty /etc/sgml /etc/shells /etc/ssl /etc/sysctl.conf
/etc/security /etc/shadow /etc/skel /etc/sudoers /etc/sysctl.d
/etc/services /etc/shadow- /etc/ssh /etc/sudoers.d /etc/systemd
sysadmin@localhost:~$
Note that the -d option prevents files from subdirectories from being displayed.
It should always be used with the ls command when you are using file globbing.
7.3.6 Step 6
The ? character can be used to match exactly 1 character in a file name.
Execute the following command to display all of the files in the /etc directory
that are exactly four characters long:
ls -d /etc/????
sysadmin@localhost:~$ ls -d /etc/????
/etc/bind /etc/init /etc/motd /etc/perl /etc/skel
/etc/dpkg /etc/ldap /etc/mtab /etc/sgml /etc/udev
sysadmin@localhost:~$
7.3.7 Step 7
By using square brackets [ ] you can specify a single character to match from
a set of characters. Execute the following command to display all of the files in
the /etc directory that begin with the letters a, b, c or d:
ls –d /etc/[abcd]*
sysadmin@localhost:~$ ls -d /etc/[abcd]*
/etc/adduser.conf /etc/blkid.conf /etc/cron.weekly
/etc/adjtime /etc/blkid.tab /etc/crontab
/etc/alternatives /etc/ca-certificates /etc/dbus-1
/etc/apparmor.d /etc/ca-certificates.conf /etc/debconf.conf
/etc/apt /etc/calendar /etc/debian_version
/etc/bash.bashrc /etc/cron.d /etc/default
/etc/bash_completion.d /etc/cron.daily /etc/deluser.conf
/etc/bind /etc/cron.hourly /etc/depmod.d
/etc/bindresvport.blacklist /etc/cron.monthly /etc/dpkg
sysadmin@localhost:~$
8.1 Introduction
This is Lab 8: Managing Files and Directories. By performing this lab, students
will learn how to navigate and manage files and directories.
8.2 Globbing
The use of glob characters in Linux is similar to what many operating systems
refer to as "wildcard" characters. Using glob characters, you match filenames
using patterns.
Glob characters are a shell feature, not something that is particular to any
specific command. As a result, you can use glob characters with any Linux
command.
When glob characters are used, the shell will "expand" the entire pattern to
match all files in the specified directory that match the pattern.