Shell Scripting
Shell Scripting
You can interact with the Linux shell by entering commands into the Linux terminal, which is also
commonly referred to as the command line or the command prompt.
In this case, the present working directory is /home/project, as indicated by the blue text. The dollar
sign $ following it is called the command prompt.
Recall that a terminal window is a simple user interface that allows you to run any commands that
you would like, simply by typing the command on your keyboard and hitting Enter. Many commands
will respond by returning some sort of output, which by default appears as text in your terminal
window.
Let's see how this works using a couple of the most common Linux commands,
the pwd and ls commands:
Here we've entered the pwd command, which prints the path name for our present working
directory on the next line. You can see that the command prints what we expected, which is the path
to the present working directory, /home/project. Notice also that the command prompt shows up
again on the following line, awaiting your next command.
Great! Now, how do you see what's inside your present working directory?
You can use the ls command to list the contents of the directory you are currently working in. At the
moment, the /home/project directory is brand new and doesn't contain anything yet, so entering
the ls command will return nothing:
Being a conservative program, ls won't bother with printing a blank line to express that there is
nothing to list.
Let's see if we can find a directory that already contains content. You can list the contents of any
directory with the ls command by specifying the directory name you'd like to explore.
You can see that the directory /home contains two objects, namely project and theia.
Tip: Think of a directory as a folder that contains files and subdirectories. In this
case, project and theia are subdirectories of /home. Subdirectories can contain additional files and
subdirectories. You'll learn more about exploring subdirectories in later labs.
Notice the naming convention for a directory's path: /home/project indicates that
the project directory is a subdirectory of /home. The path for the theia subdirectory would similarly
be /home/theia.
Like a tree, your Linux file system has a root directory (/, called "slash") from which your entire Linux
file system branches out.
One important subdirectory of your root directory is home. You can see this for yourself by
entering ls / to list the contents of /:
Summary
You can interact with the Linux shell by entering commands into the Linux terminal
The pwd command prints the path name to the present working directory
Tab completion allows you to autocomplete a command you're typing on the command line.
Pictures
Videos
Documents
Downloads
python-examples
Tab completion:
In this exercise, you will use tab completion to quickly enter a command to navigate to
the Pictures directory.
If you type:
1. ~ $ cd P
and press Tab, the shell will autocomplete the command to:
1. ~ $ cd Pictures/
because the Pictures directory is the only directory within your current folder that starts with a "P".
If you type:
1. ~ $ cd Do
and press Tab, nothing will happen because your current directory contains more than one directory
that starts with "Do". The shell won't know whether to autocomplete
with Documents or Downloads.
1. ~ $ cd Doc
1. ~ $ cd Documents/
because in this case, there's only one directory that starts with "Doc," Documents.
If you press Tab again, the shell will further autocomplete to:
1. ~ $ cd Documents/python-examples/
because the folder python-examples is the only existing file within the ~/Documents directory. With
just a few clicks, you can quickly autocomplete longer paths in your command line.
Command History
Command history allows you to navigate previous commands you've entered using the Up
Arrow and Down Arrow keys.
Let's say you've created a Python script called hello_world.py in your python-examples directory that
simply prints Hello, World! when you run it.
Consider you have just entered the following sequence of commands, and the shell is awaiting your
next input:
1. ~ $ cd ~/Documents/python-examples
3. Hello, World!
4. ~/Documents/python-examples $ cd /
5. / $
In the following exercises, you will learn how to rerun a previous command withut having to retype it
by pressing the Up Arrow key.
If you press the Up Arrow key once, the shell will automatically insert the last command you entered:
1. ~ $ cd ~/Documents/python-examples
2. Hello, World!
3. ~/Documents/python-examples $ cd /
4. / $ cd /
Notice that the last command you entered was cd /, which has been automatically inserted onto the
command line for you.
If you press the Up Arrow key two additional times (so, three times total), the shell will automatically
insert the command you ran three commands ago:
1. ~ $ cd ~/Documents/python-examples
3. Hello, World!
4. ~/Documents/python-examples $ cd /
5. / $ cd ~/Documents/python-examples
In this case, the command you ran three commands ago was cd ~/Documents/python-examples.
Note that the line of printed output, Hello, World!, does not count as a command. Command history
will only take you through commands you entered, not every line that is visible in the terminal.
Tip: If you click the Up Arrow key too many times, you can use the Down Arrow key to cycle through
your command history in the opposite direction.
Press Enter to return to your ~/Documents/python-examples directory. You should see something
like the following:
1. ~ $ cd ~/Documents/python-examples
3. Hello, World!
4. ~/Documents/python-examples $ cd /
5. / $ cd ~/Documents/python-examples
6. ~/Documents/python-examples $
Summary
In this reading, you learned how to:
Opening a terminal
When you first connect to the lab environment, there may already be an open terminal
for you to use. If not, open a new terminal by clicking the menu bar and
selecting Terminal > New Terminal.
This will open a new terminal at the bottom of the screen.
In your newly opened terminal, you will run the commands provided in the following
exercises. You can copy the code to your clipboard by clicking the copy button on the
bottom right of each codeblock, then pasting it to the command line. However, it will
be more beneficial to type the code yourself, so you can start building some
"command muscle memory".
In this exercise, you will learn how to browse the content of directories using the ls command.
Typing and entering ls will display all files and directories contained within your current working
directory:
1. ls
Your default current working directory in this lab environment, the /home/project directory, is
empty. Thus entering ls into the command line will not return anything yet.
ls [PATH TO DIRECTORY]
For example
1. ls /
will show the contents of 'slash', your Linux system's root directory.
Recall some of the standard subdirectories of 'slash' that you've learned about previously:
Directory Contains
System commands,
/bin
also called binaries
System administration
/sbin
binaries
Removable media
/media
device directories
1. ls /bin
Notice that one of the files in the /bin directory is called "ls". Indeed, when you enter
the ls command, Linux searches for and runs the ls command by executing the binary file /bin/ls.
~ Home directory
/ Root directory
Symbol Stands for path to
. Current directory
.. Parent directory
1. cd ~
Note that your lab environment's default home directory, ~, is /home/theia. After changing
directories to ~, you can check this yourself by entering the pwd command, which displays the path
to your current working directory.
1. cd ..
For example, if your working directory was /home/theia, then the new working directory will
be /home.
1. cd /
1. cd bin
The /bin directory is called a child of the root directory, /, because it's a subdirectory within it.
The cd command understands implicitly that bin is the name of a subdirectory of your current
working directory. As a result, it deduces that its path is a combination of the current working
directory path (/) and the subdirectory name (bin).
Another method to change directories is to enter the command cd ./bin. In this case, . represents the
path to your current working directory.
Give it a try by entering cd ./bin in your command line! What directory did you end up in?
Tip: If cd ./bin doesn't take you to the /bin directory, double-check what directory you are currently
in. Remember, . represents the directory you are in. If you're already in /bin, the terminal will
attempt to direct you to /bin/bin.
2.5. Changing from your working directory back to your home directory
There are multiple ways to change your current working directory back to your default home
directory. One way is to return to your parent directory, .., and type the address of your home
directory /home/theia:
1. cd ../home/theia
A simpler way to do this would be to use the ~ symbol to quickly and directly navigate to your home
directory:
1. cd ~
Both methods will change your working directory back to your home directory.
In this environment, we have provided a special empty project directory for your work.
This project directory is located at /home/project and is a sibling directory to /home/theia.
Change your current working directory to its sibling directory, your project directory, by entering the
following command:
1. cd ../project
Assuming you are still in the same session as you were when you completed the previous exercises in
this lab, you should see exactly what commands you have entered so far.
For example, suppose you want to use the cd command to navigate to /bin again. Rather than
retyping the command, simply press the Up Arrow key until you see the cd bin displayed at the
command prompt and press Enter.
Of course, this command will only work as expected if your current working directory is the correct
one. If the command fails, you can keep scrolling through your history until you find the cd command
to return to your root directory, (cd /). Enter that command first, then use the Up Arrow key to find
and enter the cd bin command from your history.
Your command history is very useful if you are doing repetitive tasks and want to avoid retyping
things repeatedly.
Type cd /bi and press the Tab key. As you might expect, the command autocompletes to cd /bin. But
what happens if you just type cd /b and press Tab?
In this case, nothing happens. But if you hit Tab a second time, you'll see a list of all the valid
candidates for autocompletion, namely bin/ and boot/.
Notice that cd /b reappears in the command prompt. You cannot use tab completion while there are
multiple valid candidates for autocompletion. To narrow down your choices, add an i at the end of
your line, then press Tab again. This time the prompt will fill with cd /bin, the only valid option
remaining.
Tip: Tab completion only works when there is one valid candidate for autocompletion. When there
are multiple possible options, the command prompt will prompt you to enter more letters until there
is only one possible option.
You can even use tab completion repeatedly to "dig into" a directory tree.
Let's say you have used tab completion to get to the point where ls /home is displayed at the
command prompt. You can press Tab twice to get a listing of all the directories under /home. As you
did with cd /bin, use tab completion to complete the address of a subdirectory such as ls
/home/theia. You can repeat this process as many times as you like until you reach the end of a
chosen path.
Future labs will cover command options more in-depth. In this lab, you became familiar with the
terminal environment and some basic commands.
Practice Exercises
Solution: ls /
Solution: cd ~
Note: Your home directory in this environment is /home/theia. The default home directory in Linux
is /home/{username}, where username is the name you would have been assigned had you created
a personal Linux user account.
Solution: pwd
Solution:
Because there are two directories in / that have names starting with a b, you need to
press Tab twice. Then the terminal displays the two available options: bin/ and boot/. To
disambiguate, enter an i and press the Tab key.
5. Use your terminal's command history to change directories back to your home directory.
Solution:
Although this sandbox environment will not require a password to run the sudo command, typical
Linux production-oriented environments will require a password to run sudo.
You will need to use the sudo command to activate the powerful apt command. You'll use apt to
upgrade nano to its latest version. You will also use apt to install Vim.
apt (Advanced Packaging Tool) is a powerful command line tool. You use it to perform system
administration operations such as installing software packages, upgrading existing packages, and
updating your system's package list index.
Before installing or upgrading any packages on your Linux system, it's best practice to
first update your package list index. Go ahead and enter this command,
to update (re-synchronize) your package index files from their sources. This will take a bit of time to
run. While you're waiting, if you're interested, go ahead and open another terminal and view the
locations apt uses to access those sources in the file /etc/apt/sources.list.
In short, running apt with the update option ensures all of your package dependencies are up-to-
date and correctly specified prior to making any changes to your system's packages.
nano is a simple command line editor that enables you to use the terminal window as a text editor.
nano is already installed on your system. Go ahead and upgrade to the latest supported version of
nano by entering:
Type Y and press Enter to continue. Updating nano will take some time to complete.
The capital Y in Y/n (yes or no) means "yes" is the default - if you press Enter without typing
anything, the terminal assumes you are choosing "yes".
Note: If you encounter a colon (:) similar to the one highlighted in the screenshot while upgrading
Nano, please press 'q' to exit and continue with the upgrade process.
You'll get to use nano soon, but first, let's take a look at the other main use case for apt, installing
packages.
Another popular text-editing program is Vim. Vim is a highly configurable text editor built for
efficiency. It takes some practice to get good at using Vim, but the time investment is very
worthwhile.
Because Vim isn't preinstalled on your Linux system, you'll need to install it yourself. If you haven't
already done so in this session, ensure you run the command sudo apt update. Then to install Vim,
enter the following command:
Similar to when you updated nano, you may again be prompted: Do you want to continue? [Y/n]
Type Y and press Enter to continue. Vim will begin installing on your system.
nano is known for being a simple and easy-to-master text editor. Vim is harder to learn, but it has
many expert-level features that nano doesn't offer.
1. cd /home/project
Try auto-completing the path by typing cd /home/pr and pressing the Tab key.
If you enter ls here, you shouldn't see any files or subdirectories listed.
1. nano hello_world.txt
in the terminal. This will simultaneously create a new file called hello_world.txt and enable you to
begin editing it using the nano text editor.
Double-check that your new file was created by opening another terminal window and running
the ls command on /home/project. You should see hello_world.txt listed.
In your nano terminal, whatever you type will be added to your text buffer, where text is stored until
you save it. Type the following text in your nano terminal:
1. Hello world!
This will create the text Hello world! in your text buffer.
To create another line of text, press Enter. In your new line, type
1. This is the second line of my first-ever text file created with nano.
Now:
Y Yes
N No ^C Cancel
At this point, nano should have exited and returned you to the command prompt.
1. cat hello_world.txt
you should be able to inspect and verify that your new file contains the two lines you wrote to it with
nano. Cool!
1. vim
at the command prompt, which displays something like the following in your terminal window:
Notice that you can get help on Vim by entering :help, and you can quit Vim by entering :q.
Go ahead and enter :help. This brings up an informative help file you can scroll through.
When you are done reading, simply enter :q to quit Vim and return to the command prompt.
Vim is very powerful and takes some time to learn. We're just covering the very basics here and
leaving it to you to explore further. Check out the official Vim site at https://www.vim.org.
1. vim hello_world_2.txt
Once your Vim session has started, go ahead and press i to enter Insert mode. This is the mode
where you can enter and delete text in the text buffer.
1. Hello World!
Just like in nano, press Enter to start a new line, and then type
When you're done typing text in the buffer, press the Escape key, Esc, to exit the Insert mode. This
brings you to Command mode.
In Vim, it's easy to accidentally end up in a mode you didn't intend to be in. No worries - you can use
the Esc key to return to Command mode.
Now that you are back in Command mode, you can save your work by entering the command :w.
This writes the contents of the text buffer to your text file.
Practice Exercises
1. Using nano, edit your new hello_world.txt file to add a new line containing the following text:
Tip: Use the Up Arrow key to go through your command history until you see the right command.
Solution:
1.
2.
3.
2. Using Vim, create a file called done.txt that prints "I am done with the lab!" when you execute
the file with Bash.
In this exercise you will use Vim to create a file that contains the echo command, which you may not
have seen yet. You will also run the file using Bash, a shell scripting language that you will learn more
about later in this course. Basically, the file you create will contain a basic command that Bash can
interpret and do something with.
Solution: 1
Enter
1. vim done.txt
Solution: 2
1. Using Esc to exit out of Insert mode and return to Command mode
Solution: 3
Once you've saved and exited and are back at the command prompt, you can run the file with Bash
by entering:
1. bash done.txt
This command invokes the Bash shell to interpret the text contained in done.txt as a command. In
particular, Bash runs the echo command along with the quoted text as the input to echo,
while echo simply prints the input text to the terminal window.
When finished, you should see the following text echoed to your terminal:
Summary
By now you are beginning to understand a lot more about how your Linux system functions. In this
lab, you learned how to:
Perform some fundamental sys admin operations, such as updating your package list,
upgrading existing packages, and installing new packages
Create and edit a few text files using some serious command-line text editors, nano and Vim
Understand how the Bash scripting language can be used to interpret commands you include
within a text file
In the 1980s, GNU was developed at MIT. GNU stands for “GNU’s not Unix” and was made as
a free, open source set of the existing Unix system tools. And in 1991, Linus Torvalds
developed a free, open source version of the Unix kernel called Linux.
Linux is widely used today in mobile devices, desktops, supercomputers, data centers, and
cloud servers.
Linux distributions (also known as distros) differ by their UIs, shell applications, and how the
OS is supported and built.
The design of a distro is catered toward its specific audience and/or use case. Popular Linux
distributions include Red Hat Enterprise Linux (RHEL), Debian, Ubuntu, Suse (SLES, SLED,
OpenSuse), Fedora, Mint, and Arch.
The Linux system consists of five key layers: the UI, application, OS, kernel, and hardware.
The user interface enables users to interact with applications. Applications enable users to
perform tasks within the system. The operating system runs on top of the kernel and is vital
for system health and stability, and the kernel is the lowest-level software that enables
applications to interact with hardware. Hardware includes all the physical or electronic
components of your PC.
The Linux filesystem is a tree-like structure consisting of all directories and files on the
system.
A Linux shell is an OS-level application that you can use to enter commands. You use a
terminal to send commands to the shell, and you can use the command to navigate around
your Linux filesystem.
You can use a variety of command-line or GUI-based text editors such as GNU nano, vim, vi,
and gedit.
.deb and .rpm are distinct file types used by package mangers in Linux operating systems.
You can use GUI-based and command-line package managers to update and install software
on Linux systems.
Scroll through your command history with the Up Arrow and Down Arrow keys to find and re-run a
command you already used.
Getting information
1. man ls
Special paths
~ home directory
/ root directory
1. ls
1. pwd
1. cd child_directory_name
Tip: Because cd looks in the current directory for child_directory_name, you don’t need to type the
entire path.
To home: cd ~ or cd
Suppose you have two sibling directories within the same directory, dir_1 and dir_2, and your
present working directory is dir_1. To switch to dir_2, enter:
cd ../dir_2
Tip: Using .., you don't need to know the path to the parent directory to switch to a sibling.
Change the current directory back to the directory you were in previously:
cd -
Install Vim:
1. nano file_name.txt
Tip: If the file already exists, nano simply opens it for editing.
MODULE 2 – INTRODUCTION TO LINUX COMMANDS
Getting Help for Linux Commands
There is huge value in spending time exploring and experimenting with commands, but there are
many things you can't figure out just by experimenting. You need to see what's out there, see what's
possible, and learn where to look to get answers. Let's take a look at some great ways to get the
information you need to help you make progress.
In this reading, you may see links to external sources. You can open them by right-clicking and
pressing "Open in new tab."
The man command, which stands for "manual", provides the standard way to access help for Unix-
like commands from the command prompt. It has been in development since 1971.
You can get a listing of all the commands on your system that have a manual page by entering:
1. man -k .
The resulting list includes a brief description of what each command does.
1. man command_name
NAME
The name of the command or feature and a brief description of what it does.
SYNOPSIS
A summary of the command syntax, including any options and arguments that can be used.
DESCRIPTION
A more detailed description of the command, including its function and behavior.
OPTIONS
All the available options and arguments that can be used with the command.
EXAMPLES
SEE ALSO
You may also see other sections, including: EXIT STATUS, RETURN VALUE, ENVIRONMENT, BUGS,
FILES, AUTHOR, REPORTING BUGS, HISTORY, and COPYRIGHT.
Similar to man pages, TLDR Pages is a free and open-source collaborative documentation effort. The
goal is to create documentation that is more accessible than the traditional man pages, which tend
to be quite verbose.
TLDR Pages, short for "Too Long; Didn't Read" and also known simply as tldr, provide examples for
common use cases of various commands. The format of TLDR pages is similar to that of a cheatsheet.
You can install a command-line tool to access TLDR Pages from your terminal. Install it using the
following command:
Once you've installed the tool, you can use the tldr command to easily access the TLDR page of a
command.
1. tldr command_name
The tool will display a short, easy-to-understand summary of the command along with some
examples of how to use it.
Stack Overflow is a popular community-driven question and answer platform for programmers,
developers, and system administrators. It has a vast repository of questions and answers related to
various programming languages, tools, and operating systems, including Linux.
To search for information about commands on Stack Overflow, you can use the search bar on the
homepage and enter the name of the command you're looking for, along with any specific keywords
or parameters. You can also refine your search by adding relevant tags, such as "linux" or "command-
line".
Once you've entered your search query, Stack Overflow will display a list of relevant questions and
answers that match your query. You can browse through the results to find the information you
need, and even post your own question if you can't find an answer to your specific query.When
searching for information about commands on Stack Overflow, it's important to check the date of the
answers to ensure that the information is still current and relevant. You should also read through the
comments and discussion threads to get a better understanding of the context and any potential
issues or limitations related to the command you're researching.
Stack Exchange is a network of question and answer communities, similar to Stack Overflow, but
covering a broader range of topics beyond just programming. There are several Stack Exchange
communities that specialize in topics related to Linux and open source software, such as Unix &
Linux, Ask Ubuntu, and Server Fault.
Visit the relevant community to search for information on Stack Exchange. Like Stack Overflow, you
can use the search bar to enter the name of the command you're looking for, along with any
keywords or parameters.
Google is a powerful tool that can provide you the answer to almost any question. Learn how to
enter the right queries and filter your results, such as by including "Wikipedia", "Stack Overflow", or
"Linux" as part of your search. However, use at your own risk. Never blindly trust what you find on
the web - there's a lot of noise out there!
Throughout this course, you will encounter "cheat sheets" that condense the information you've
learned into easy-to-reference guides. They are great for reviewing the material you've learned and
can also help you out with your graded assignments.
Finally, Wikipedia maintains a list of commands that can be found on Unix operating systems, along
with a short description. You can check the page to quickly reference a Unix
command: https://en.wikipedia.org/wiki/List_of_Unix_commands
Summary
This reading has provided an overview of how to find more information about Linux commands. As
you continue to use commands in this course and beyond, you'll likely find yourself using the same
command patterns over and over. Each time you use a command, you'll get a little more comfortable
and familiar with it. Your fingers will develop muscle memory until using commands becomes second
nature!
In this exercise, you will familiarize yourself with useful commands for providing system and user
information.
1. whoami
It will display the user name as theia because you are logged into this lab as theia.
You can get a list of currently logged in users using the command who, but this command doesn't
work in the Theia environment yet.
uname
By default the command prints the kernel name. The u in uname refers to "unix-like 0S".
1. uname
If you enter the command uname, you will see Linux printed in the output.
1. uname -a
Kernel name
Kernel version
Hardware platform
Operating system
id
This command displays the user id and group id information of the current user.
1. id
It will display the uid (user id) and gid (group id) for the user theia.
df
1. df
This command will display available disk space in 512-byte blocks. To get available disk space in a
"human-readable" format, enter:
1. df -h
This will return the available disk space in units like gigabytes and terabytes.
ps
The ps command lists each process that is currently running and its PID (process id).
1. ps
However, the output only contains the processes that are owned by you.
By using the -e option, you can display all of the processes running on the system. The includes
processes owned by other users.
1. ps -e
top
The top or "table of processes" command provides a dynamic, real-time view of your system.
1. top
The top command displays a table of detailed information related to the processes or threads which
are currently running and managed by the kernel. Additionally, it also provides information related to
CPU and memory usage per process.
When you start top, you'll be presented with the following elements on the main top screen:
Summary area - shows information like system uptime, number of users, load average, and
overall memory usage
If you want to exit automatically after a specified number of repetitions, use the -n option as follows:
1. top -n 10
You can press the following keys with Shift while top is running to sort the table:
Key Sorts by
M Memory Usage
P CPU Usage
N Process ID (PID)
Key Sorts by
T Running Time
For example, you can find out which process is consuming the most memory by entering Shift + m.
echo
The echo command displays the given text on the screen. For example, entering:
prints:
\t Insert a tab
Use the -e option of the echo command when working with special characters. For example:
will print:
2. in two lines
date
1. date
It has several options which allow you to display the current date and time in different formats.
For example, the following command displays the current date in mm/dd/yy format:
1. date "+%D"
Here are some popular format specifiers that you can try out:
Specifier Explanation
man
The man command displays the user manual for any command that you provide as its argument.
For example, to see the manual page for the ls command, enter:
1. man ls
Scroll through the command's manual to find any info you may need. When you're done, press q to
quit.
You will sometimes encounter a command that does not have a man page avaible on your system. To
see all available man pages with a brief description of each command, enter:
1. man -k .
Practice exercises
Solution: uname
Solution: ps -e
Solution: top
5. Using one command, display the messages "Hello!" and "Goodbye!" separated by a new line.
In this lab, you learned that you can use the commands:
pwd
When working in a Linux terminal, you will always be working from a directory. By default, you will
start in your home directory. To get the absolute path of your present working directory, enter the
following:
1. pwd
This will print the name of the directory you are currently working in.
ls
To list the files and directories in the current directory, enter the following:
1. ls
The following command will list the many binary and executable files which are present in
your /bin (binaries) directory.
1. ls /bin
The /bin directory happens to be where Linux commmands such as ls and pwd are stored. For
example, you can see that ls is present by entering the following:
1. ls /bin/ls
To list all files starting with b in the /bin directory, try entering the following:
1. ls /bin/b*
Tip: The asterisk * is a special character called a wildcard. It is used to represent any string of
characters.
To list all files ending in r in the /bin directory, enter the following:
1. ls /bin/*r
To print a longer list of files with additional information, such as the last-modified date, enter the
following:
1. ls -l
Here are some common options that you can try with the ls command:
Option Description
To get a long list of all files in /etc, including any hidden files, enter the following:
1. ls -la /etc
Here we combined the options -l and -a by using the shorter notation, -la.
mkdir
To create a directory named scripts in your current directory, run the following command:
1. mkdir scripts
Use the ls command to verify whether the scripts directory was created:
1. ls
You should see a directory named scripts listed.
cd
To change your present working directory to the scripts directory, run the following command:
1. cd scripts
Now use the pwd command to verify whether your current working directory has changed as
expected:
1. pwd
You can enter cd without any directory name to move back to your home directory:
1. cd
Then, enter the pwd command to verify whether your current working directory has changed:
1. pwd
The syntax .. is a shortcut that refers to the parent directory of your current directory. Run the
following command to change dictories up one level:
1. cd ..
touch
1. cd
Next, use the touch command to create an empty file named myfile.txt:
1. touch myfile.txt
1. ls
If the file already exists, the touch command updates the access timestamp, or last-modified date of
the file. To see this, enter:
1. touch myfile.txt
And use use the date command to verify the date change:
1. date -r myfile.txt
find
The find command is used to search for files in a directory. You can search for files based on different
attributes, such as the file's name, type, owner, size, or timestamp.
The find command conducts a search of the entire directory tree starting from the given directory
name.
For example, the following command finds all .txt files in the /etc directory and all of its
subdirectories:
Note: Along with listing all the .txt files, the terminal may return "Permission denied" errors.
These errors are normal, as you have limited access permissions on the lab machine.
rm
The rm command is used to delete files, ideally with the -i option, which creates a prompt to ask for
confirmation before every deletion.
To remove the file myfile.txt, enter the following command and press y to confirm deletion, or n to
deny deletion:
1. rm -i myfile.txt
1. ls
Tip: When you are only removing one file with the rm command, the -i option is redundant. But if
you want to remove multiple files, for example by using a wildcard to find all filenames matching a
pattern, it's best practice to confirm or deny each deletion by including the -i option.
Be careful when deleting files or directories! There is normally no way to restore a deleted file once it
is deleted, as there is no trash folder. This is why you should always back up, or archive, your
important files. You will learn more about archiving files soon.
mv
You can use the mv command to move files from one directory to another and/or rename them.
Before doing so, let's first create a new file called users.txt:
1. touch users.txt
You should always use caution when moving a file. If the target file already exists, it will be
overwritten, or replaced, by the source file.
Conveniently, however, when the source and target directories are the same, you can use mv to
rename a file.
To illustrate this, use mv to rename users.txt to user-info.txt by entering the following command:
1. mv users.txt user-info.txt
Because the source and target directories are the same (your present working directory),
the mv command will rename the file.
1. ls
1. mv user-info.txt /tmp
1. ls
1. ls -l /tmp
cp
You can use the cp command to copy user-info.txt, which is now in your /tmp directory, to your
current working directory:
1. cp /tmp/user-info.txt user-info.txt
1. ls
At times, you may want to copy the contents of an existing file into a new one.
The following command copies the content of /etc/passwd to a file named users.txt within the
current directory:
1. cp /etc/passwd users.txt
1. ls
Practice exercises
Solution: ls /home
Solution: cd
pwd
ls
4. Create a new, empty file named display.sh in the tmp directory, and verify its creation.
Solution: cd tmp
touch display.sh
ls -l
6. Move your copied file, report.sh, up one level in the directory tree to the parent directory. Verify
your changes.
ls
ls ../
Solution: rm -i display.sh
8. List the files in /etc directory in the ascending order of their access time.
Solution: cp /var/log/bootstrap.log
Summary
In this lab, you learned that you can use the commands:
rm to remove a file
cp to copy a file
Linux is a multi-user operating system. This means that by default, other users will be able to view
any files you store on the system. However, you may have some files - such as your personal tax
documents or your employer's intellectual property documents - that are private or confidential.
How can you protect these sensitive documents from being viewed or modified by others?
There are three possible levels of file ownership in Linux: user, group, and other.
Whoever creates a file, namely the user at the time of creation, becomes the owner of that file by
default. A group of users can also share ownership of a file. The other category essentially refers
anyone in the universe with access to your Linux machine - careful when assigning ownership
permission to this level!
Only an official owner of a file is allowed to change its permissions. This means that only owners can
decide who can read the file, write to it, or execute it.
2. $ more my_new_file
4. $ ls -l my_new_file
Here we've echoed the string "Who can read this file?" into a new file called my_new_file. The next
line uses the more command to print the contents of the new file. Finally, the ls command with the -
l option displays the file's (default) permissions: rw-r--r--
The first three characters (rw-) define the user permissions, the next three (r--)
the group pemissions, and the final three (r--) the other permissions.
So you, being the user, have the permission rw-, which means you have read and write permissions
by default, but do not have execution permissions. Otherwise there would be an x in place of the
last -.
Thus by looking at the entire line, rw-r--r--, you can see that anyone can read the file, nobody can
execute it, and you are the only user that can write to it.
Note: The - at the very beginning of the line in the terminal means that the permissions are referring
to a file. If you were getting the permissions to a directory, you would see a d in the front for
"directory".
Directory permissions
The permissions for directories are similar but distinct for files. Though directories use the
same rwx format, the symbols have slightly different meanings.
The following table illustrates the meanings of each permission for directories:
Directory Permission Permissible action(s)
Setting appropriate permissions on directories is a best practice for both security and stability
reasons. Though this reading focuses on security, you will learn more about other reasons for setting
file permissions and ownership later in this course.
You can revoke read permissions from your group and all other users by using the chmod command.
Ensure successful modification by using the ls -l command again:
2. ls -l my_new_file
In the chmod command, go-r is the permission change to be applied, which in this case means
removing for the group (g) and others (o) the read (r) permission. The chmod command can be used
with both files and directories.
You've learned what it means to read or write to a file, but what does it mean to have permissions
to execute a file in Linux?
A Linux file is executable if it contains instructions that can be directly interpreted by the operating
system. Basically, an exectuable file is a ready-to-run program. They're also referred to
as binaries or executables.
In this course, you will become very familiar with a particular kind of executable called a script, which
is a program written in a scripting language. You'll learn all about shell scripting, or more
specifically Bash scripting, which is writing scripts in Bash (born-again shell), a very popular shell
scripting language. A shell script is a plain text file that can be interpreted by a shell.
Formally speaking, for a text file to be considered an executable shell script for a given user, it needs
to have two things:
2. A directive, called a "shebang", in its first line to declare itself to the operating system as a
binary
All of this will become more clear to you soon when we get to the topic of shell scripting.
Summary
You can use the ls -l command to view file and directory permissions
Required files:
Run the following code to download the required files for this exercise:
1. cd /home/project
2. wget https://cf-courses-data.s3.us.cloud-object-storage.appdomain.cloud/IBM-LX0117EN-
SkillsNetwork/labs/module%201/usdoi.txt
Each file and each directory in your Linux system has permissions set for three permission categories:
the 'user', the 'group', and 'all users' (or 'other').
The following permissions are set for each file and directory:
Permission Symbol
Read r
Write w
Execute x
To see the permissions currently set for a file, run the ls command with the -l option.
For example, to see the permissions for the file named usdoi.txt in your current directory, enter the
following:
1. ls -l usdoi.txt
The permissions set here are rw-r--r--. The - preceeding these permissions indicates that usdoi.txt is a
file. If it were a directory, you would see a d instead of the -.
The first three entries correspond to the current user, the next three correspond to the group, and
the last three are for all others. You can see the user has read and write permissions, while the user
group only has read permission, and all other users have only read permission. No users have
execute permission, as indicated by the - instead of an x in the third position for each user category.
1.2 Change file access permissions
chmod
The chmod or change mode command lets you change the permissions set for a file.
Option Description
The following command revokes read permissions for all users (user, group, and other) on the
file usdoi.txt:
1. chmod -r usdoi.txt
1. ls -l usdoi.txt
1. chmod +r usdoi.txt
1. ls -l usdoi.txt
Now to remove the read permission only for 'other' category, enter the following:
1. ls -l usdoi.txt
Recall the following table, which illustrates the meanings of each permission for directories with
examples of allowable operations for a given directory.
For this exercise, first move to your project directory and create a new directory called test:
1. cd /home/project
2. mkdir test
Check the default permissions that the system sets for your new test directory:
1. ls -l
1. total 12
You, "theia", as the owner of test, have read, write, and execute permissions set by default. But all
others only have read and execute permissions set and cannot write to your test directory. This
means users outside your group can't add or remove files from test. They can, however, explore your
directory to see what files and directories exist there.
Note: You might be wondering what that s permission is in the execute slot for your group.
The s stands for "special permission". It means that any new files created within the directory will
have their group ownership set to be the same as the directory owner. We won't go into this level of
detail in this course, but you can learn more about advanced Linux permissions here: Linux
permissions: SUID, SGID, and sticky bit.
Go ahead and verify for yourself that you have permission to run the following commands. Change
the directory to your test directory, create a new directory within it, then return to your parent
directory:
1. cd test
2. mkdir test2
3. cd ../
Remove your user execute permissions on test using the following command:
1. cd test
1. ls -l
Even though you have "write" permissions set, you can't actually create a new directory within test,
because removing execute permissions overrides write permissions. For example, entering,
1. mkdir test/test3
throws an error:
This time, try restoring execute permissions on test and denying write permissions. Then verify your
changes:
2. ls -l
Now you can go into test, but you still can't write to it! Entering
1. cd test
2. mkdir test_again
Practice exercises
1. List the permissions set for the file usdoi.txt that you downloaded to your project directory at
the beginning of the lab.
Solution: cd /home/project
ls -l usdoi.txt
2. Revoke the write permission on usdoi.txt for the user, and verify your result.
ls -l usdoi.txt
You should only see read permissions set for all three user categories.
3. What happens if you try to delete usdoi.txt after revoking write permissions for the user?
1. rm usdoi.txt
Entering y will override the write protection and delete the file, which you can verify as follows:
1. ls usdoi.txt
Solution: cd tmp_dir
mkdir sub_dir
You should be able to enter the directory with the cd command, but you should see a "permission
denied" error after entering the mkdir command.
Again, you should see a "permission denied" error for the mkdir command.
Summary
Files and directories can have read, write, and execute permissions for the user, group, and
others
You can view the permissions for all files and directories within a directory by using ls -l
Changing permissions on a directory will change who can do what to the directory and its
files
In this exercise, you will learn how to explore file contents using the cat, more, and less commands to
display the file contents in your terminal window.
1. cd ~
Using the ls command, you should see a file called entrypoint.sh. The .sh is a convention used to
identify a text file as being a shell script.
The cat command displays the contents of the file and exits back to the command prompt as follows:
1. cat entrypoint.sh
It only displays the tail end of the file, so if the file is too long to fit on the terminal, you won't be able
to see some of its contents.
Although the cat command may not be the best way to view the contents of a file, especially larger
files, it is quite useful for shell scripting applications. For example, it is often used to concatenate, or
append one file onto another.
A better alternative to the cat command for viewing file contents is the more command. By entering
the following command:
1. more entrypoint.sh
Tip: The first line of this particular file, #!/bin/bash, is called a shebang. Basically, this shebang line
makes the file a bash script by invoking the bash shell. You will learn more about shebang lines later
in this course.
When using the more command, you can see only as many lines as will fit on your terminal window
at once.
To see the next portion of the file, just press your spacebar. You can keep paging this way, tapping
the spacebar until you reach the end of the file. Once you reach the last page, you will exit back to
the command prompt.
Another way to exit is simply to type q, which quits and returns to the command prompt.
What if you want to move up and down through the file, not just downward? In this case, you can
use the less command:
1. less entrypoint.sh
Just like more, the less command displays the first page of the file. What's useful about less is that
you can use it to move around the file, page by page, using the Page Up and Page Down keys.
You can also scroll up and down through the file line-by-line, using the Up Arrow and Down
Arrow keys, ↑ and ↓.
Unlike more, less does not automatically exit when you reach the end of a file, allowing you the
option to continue scrolling around. You can quit at any time by typing q.
1. cd /home/project
1. wget https://cf-courses-data.s3.us.cloud-object-storage.appdomain.cloud/IBM-DB0250EN-
SkillsNetwork/labs/Bash%20Scripting/usdoi.txt
The wget command downloads a text file called usdoi.txt from the provided URL. You'll see this
command again later in the context of networking commands. You can check to see if you
successfully downloaded the usdoi.txt by using the ls command.
head
By default, head will print the first 10 lines of a file. To use it with usdoi.txt, enter the following:
1. head usdoi.txt
You can also specify the number of lines to be printed. Print only the first 3 lines of text from the
file usdoi.txt by entering:
1. head -3 usdoi.txt
tail
By default, tail will print the last 10 lines of the file usdoi.txt:
1. tail usdoi.txt
Just like with head, you can specify the number of lines to be printed. Print the last 2 lines of the
file usdoi.txt by entering the following:
1. tail -2 usdoi.txt
wc
If you want to find the number of lines, words, and characters in a file like usdoi.txt, enter the
following command:
1. wc usdoi.txt
The output contains the number of lines, followed by the number of words, followed by the number
of characters in the file.
1. wc -l usdoi.txt
1. wc -c usdoi.txt
sort
You can use the sort command to display the lines of a file sorted alphanumerically.
1. sort usdoi.txt
1. sort -r usdoi.txt
uniq
1. wget https://cf-courses-data.s3.us.cloud-object-storage.appdomain.cloud/IBM-LX0117EN-
SkillsNetwork/labs/module%201/zoo.txt
1. cat zoo.txt
View the contents of zoo.txt with identical, consecutive lines dropped using the uniq command:
1. uniq zoo.txt
The uniq line will drop any lines in the file that are identical and consecutive. This is similar to what is
known as "dropping duplicates". As you can see from this example, however, there can still be
duplicated lines left over if these lines are not repeated right after the other.
grep
The grep command allows you to specify a pattern and search for lines within a file that match that
pattern.
For example, the following command prints all lines in the file usdoi.txt which contain the
word people:
-n Along with the matching lines, also print the line numbers
You can use these options to print all the lines from the /etc/passwd file which do not contain the
pattern login:
cut
The cut command allows you to view only specific fields from each line of text in a file.
For example, you can use cut with the -c option to view only the first two characters of each line:
1. cut -c -2 zoo.txt
1. cut -c 2- zoo.txt
The cut command can also be used to extract a field from a delimited file.
To demonstrate this, start by downloading and taking a look at the following comma-separated file:
1. wget https://cf-courses-data.s3.us.cloud-object-storage.appdomain.cloud/IBM-LX0117EN-
SkillsNetwork/labs/v4_new_content/labs/names_and_numbers.csv
2. cat names_and_numbers.csv
Now you can extract just the phone numbers for each person listed in the file using the -d (delimiter)
and f (field) options as follows:
-d "," tells the command that the delimiter is a comma, and -f2 tells it to extract the second field.
paste
Then use the paste command to view the two files merged together, line-by-line, as columns
delimited by a Tab character:
Try changing the delimiter. Instead of the default Tab delimiter, you can specify a comma , as follows:
Practice Exercises
Before you begin, ensure you're working in your home directory by entering:
1. cd ~
2. pwd
Solution: wc -l /etc/passwd
2. Display the lines that contain the string "not installed" in /var/log/bootstrap.log.
Solution:
7. Extract and view only the names, without their phone numbers, from the
file names_and_numbers.csv.
Solution: cd /home/project
Summary
See the first and last N lines of a file using head and tail
Find the number of lines, words, and characters in a file with wc
Extract lines and fields from a file with grep and cut
hostname
1. 1
1. hostname
An IP address (Internet Protocol address) is a numerical label assigned to each device connected to
a computer network that uses the Internet Protocol for communication.
You can use the -i option to view the IP address of the host:
1. 1
1. hostname -i
ifconfig
The ifconfig command is used to configure or display network interface parameters for a network.
1. 1
1. ifconfig
To display the configuration of a particular device, such as the ethernet adapter eth0, enter:
1. 1
1. ifconfig eth0
eth0 is usually the primary network interface that connects your server to the network.
You can see your server's IP address in line 2 after the word inet.
1Hands-on Lab: Working with Networking Commands2About Skills Network Cloud IDE3Exercise 1 -
View configuration info about your network4Exercise 2 - Test network connectivity5Exercise 3 -
View or download data from a server6Practice exercises7Summary
ping
Use the ping command to check if www.google.com is reachable. The command keeps pinging
data packets to server at www.google.com and prints the response it gets back. (Press Ctrl+c to
stop pinging.)
1. 1
1. ping www.google.com
1. 1
1. ping -c 5 www.google.com
1Hands-on Lab: Working with Networking Commands2About Skills Network Cloud IDE3Exercise 1 -
View configuration info about your network4Exercise 2 - Test network connectivity5Exercise 3 - View
or download data from a server6Practice exercises7Summary
curl
You can use curl to access the file at the following URL and display the file's contents on your screen:
1. 1
1. curl https://cf-courses-data.s3.us.cloud-object-storage.appdomain.cloud/IBM-DB0250EN-
SkillsNetwork/labs/Bash%20Scripting/usdoi.txt
To access the file at the given URL and also save it in your current working directory, use the -
O option:
1. 1
1. curl -O https://cf-courses-data.s3.us.cloud-object-storage.appdomain.cloud/IBM-DB0250EN-
SkillsNetwork/labs/Bash%20Scripting/usdoi.txt
You can also use curl to view the HTML code for any web page if you know its URL.
wget
The wget command is similar to curl, however its primary use is for file downloading. One unique
feature of wget is that it can recursively download files at a URL.
To see wget in action, first remove usdoi.txt from your current directory:
1. 1
1. rm usdoi.txt
1. 1
1. wget https://cf-courses-data.s3.us.cloud-object-storage.appdomain.cloud/IBM-DB0250EN-
SkillsNetwork/labs/Bash%20Scripting/usdoi.txt
1Hands-on Lab: Working with Networking Commands2About Skills Network Cloud IDE3Exercise 1 -
View configuration info about your network4Exercise 2 - Test network connectivity5Exercise 3 - View
or download data from a server6Practice exercises7Summary
Practice exercises
1. 1
2. 2
1. cd `/home/project`
2. pwd
Solution:
1. 1
1. hostname -i
Solution:
1. 1
1. ping www.google.com
Solution:
1. 1
1. ifconfig eth0
Solution:
1. 1
1. curl www.google.com
Solution:
1. 1
1. wget www.google.com
Note: wget saves the HTML code as index.html. You can check this with:
1. 1
1. ls -l
1Hands-on Lab: Working with Networking Commands2About Skills Network Cloud IDE3Exercise 1 -
View configuration info about your network4Exercise 2 - Test network connectivity5Exercise 3 - View
or download data from a server6Practice exercises7Summary
Summary
View your network configuration using the hostname and ifconfig commands
Authors
Jeff Grossman
Ramesh Sannareddy
Sam Prokopchuk
Other contributors
Rav Ahuja
1Hands-on Lab: Archiving and Compressing Files2About Skills Network Cloud IDE3Exercise 1 - File
and folder archiving and compression4Summary
tar
The tar command allows you to pack multiple files and directories into a single archive file.
The following command creates an archive of the entire /bin directory and writes the archive to a
single file named bin.tar.
Option Description
1. 1
To untar the archive or extract files from the archive, use the -x option:
1. 1
1. 1
1. ls -l
zip
The following command creates a zip file named config.zip consisting of all the files with
extension .conf in the /etc directory.
1. 1
1. 1
unzip
1. 1
1. unzip -l config.zip
The following command extracts all the files in the archive bin.zip.
1. 1
1. unzip -o bin.zip
We added the -o option to force overwrite in case you run the command more than once.
Summary
tar allows you to pack multiple files and directories into a single archive file
The curl and wget commands display and download files from URLs, and
the cat and tail commands display file contents.
You can get user information with the whoami and id commands, and get operating system
information using the uname command. You can check system disk usage using
the df command and monitor processes and resource usage with ps and top. Print string or
variable value using echo, print and extract information about the date with
the date command, and read the manual for any command using man.
ls lists all files and directories within a specified directory tree and cd navigates between
directories. The find command finds files in your directories.
Relative paths are relative to your current working directory, while absolute paths stand
independently
You can create files and directories with the touch and mkdir commands, delete them
with rm and rmdir, and copy and move them cp and mv.
The cat, more, head, and tail commands allow you to sort and view file contents or view
only a certain number of lines. Determine line, word, and character counts with wc.
You can use sort to view the lines of a file alphanumerically and uniq to remove repeated
lines from your view. grep gets the lines of a file that match your desired criteria,
and cut extracts slices and fields from lines. You can merge lines from different files
using paste.
hostname and ifconfig allow you to view the network configuration. You can test a network
connection using ping and send and receive data using curl and wget.
Compression preserves storage space, speeds data transfer, and reduces system load.
zip compresses files and folders prior to archiving them. tar archives and compresses files
and directories into a tarball. unzip unpacks and decompresses a zipped archive, and tar can
also decompress and unpack a tar.gz archive.
List available man pages, including a brief description for each command:
1. man -k .
This provides a brief overview of the curl command's usage and options.
Return the current date and time:
1. date
To home:
1. cd ~` or `cd
1. ps -e
Copy a file:
1. cp file.txt new_path/new_name.txt
Change/modify file permissions to 'execute' only for you, the current user:
1. chmod u+x my_file.txt
In reverse order:
1. sort -r text_file.txt
Words:
1. wc -w my_essay.txt
Characters:
1. wc -m some_document.txt
Option Description
Extract lines containing the word "hello", case insensitive and whole words only:
1. grep -iw hello a_bunch_of_hellos.txt
Extract lines containing the pattern "hello" from all files in the current directory
ending in .txt:
1. grep -l hello *.txt
Extract the second to fifth characters (bytes) from each line of a file:
1. cut -b 2-5 my_text_file.txt
Extract the characters (bytes) from each line of a file, starting from the 10th byte to the
end of the line:
1. cut -b 10- my_text_file.txt
Print hostname:
1. hostname
1. $ firstname=Jeff
2. $ echo $firstname
3. Jeff
The first line assigns the value Jeff to a new variable called firstname. The next line accesses and
displays the value of the variable, using the echo command along with the special character $ in
front of the variable name to extract its value, which is the string Jeff.
Thus, we have created a new shell variable called firstname for which the value is Jeff.
This is the most basic way to create a shell variable and assign it to a value all in one step.
Here's another way to create a shell variable, using the read command.
After entering
1. $ read lastname
on the command line, the shell waits for you to enter some text:
1. $ read lastname
2. Grossman
3. $
Now we can see that the value Grossman has just been stored in the variable lastname by
the read command:
1. $ read lastname
2. Grossman
3. $ echo $lastname
4. Grossman
By the way, notice that you can echo the values of multiple variables at once.
2. Jeff Grossman
As you will soon see, the read command is particularly useful in shell scripting. You can use it within a
shell script to prompt users to input information, which is then stored in a shell variable and available
for use by the shell script while it is running. You will also learn about command line arguments,
which are values that can be passed to a script and automatically assigned to shell variables.
Summary
Shell variables store values and allow users to later access them by name
You can create shell variables by declaring a shell variable and value or by using
the read command
In this exercise, you will create a simple script which will do the following:
You will also add comments to the script, which are lines starting with #. Comments are not executed
by the shell.
When used appropriately, comments can make a shell script more readable and help in debugging
the script.
1.1. Create a new script file
Step 1: On the menu on the lab screen, use File->New File to create a new file.
Step 3: Copy and paste the following lines into the newly created file.
1. 1
2. 2
3. 3
4. 4
5. 5
6. 6
7. 7
8. 8
9. 9
10. 10
11. 11
12. 12
13. 13
14. 14
15. 15
3.
6.
7. # Wait for user to enter a name, and save the entered name into the variable \'name\'
8. read name
9.
12.
13. # The following message should print on a single line. Hence the usage of \'-n\'
14. echo -n "Congratulations! You just created and ran your first shell script "
Open a new terminal by clicking on the menu bar and selecting Terminal->New Terminal, as in the
image below.
This will open a new terminal at the bottom of the screen.
Run the commands below in the newly opened terminal.
Let's check the permissions for this new file by entering the following:
1. 1
1. ls -l greet.sh
If the file exists and has read permissions, run the following command to execute it:
1. 1
1. bash greet.sh
You should now see the welcome messages displayed on screen with your entered name.
Congratulations! You have succesfully executed your first Bash shell script.
Exercise 2 - Using a shebang line
In this exercise, you will edit the greet.sh script you created in the previous exercise by adding a
'shebang' and making it an executable file.
This is done to ensure that the name of the script can be used like a command. Adding this special
shebang line lets you specify the path to the interpreter of the script - in this case, the Bash shell.
Follow the steps below to learn how to add a shebang to your script.
The which command helps you find out the path of the command bash.
1. 1
1. which bash
2.2. Edit the script greet.sh and add the shebang line to the script
Open the file and add the following line at the beginning of the script:
1. 1
1. #! /bin/bash
One more step needs to be completed to make greet.sh completely executable by name.
To add the execute permission for the user on greet.sh, enter the following:
1. 1
1. chmod +x greet.sh
Verify whether the execute permission is granted.
Tip: Generally it's not a good idea to grant permissions to a script for all users, groups, and others.
It's more appropriate to limit the execute permission to only the owner, or the user who created the
file (you).
To change permissions for greet.sh to make the file executable for the user, run the command below:
1. 1
1. 1
1. ls -l greet.sh
If you wish to grant execute permission to everyone, you need to run the command chmod +x
greet.sh.
1. 1
1. ./greet.sh
The . here refers to the current directory. You are telling Linux to execute the script greet.sh and that
it can be found in the current directory.
Practice exercise
1. Create a script named greetnew.sh that takes the first and last names of the user, saves them in
corresponding variables firstname and lastname, and prints a welcome message, such as "Hello
<firstname> <lastname>".
Use the read command and echo commands. Write comments. Make sure to add the shebang line.
Solution:
1. 1
2. 2
3. 3
4. 4
5. 5
6. 6
7. 7
8. 8
9. 9
10. 10
11. 11
12. 12
13. 13
14. 14
15. 15
16. 16
17. 17
18. 18
19. 19
1. #! /bin/bash
2.
5.
8.
9. # Wait for user to enter a name, and save the entered name into the variable \'name\'
11.
14.
15. # Wait for user to enter a name, and save the entered name into the variable \'name\'
17.
1. 1
Step 5: Execute the file from the command prompt using the following command:
1. 1
1. ./greetnew.sh
Summary
In this lab, you'll take a closer look at how you can use pipes and filters to solve basic data processing
problems.
Pipe examples
Combining commands
Let's start with a commonly used example. Recall the following commands:
sort - sorts the lines of text in a file and displays the result
uniq - prints a text file with any consecutive, repeated lines collapsed to a single line
With the help of the pipe operator, you can combine these commands to print all the unique lines in
a file.
Suppose you have the file pets.txt with the following contents:
1. $ cat pets.txt
2. goldfish
3. dog
4. cat
5. parrot
6. dog
7. goldfish
8. goldfish
1. $ sort pets.txt
2. cat
3. dog
4. dog
5. goldfish
6. goldfish
7. goldfish
8. parrot
The file is sorted, but there are duplicated lines of "dog" and "goldfish".
On the other hand, if you only use uniq, you get:
1. $ uniq pets.txt
2. goldfish
3. dog
4. cat
5. parrot
6. dog
7. goldfish
This time, you removed consecutive duplicates, but non-consecutive duplicates of "dog" and
"goldfish" remain.
But by combining the two commands in the correct order - by first using sort then uniq - you get
back:
2. cat
3. dog
4. goldfish
5. parrot
Since sort sorts all identical items consecutively, and uniq removes all consecutive duplicates,
combining the commands prints only the unique lines from pets.txt!
Some commands such as tr only accept standard input - normally text entered from your keyboard -
but not strings or filenames.
In cases like this, you can use piping to apply the command to strings and file contents.
With strings, you can use echo in combination with tr to replace all the vowels in a string with
underscores _:
To perform the complement of the operation from the previous example - or to replace all
the consonants (any letter that is not a vowel) with an underscore - you can use the -c option:
2. _i_u__a_____e______i__i___a_e_a_e_o_e_
With files, you can use cat in combination with tr to change all of the text in a file to uppercase as
follows:
2. GOLDFISH
3. DOG
4. CAT
5. PARROT
6. DOG
7. GOLDFISH
8. GOLDFISH
The possibilities are endless! For example, you could add uniq to the above pipeline to only return
unique lines in the file, like so:
2. CAT
3. DOG
4. GOLDFISH
5. PARROT
Let's see how you can use this json file to get the current price of Bitcoin (BTC) in USD, by using grep
command.
1. {
2. "coin": {
3. "id": "bitcoin",
4. "icon": "https://static.coinstats.app/coins/Bitcoin6l39t.png",
5. "name": "Bitcoin",
6. "symbol": "BTC",
7. "rank": 1,
8. "price": 57907.78008618953,
9. "priceBtc": 1,
19. "exp": [
20. "https://blockchair.com/bitcoin/",
21. "https://btc.com/",
22. "https://btc.tokenview.com/"
23. ]
24. }
25. }
The JSON field you want to grab here is "price": [numbers].[numbers]". To get this, you can use the
following grep command to extract it from the JSON text:
: matches :
Use the cat command to get the output of the JSON file and pipe it with the grep command to get
the required output.
You can also extract information directly from URLs and retreive any specific data using such grep
commands.
Click here to see the process of extracting information directly from URLs and retreiving specific data:
1.
2.
3.
4.
5.
6.
7.
8. Utilize the ‘grep’ command, following the guidelines outlined earlier, to fetch
the necessary data.
Summary
Pipes are commands in Linux which allow you to use the output of one command as the
input of another
You can combine commands such as sort and uniq to organize strings and text file contents
You can pipe the output of a curl command to grep to extract components of URL data
Learning Objectives
Metacharacters
Metacharacters are characters having special meaning that the shell interprets as instructions.
Metacharacter Meaning
# Precedes a comment
; Command separator
Pound #
The pound # metacharacter is used to represent comments in shell scripts or configuration files. Any
text that appears after a # on a line is treated as a comment and is ignored by the shell.
1. 1
2. 2
3. 3
4. 4
1. #!/bin/bash
2.
3. # This is a comment
Comments are useful for documenting your code or configuration files, providing context, and
explaining the purpose of the code to other developers who may read it. It's a best practice to
include comments in your code or configuration files wherever necessary to make them more
readable and maintainable.
Semicolon ;
The semicolon ; metacharacter is used to separate multiple commands on a single command line.
When multiple commands are separated by a semicolon, they are executed sequentially in the order
they appear on the command line.
1. 1
2. 2
3. 3
2. Hello,
3. world!
As you can see from the example above, the output of each echo command is printed on separate
lines and follows the same sequence in which the commands were specified.
The semicolon metacharacter is useful when you need to run multiple commands sequentially on a
single command line.
Asterisk *
The asterisk * metacharacter is used as a wildcard character to represent any sequence of characters,
including none.
1. 1
1. ls *.txt
In this example, *.txt is a wildcard pattern that matches any file in the current directory with
a .txt extension. The ls command lists the names of all matching files.
Question mark ?
The question mark ? metacharacter is used as a wildcard character to represent any single character.
1. 1
1. ls file?.txt
In this example, file?.txt is a wildcard pattern that matches any file in the current directory with a
name starting with file, followed by any single character, and ending with the .txt extension.
Quoting
Quoting is a mechanism that allows you to remove the special meaning of characters, spaces, or
other metacharacters in a command argument or shell script. You use quoting when you want the
shell to interpret characters literally.
Symbol Meaning
Backslash \
The backslash character is used as an escape character. It instructs the shell to preserve the literal
interpretation of special characters such as space, tab, and $. For example, if you have a file with
spaces in its name, you can use backslashes followed by a space to handle those spaces literally:
1. 1
When a string is enclosed in double quotes, most characters are interpreted literally, but
metacharacters are interpreted according to their special meaning. For example, you can access
variable values using the dollar $ character:
1. 1
2. 2
2. Hello <username>
1. 1
2. 2
2. Hello $USER
Notice that instead of printing the value of $USER, single quotes cause the terminal to print the
string "$USER".
Input/Output redirection
Symbol Meaning
Input/output (IO) redirection is the process of directing the flow of data between a program and its
input/output sources.
By default, a program reads input from standard input, the keyboard, and writes output to standard
output, the terminal. However, using IO redirection, you can redirect a program's input or output to
or from a file or another program.
This symbol is used to redirect the standard output of a command to a specified file.
ls > files.txt will create a file called files.txt if it doesn't exist, and write the output of the ls command
to it.
Warning: When the file already exists, the output overwrites all of the file's contents!
This notation is used to redirect and append the output of a command to the end of a file. For
example,
ls >> files.txt appends the output of the ls command to the end of file files.txt, and preserves any
content that already existed in the file.
Redirect standard output 2>
This notation is used to redirect the standard error output of a command to a file. For example, if you
run the ls command on a non-existing directory as follows,
ls non-existent-directory 2> error.txt the shell will create a file called error.txt if it doesn't exist, and
redirect the error output of the ls command to the file.
Warning: When the file already exists, the error message overwrites all of the file's contents!
This symbol redirects the standard error output of a command and appends the error message to the
end of a file without overwriting its contents.
ls non-existent-directory 2>> error.txt will append the error output of the ls command to the end of
the error.txt file.
This symbol is used to redirect the standard input of a command from a file or another command.
For example,
sort < data.txt will sort the contents of the data.txt file.
Command Substitution
Command substitution allows you to run command and use its output as a component of another
command's argument. Command substitution is denoted by enclosing a command in either backticks
(`command`) or using the $() syntax. When the encapsulate command is executed, its output is
substituted in place, and it can be used as an argument within another command. This is particularly
useful for automating tasks that require the use of a command's output as input for another
command.
For example, you could store the path to your current directory in a variable by applying command
substitution on the pwd command, then move to another directory, and finally return to your
original directory by invoking the cd command on the variable you stored, as follows:
1. 1
2. 2
3. 3
1. $ here=$(pwd)
2. $ cd path_to_some_other_directory
3. $ cd $here
Command line arguments are additional inputs that can be passed to a program when the program
is run from a command line interface. These arguments are specified after the name of the program,
and they can be used to modify the behavior of the program, provide input data, or provide output
locations. Command line arguments are used to pass arguments to a shell script.
For example, the following command provides two arguments, arg1, and arg2, that can be accessed
from within your Bash script:
1. 1
Summary
Metacharacters such as # ; * and ? are characters that the shell interprets with special
meanings
Quoting allows you to ensure any special characters, spaces, or other metacharacters are
interpreted literally by the shell
Command substitution allows you to use the output of a command as an argument for
another command
In the hands-on lab portion of the final project, you will be using more advanced scripting commands
and concepts that the course has not covered yet. This reading will familiarize you with these more
advanced concepts, so you can complete the lab with confidence.
Objectives
After completing this reading, you will be able to create Bash scripts that:
use conditional statements to run a set of commands only if a specified condition is true
Conditionals
Conditionals, or if statements, are a way of telling a script to do something only under a specific
condition.
Bash script conditionals use the following if-then-else syntax:
1. 1
2. 2
3. 3
4. 4
5. 5
6. 6
1. if [ condition ]
2. then
3. statement_block_1
4. else
5. statement_block_2
6. fi
If the condition is true, then Bash executes the statements in statement_block_1 before exiting the
conditional block of code. After exiting, it will continue to run any commands after the closing fi.
Alternatively, if the condition is false, Bash instead runs the statements in statement_block_2 under
the else line, then exits the conditional block and continues to run commands after the closing fi.
Tips:
You must always put spaces around your condition within the square brackets [ ].
Every if condition block must be paired with a fi to tell Bash where the condition block ends.
The else block is optional but recommended. If the condition evaluates to false without
an else block, then nothing happens within the if condition block. Consider options such as
echoing a comment in statement_block_2 to indicate that the condition was evaluated
as false.
In the following example, the condition is checking whether the number of command-line arguments
read by some Bash script, $#, is equal to 2.
1. 1
2. 2
3. 3
4. 4
5. 5
6. 6
1. if [[ $# == 2 ]]
2. then
4. else
6. fi
Notice the use of the double square brackets, which is the syntax required for making integer
comparisons in the condition [[ $# == 2 ]].
You can also make string comparisons. For example, assume you have a variable
called string_var that has the value "Yes" assigned to it. Then the following statement evaluates
to true:
1. 1
1. `[ $string_var == "Yes" ]`
Notice you only need single square brackets when making string comparisons.
You can also include multiple conditions to be satified by using the "and" operator && or the "or"
operator ||. For example:
1. 1
2. 2
3. 3
4. 4
5. 5
6. 6
2. then
4. else
6. fi
1. 1
2. 2
3. 3
4. 4
5. 5
6. 6
1. if [ condition1 ] || [ condition2 ]
2. then
4. else
6. fi
Logical operators
The following logical operators can be used to compare integers within a condition in an if condition
block.
==: is equal to
If a variable a has a value of 2, the following condition evaluates to true; otherwise it evalutes
to false.
1. 1
1. $a == 2
If a variable a has a value different from 2, the following statement evaluates to true. If its value is 2,
then it evalutes to false.
1. 1
1. a != 2
Tip: The ! logical negation operator changes true to false and false to true.
1. 1
1. a <= 3
and the following statement evalutates to false:
1. 1
1. a <= 1
Alternatively, you can use the equivalent notation -le in place of <=:
1. 1
2. 2
3. 3
4. 4
5. 5
6. 6
7. 7
8. 8
1. a=1
2. b=2
3. if [ $a -le $b ]
4. then
6. else
8. fi
We've only provided a small sampling of logical operators here. You can explore resources such as
the Advanced Bash-Scripting Guide to find out more.
Arithmetic calculations
You can perform integer addition, subtraction, multiplication, and division using the notation $(()).
For example, the following two sets of commands both display the result of adding 3 and 2.
1. 1
1. echo $((3+2))
or
1. 1
2. 2
3. 3
4. 4
1. a=3
2. b=2
3. c=$(($a+$b))
4. echo $c
Bash natively handles integer arithmetic but does not handle floating-point arithmetic. As a result, it
will always truncate the decimal portion of a calculation result.
For example:
1. 1
1. echo $((3/2))
prints the truncated integer result, 1, not the floating-point number, 1.5.
Symbol Operation
+ addition
- subtraction
* multiplication
/ division
Arrays
The array is a Bash built-in data structure. An array is a space-delimited list contained in
parentheses.ʊTo create an array, declare its name and contents:
1. 1
1. 1
1. declare -a empty_array
If you want to add items to your array after creating it, you can add to your array by appending one
element at a time:
1. 1
2. 2
1. my_array+=("six")
2. my_array+=(7)
1. 1
2. 2
3. 3
4. 4
5. 5
6. 6
7. 7
8. 8
2. echo ${my_array[0]}
3.
5. echo ${my_array[2]}
6.
8. echo ${my_array[@]}
Tip: Note that array indexing starts from 0, not from 1.
for loops
You can use a construct called a for loop along with indexing to iterate over all elements of an array.
For example, the following for loops will continue to run over and over again until every element is
printed:
1. 1
2. 2
3. 3
2. echo $item
3. done
or
1. 1
2. 2
3. 3
1. for i in ${!my_array[@]}; do
2. echo ${my_array[$i]}
3. done
The for loop requires a ; do component in order to cycle through the loop. Additionally, you need to
terminate the for loop block with a done statement.
Another way to implement a for loop when you know how many iterations you want is as follows.
For example, the following code prints the number 0 through 6.
1. 1
2. 2
3. 3
4. 4
1. N=6
3. echo $i
4. done
You can use for loops to accomplish all sorts of things. For example, you could count the number of
items in an array or sum up its elements, as the following Bash script does:
1. 1
2. 2
3. 3
4. 4
5. 5
6. 6
7. 7
8. 8
9. 9
10. 10
11. 11
12. 12
13. 13
14. 14
15. 15
1. #!/usr/bin/env bash
3. my_array=(1 2 3)
4. count=0
5. sum=0
6. for i in ${!my_array[@]}; do
8. echo ${my_array[$i]}
10. count=$(($count+1))
12. sum=$(($sum+${my_array[$i]}))
13. done
14. echo $count
Go ahead and try running this script, so you get a sense of how this loop works.
Summary
You can create list-like arrays and access their individual elements
1Hands-on Lab: Scheduling Jobs using crontab2About Skills Network Cloud IDE3Exercise 1 -
Understand crontab file syntax4Exercise 2 - List cron jobs5Exercise 3 - Add a job in the crontab
file6Exercise 4 - Remove the current crontab7Practice exercises8Summary
Exercise 1 - Understand crontab file syntax
Cron is a system daemon used to execute desired tasks in the background at designated times.
A crontab file is a simple text file containing a list of commands meant to be run at specified times. It
is edited using the crontab command.
Each line in a crontab file has five time-and-date fields, followed by a command, followed by a
newline character (\n). The fields are separated by spaces.
The five time-and-date fields cannot contain spaces and their allowed values are as follows:
minute 0-59
day 1-31
month 1-12
1Hands-on Lab: Scheduling Jobs using crontab2About Skills Network Cloud IDE3Exercise 1 -
Understand crontab file syntax4Exercise 2 - List cron jobs5Exercise 3 - Add a job in the crontab
file6Exercise 4 - Remove the current crontab7Practice exercises8Summary
Open a new terminal, by clicking on the menu bar and selecting Terminal->New Terminal, as in the
image below.
This will open a new terminal at the bottom of the screen as in the image below.
Run the commands below on the newly opened terminal.
1. 1
1. crontab -l
You may get a message no crontab for theia if your crontab is empty.
1Hands-on Lab: Scheduling Jobs using crontab2About Skills Network Cloud IDE3Exercise 1 -
Understand crontab file syntax4Exercise 2 - List cron jobs5Exercise 3 - Add a job in the crontab
file6Exercise 4 - Remove the current crontab7Practice exercises8Summary
1. 1
1. crontab -e
This will create a new crontab file for you (if you don't have one already). Now you are ready to add a
new cron job.
Your crontab file will be opened in an editor as shown in the image below:
Scroll down to the end of the file using the arrow keys:
Add the below line at the end of the crontab file:
1. 1
The above job specifies that the echo command should run when the minute is 0 and the hour is 21.
It effectively means the job runs at 9.00 p.m every day.
Press y to confirm.
Press Enter to come out of the editor.
Check if the job is added to the crontab by running the following command.
1. 1
1. crontab -l
Let us create a simple shell script that prints the current time and the current disk usage statistics.
Step 1: On the menu on the lab screen, use File->New File to create a new file:
1. 1
2. 2
3. 3
4. 4
5. 5
1. #! /bin/bash
3. date
5. df -h
1. 1
2. 2
2. ./diskusage.sh
The script should print the current timestamp and the disk usage stats.
Let us schedule this script to be run everyday at midnight 12:00 (when the hour is 0 on the 24 hour
clock).
We want the output of this script to be appended to /home/project/diskusage.log.
1. 1
1. crontab -e
1. 1
1. 0 0 * * * /home/project/diskusage.sh >>/home/project/diskusage.log
Check if the job is added to the crontab by running the following command:
1. 1
1. crontab -l
1Hands-on Lab: Scheduling Jobs using crontab2About Skills Network Cloud IDE3Exercise 1 -
Understand crontab file syntax4Exercise 2 - List cron jobs5Exercise 3 - Add a job in the crontab
file6Exercise 4 - Remove the current crontab7Practice exercises8Summary
Caution: This removes all your cron jobs. Be extra cautious when you use this command on a
production server.
1. 1
1. crontab -r
1. 1
1. crontab -l
1Hands-on Lab: Scheduling Jobs using crontab2About Skills Network Cloud IDE3Exercise 1 -
Understand crontab file syntax4Exercise 2 - List cron jobs5Exercise 3 - Add a job in the crontab
file6Exercise 4 - Remove the current crontab7Practice exercises8Summary
Practice exercises
1. Create a cron job that runs the task date >> /tmp/everymin.txt every minute.
Solution:
1. 1
1. crontab -e
1. 1
1Hands-on Lab: Scheduling Jobs using crontab2About Skills Network Cloud IDE3Exercise 1 -
Understand crontab file syntax4Exercise 2 - List cron jobs5Exercise 3 - Add a job in the crontab
file6Exercise 4 - Remove the current crontab7Practice exercises8Summary
Summary
Bash shebang
1. 1
1. #!/bin/bash
Get the path to a command
1. 1
1. which bash
1. 1
1. ls | sort -r
Pipe the output of manual page for ls to head to display the first 20 lines:
1. 1
Use a pipeline to extract a column of names from a csv and drop duplicate names:
1. 1
1. 1
1. set
Define a shell variable called my_planet and assign value Earth to it:
1. 1
1. my_planet=Earth
1. echo $my_planet
1. 1
1. read first_name
Tip: Whatever text string you enter after running this command gets stored as the value of the
variable first_name.
1. 1
1. env
1. 1
2. 2
1. export my_planet
Metacharacters
Comments #:
1. 1
Command separator ;:
1. 1
1. 1
1. ls *.json
1. 1
1. ls file_2021-06-??.json
Quoting
1. 1
1. 1
1. 1
I/O Redirection
1. 1
1. 1
1. 1
1. 1
1. 1
Command Substitution
1. 1
2. 2
1. THE_PRESENT=$(date)
1. 1
1. 1
1. 1
1. 1
1. 1
1. crontab -e
1. 1
1. 1
Run a shell script on the first minute of the first day of each month:
1. 1
1. 1 0 1 * * ./My_Shell_Script.sh
Back up your home directory every Monday at 3:00 am:
1. 1
1. crontab -l
Conditionals
if-then-else syntax:
1. if [[ $# == 2 ]]
2. then
4. else
6. fi
1. if [ condition1 ] || [ condition2 ]
Logical operators
Operator Definition
== is equal to
!= is not equal to
Arithmetic calculations
1. $(())
Symbol Operation
+ addition
- subtraction
* multiplication
/ division
1. echo $((3+2))
Negate a number:
1. echo $((-1*-2))
Arrays
1. my_array+="six"
2. my_array+=7
for loops
1. for i in {0..5}; do
2. echo "this is iteration number $i"
3. done
2. echo $item
3. done
Use array indexing within a for loop, assuming the array has seven elements:
1. for i in {0..6}; do
2. echo ${my_array[$i]}
3. done
Scenario
You've been tasked by your team to create an automated Extract, Transform, Load (ETL) process to
extract daily weather forecast and observed weather data and load it into a live report to be used for
further analysis by the analytics team. As part of a larger prediction modelling project, the team
wants to use the report to monitor and measure the historical accuracy of temperature forecasts by
source and station.
As a proof-of-concept (POC), you are only required to do this for a single station and one source to
begin with. For each day at noon (local time), you will gather both the actual temperature and the
temperature forecasted for noon on the following day for Casablanca, Morocco.
At a later stage, the team anticipates extending the report to include lists of locations, different
forecasting sources, different update frequencies, and other weather metrics such as wind speed and
direction, precipitation, and visibility.
Data source
For this practice project, you'll use the weather data package provided by the open source
project wttr.in, a web service that provides weather forecast information in a simple and text-based
format. For further information, you can read more about the service on its GitHub Repo.
First, you'll use the curl command to scrape weather data via the wttr.in website. For example, to get
data for Casablanca, enter:
1. curl wttr.in/casablanca
After completing this practice project, you will be able to apply your new shell scripting skills in a
real-world scenario to:
Overview
You must extract and store the following data every day at noon, local time, for Casablanca,
Morocco:
The forecasted temperature (in degrees Celsius) for the following day at noon
Here is an example of what the the resulting weather report should look like:
year month day obs_tmp fc_temp
2023 1 1 10 11
2023 1 2 11 12
2023 1 3 12 10
2023 1 4 13 13
2023 1 5 10 9
2023 1 6 11 10
… … … … …
Feel free to try completing the entire project on your own, or follow the exercises below to guide you
through the process. Either way, check out the exercises, hints, and Solution:s.
Tip: At each step of the process, test your code to ensure it does what you intended. For more
complicated steps, break the task down into smaller, more managable steps that you can test
individually. You can test code on the command line or by running your script as you develop it. The
context will indicate the best approach.
rx_poc.log will be your POC weather report log file, or a text file which contains a growing history of
the daily weather data you will scrape. Each entry in the log file corresponds to a row as in Table 1.
Solution:
1. touch rx_poc.log
Your header should consist of the column names from Table 1, delimited by tabs.
Write the header to your weather report.
Solution:
1. header=$(echo -e "year\tmonth\tday\tobs_temp\tfc_temp")
2. echo $header>rx_poc.log
1. 1
1. echo -e "year\tmonth\tday\thour\tobs_temp\tfc_temp">rx_poc.log
Tip: Although it might seem redundant, it is better practice to use variables in these cases. Variables
make for much cleaner code, which is easier to understand and safer to modify by others or even
yourself at a later date. Using meaningful names for your variables also makes the code more "self-
documenting."
2.1. Create a text file called rx_poc.sh and make it an executable Bash script
Solution: 1
1. touch rx_poc.sh
1. #! /bin/bash
Solution: 2
1. 1
2.2. Assign the city name to Casablanca for accessing the weather report
Solution:
1. city=Casablanca
Solution:
3.1. Edit rx_poc.sh to extract the required data from the raw data file and assign them to
variables obs_temp and fc_temp
Extracting the required data is a process that will take some trial and error until you get it right. Study
the weather report you obtained in Step 2.3, determine what you need to extract, and look for
patterns.
You are looking for ways to 'chip away' at the weather report by:
Using shell commands to extract only the data you need (the signal)
Combining your filters into a pipeline (recall the use of pipes to chain filters together)
Extract only those lines that contain temperatures from the weather report, and save the result to
variables representing the temparature output.
3.1.1. Extract the current temperature, and store it in a shell variable called obs_temp
You may have noticed by now that the temperature values extracted from wttr.in are surrounded by
special formatting characters. These "hidden" characters cause the numbers to display in specific
color - for example, when you use the cat command to display your log file.
Unfortunately you cannot perform arithmetic calculations on such formatted text, so you will need to
extract the values from the surrounding formatting so you can make use of them later in this lab.
Solution:
While adding the following lines to rx_poc.sh, ensure you understand what each filter accomplishes
by using the command line. Try adding one filter at a time to see what the outcome is as you develop
the pipeline.
The first line uses the curl command to fetch weather information from wttr.in for the
specified city ($city). It then uses a combination of grep and grep -Eo to extract the current
temperature in degrees Celsius and assigns it to the variable obs_temp.
The second line (echo $obs_temp) prints the current temperature to the console.
3.1.2. Extract tomorrow's temperature forecast for noon, and store it in a shell variable
called fc_temp
Solution:
Add to rx_poc.sh:
3. echo "The forecasted temperature for noon tomorrow for $city : $fc_temp C"
The first line uses a combination of head and tail to narrow down to the line containing
tomorrow’s noon forecast temperature. grep and cut are employed to isolate and format the
temperature information, and, the numeric part of the temperature is captured using grep -
Eo.
The second line (echo $fc_temp) prints the forecast temperature for noon tomorrow to the
console.
3.2. Store the current hour, day, month, and year in corresponding shell variables
Solution:
Add to rx_poc.sh:
2. TZ='Morocco/Casablanca'
3.
4. # Use command substitution to store the current day, month, and year in corresponding shell
variables:
Note: You might be wondering why we didn't just set hour to a value of 12, since we want to get the
time at noon.
However, if we did, we would lose the ability to verify that we are actually running the code at the
correct local time.You should think of the local time as a measurement rather than a set number.
3.3. Merge the fields into a tab-delimited record, corresponding to a single row in Table 1
Append the resulting record as a row of data to your weather log file.
Solution:
Add to rx_poc.sh:
2. echo $record>>rx_poc.log
Exercise 4 - Schedule your Bash script rx_poc.sh to run every day at noon local time
4.1. Determine what time of day to run your script
Recall that you want to load the weather data coresponding to noon, local time, in Casablanca every
day.
First, check the time difference between your system's default time zone and UTC.
Solution:
Running the following commands gives you the info you need to get the time difference between
your system and UTC.
1. $ date
3. $ date -u
Now you can determine how many hours difference there are between your system's local time and
that of Casablanca. In the example above, we see that the system time relative to UTC is UTC+5 (i.e.
16 - 11 = 5).
We know Casablanca is UTC+1, so the system time relative to Casablanca is 4 hours earlier. Thus to
run your script at noon Casablanca time, you need to run it at 8 am.
Solution:
1. crontab -e
1. 0 8 * * * /home/project/rx_poc.sh
For reference, here is a Bash script that creates the raw weather report. Try to follow all the steps on
your own before looking!
1. #! /bin/bash
2.
4. city=Casablanca
5.
6. #Obtain the weather report for Casablanca
8.
12.
14. fc_temp=$(curl -s wttr.in/$city?T | head -23 | tail -1 | grep '°.' | cut -d 'C' -f2 | grep -Eo -e '-?
[[:digit:]].*')
15. echo "The forecasted temperature for noon tomorrow for $city : $fc_temp C"
16.
18. TZ='Morocco/Casablanca'
19.
20.
21. # Use command substitution to store the current day, month, and year in corresponding shell
variables:
26.
27.
Now that you've created an ETL shell script to gather weather data into a report, let's create another
script to measure and report the accuracy of the forecasted temperatures against the actuals.
Insert the following code into the file to include a header with column names:
1. echo -e "year\tmonth\tday\tobs_temp\tfc_temp\taccuracy\taccuracy_range" >
historical_fc_accuracy.tsv
One key difference between this report and the previous report you generated is that the forecast
temperature will now be aligned with the date the forecast is for. As a result, the date will be in the
same row as the observed temperature for that date, rather than the previous row on the day that
the forecast was made.
Rather than scheduling your new script to run periodically, think of it as a tool you can use to
generate the historical forecast accuracy on demand.
5.1. Determine the difference between today's forecasted and actual temperatures
Rather than writing the script to process all of the data at once, let's simplify by solving the problem
for just one instance. Later you can modify the script to handle the general case of mupltiple days.
5.1.1. Extract the forecasted and observed temperatures for today and store them in variables
Solution:
Solution:
2. accuracy=$(($yesterday_fc-$today_temp))
Tip: Your weather report must have at least two days of data for this calulation to make sense.
To test your code you can simply append some artificial data to your weather report, rx_poc.log.
Let's set the accuracy labels according to the range that the accuracy fits most tightly within,
according to the following table. Validate your result.
Solution:
2. then
3. accuracy_range=excellent
5. then
6. accuracy_range=good
8. then
9. accuracy_range=fair
10. else
11. accuracy_range=poor
12. fi
13.
Solution:
1. row=$(tail -1 rx_poc.log)
5. echo -e "$year\t$month\t$day\t$today_temp\t$yesterday_fc\t$accuracy\t$accuracy_range"
>> historical_fc_accuracy.tsv
Solution:
1. #! /bin/bash
2.
5. accuracy=$(($yesterday_fc-$today_temp))
6.
8.
10. then
11. accuracy_range=excellent
13. then
14. accuracy_range=good
16. then
17. accuracy_range=fair
18. else
19. accuracy_range=poor
20. fi
21.
23.
We leave it as an exercise for you to generalize your code to create the entire weather accuracy
history. In the next exercise, you will download and work with a synthetic version of this weather
accuracy history report.
Here are some suggestions to guide you should you wish to create the weather accuracy history
yourself:
Iterate through your weather log file using a for loop. On each iteration:
o Use head and tail to extract consecutive pairs of lines on each iteration
This provides you with the current and previous day's data
o Treat this pair of lines like you did in your code as yesterday and today's data
In this exercise, you will download a synthetic historical forecasting accuracy report and calculate
some basic statistics based on the latest week of data.
Run the following command in the terminal to download the dataset to your current working
directory.
1. 1
1. wget https://cf-courses-data.s3.us.cloud-object-storage.appdomain.cloud/IBMSkillsNetwork-
LX0117EN-Coursera/labs/synthetic_historical_fc_accuracy.tsv
6.2. Load the historical accuracies into an array covering the last week of data
Remember to make your script executable. Also validate your result by printing the array to the
terminal.
Solution:Your shell script should look something like the following. There are many ways to
accomplish this task, so the Solution: provided is not unique.
1. #!/bin/bash
2.
3. echo $(tail -7 synthetic_historical_fc_accuracy.tsv | cut -f6) > scratch.txt
4.
6.
7. # validate result:
8. for i in {0..6}; do
9. echo ${week_fc[$i]}
10. done
6.3. Display the minimum and maximum absolute forecasting errors for the week
Now use your array to calculate the minimum and maximum absolute errors over the last week. For
example, if you have a vaule of -1, change it to be 1. Echo the minimum and maximum absolute
errors to the terminal.
Solution:
Your final shell script for weekly_stats.sh should now look something like the following.
1. #!/bin/bash
2.
4.
6.
7. # validate result:
8. for i in {0..6}; do
9. echo ${week_fc[$i]}
10. done
11.
14. then
15. week_fc[$i]=$(((-1)*week_fc[$i]))
16. fi
19. done
20.
21. minimum=${week_fc[1]}
22. maximum=${week_fc[1]}
25. then
26. minimum=$item
27. fi
29. then
30. maximum=$item
31. fi
32. done
33.
Congratulations! You have finished all the modules and completed a real-world practice project.
You will soon get to put your skills to the test by completing a peer-graded, hands-on lab as the main
component of your final project.
A cumulative cheat sheet that covers the entire course, including any advanced concepts that
you will encounter in the final project
Sufficient hints and/or the required code snippets for you within each task as needed
Scenario
Imagine that you are a lead Linux developer at the top-tech company ABC International Inc. ABC
currently suffers from a huge bottleneck: each day, interns must painstakingly access encrypted
password files on core servers and back up any files that were updated within the last 24 hours. This
process introduces human error, lowers security, and takes an unreasonable amount of work.
As one of ABC Inc.'s most trusted Linux developers, you have been tasked with creating a script
called backup.sh which runs every day and automatically backs up any encrypted password files that
have been updated in the past 24 hours.
Learning Objectives
Apply the knowledge you've gained to reviewing and grading technical work submtted by
your peers
Overview
Instructions
Immediately following this reading, you will complete the hands-on lab portion of your final project
where you will create a scheduled backup script.
Your work will be graded by your peers who are also completing this assignment within the same
session. Likewise, as part of your final project requirements, you will also review the work done by
your peers.
Deliverables
You will need to submit the following items for peer review:
Screenshots clearly displaying both the code and the output for each task
Full details are provided for each task within the hands-on lab.
Grading criteria
There are a total of 20 points to be earned for 17 tasks in this final project.
Your grade will be based on the following tasks within the hands-on lab:
[Tasks 1-13]: Upload screenshot of sections from the backup.sh script displaying the correct
code (13 pts: 1 pt for each of the 13 tasks)
[Task 17]: Upload screenshot showing crontab schedule of once every day (2 pts)
How to submit
IMPORTANT: You will be prompted to save screenshots throughout the lab and to save your
completed script at the end. These will be the files that you will upload during the Project
Submission and Peer Review steps.
Please note that you must submit your screenshots in either JPEG or PNG format.
You can chat with other learners taking this course using the Discussion tab near the top of this page.
My Submission tab
Once you are ready to submit your work for peer review, simply click on the My Submission tab and
follow the instructions provided there. Good luck!
Summary
Congratulations! You've just completed a challenging, real-world practice project using many of the
concepts you've learned from this course. The knowledge you've gained has prepared you to solve
many practical real world problems. You're almost finished with this course now, and the final step in
your journey is to complete the peer-reviewed Final Project.
Write a Bash script that downloads the raw weather data, and extracts and loads the
required data
Schedule your Bash script rx_poc.sh to run every day at noon local time
Create a script to report the minimum and maximum absolute errors for the week
Cumulative Cheat Sheet - Hands-On Introduction to Linux Commands and Shell Scripting
Introduction to Linux
Comman Descriptio
Syntax Example
d n
List files
ls [OPTIONS] ls
and
List [FILE/DIRECTOR /home/user/docume
directorie
Y] nts
s at path
Print
Print
present
Working pwd pwd
working
Directory
directory
Comman Descriptio
Syntax Example
d n
Change cd
Change
cd [DIRECTORY] current /home/user/docume
Directory
directory nts
Run
command
Super sudo
with sudo apt update
user do [COMMAND]
superuser
privileges
Open file
Text with Nano
nano [FILE] nano myfile.txt
Editor text
editor
Comman Descripti
Syntax Example
d on
Return
Who Am
whoami usernam whoami
I
e
Return
current
User ID id id
user or
group ID
Display
System
uname system
Informati uname -a
[OPTIONS] informati
on
on
Display
manual
Manual man page for
man ls
Pages [COMMAND] a
comman
d
Comman Descripti
Syntax Example
d on
Transfer
curl
curl [OPTIONS] data from
Curl https://some_website.
[URL] or to
com
server
Display
date current
Date date
[OPTIONS] date and
time
Find files
and
find
directorie find /home/user -
Find [DIRECTORY]
s at name '*.txt'
[OPTIONS]
specified
path
Create
Make mkdir
new mkdir myfolder
Directory [DIRECTORY]
directory
Remove
Remove rmdir
empty rmdir myfolder
Directory [DIRECTORY]
directory
Display
process
Process
ps [OPTIONS] status ps -ef
Status
informati
on
Display
live
Table of
top system top
Processes
resource
usage
Display
Disk df [OPTIONS] disk
df -h
Usage [FILESYSTEM] space
usage
Comman Descripti
Syntax Example
d on
Create
Create new file
Empty touch [FILE] or update touch myfile.txt
File timestam
p
Copy files
or
cp [OPTIONS]
directorie cp myfile.txt
[SOURCE]
Copy s from /home/user/documen
[DESTINATION
source to ts
]
destinati
on
Move or
mv [OPTIONS]
rename mv myfile.txt
[SOURCE]
Move files and /home/user/documen
[DESTINATION
directorie ts
]
s
rm [OPTIONS]
Remove
Remove [FILE/DIRECTO rm my_scratch_file.txt
files
RY]
Remove
rm -r
nonempt
path_to_temp_directo
y
ry
directory
rmdir Remove
rmdir
[OPTIONS] empty
path_to_my_directory
[DIRECTORY] directory
Change
chmod file or
Change
[OPTIONS] directory chmod u+x myfile.txt
Mode
[MODE] [FILE] permissio
ns
Display the
Concat
cat [FILE] contents of cat myfile.txt
enate
a file
Concatenta
te and
display
cat file1 file2
contents of
multiple
files
Display file
more
More one screen more myfile.txt
[FILE]
at a time
head Display
Head [OPTION first N lines head -5 myfile.txt
S] [FILE] of file
echo Display
Echo [ARGUM arguments echo Hello, World!
ENTS] in console
Alphanum
sort
erically
Sort [OPTION sort file.txt
sort file
S] [FILE]
contents
Report or
remove
uniq
consecutiv
Unique [OPTION uniq file.txt
ely
S] [FILE]
repeated
lines in file
words, and
characters
in a file
grep
Search for
[OPTION
a specified
Grep S] grep "hello" file.txt
pattern in a
PATTERN
file
[FILE]
paste Merge
[OPTION lines of
Paste paste file1.txt file2.txt
S] [FILE1] files side
[FILE2] by side
Remove
cut sections
Cut [OPTION from each cut -d":" -f1 /etc/passwd
S] [FILE] line of a
file
Archive
tar files
tar -czvf archive.tar.gz
Tar [OPTION together
/directory
S] [FILE] into a
single file
zip Compress
zip archive.zip file1.txt
Zip [OPTION files into a
file2.txt
S] [FILE] zip archive
Uncompres
unzip
s files from
Unzip [OPTION unzip archive.zip
a zip
S] [FILE]
archive
Print the
name of
Hostna hostnam
the current hostname
me e
host
system
Comma Descriptio
Syntax Example
nd n
Send ICMP
ping
ECHO_REQ
[OPTION
UEST
Ping S] ping google.com
packets to
HOSTNA
a network
ME/IP
host
Display or
ifconfig configure
Ifconfig [INTERFA network ifconfig
CE] interface
parameters
Show or
manipulate
routing,
ip
devices,
IP [OPTION ip addr
policy
S]
routing,
and
tunnels
Transfer
curl
data from curl
Curl [OPTION
or to a https://some_website.com
S] URL
server
Com Descriptio
Syntax Example
mand n
Chain any
Pipe filter1 | filter2 number of ls | sort -r
filters
Com Descriptio
Syntax Example
mand n
Locat Display
e which location
which bash
execu [EXECUTABLE] of bash exe
table cutable
Interpret
and run
Bash bash [SCRIPT] script bash script.txt
using Bash
shell
List all
Set set [OPTION] shell set
variables
Define
Defin shell
e [VARIABLE_NAM variable by
name="John"
varia E]=[VALUE] name and
ble assign
value
Read from
standard
Read read [VARIABLE] input and read name
store result
in variable
Print all
environme
nt
Env env env
variables
and their
values
Extend
scope of
Expor export local
export name
t [VARIABLE] variable to
all child
processes
Com Descriptio
Syntax Example
mand n
Open
Cront crontab crontab
crontab -e
ab [OPTIONS] default
editor
Sched
ule
tasks
to Append
run at date/time
speci m h dom mon to file 15 18 * * 0 date >>
fied dow command every sundays.txt
times Sunday at
using 6:15 pm
cron
daem
on
Back up
home 0 3 * * 1 tar -cvf
directory my_backup_path\
every my_archive.tar.gz
Monday at $HOME\
3:00 am
Run shell
script first
minute of 101**
first day of ./My_Script.sh
each
month
In this scenario, you are a lead Linux developer at the top-tech company ABC International Inc. As
one of ABC Inc.'s most trusted Linux developers, you have been tasked with creating a script
called backup.sh which runs every day and automatically backs up any encrypted password files
that have been updated in the past 24 hours.
Please complete the following tasks, and be sure to follow the directions as you go. Don't forget to
save your work.
Getting started
Task 0
1. Open a new terminal by clicking on the menu bar and selecting Terminal->New Terminal:
1. 1
1. wget https://cf-courses-data.s3.us.cloud-object-storage.appdomain.cloud/IBM-LX0117EN-
SkillsNetwork/labs/Final%20Project/backup.sh
Copied!
1. You will notice the template script contains comments (lines starting with the # symbol).
Do not delete these.
The ones that look like # [TASK {number}] will be used by your grader:
2. Also, please do not modify any existing code above # [TASK 1] in the script.
1. Save the current working file (backup.sh) with CTRL+s [Windows/Linux], CMD+s [MAC], or
navigate to File->Save as seen below:
2. Download the file to your local computer by navigating to File->Download as seen below:
3. Unfortunately, our editor does not currently support file uploading, so you will need to
copy and paste your work as follows:
Task 1
Set two variables equal to the values of the first and second command line arguments, as follows:
The command line arguments interpreted by the script can be accessed via $1 (first argument)
and $2 (second argument).
Task 2
1. Display the values of the two command line arguments in the terminal.
Task 3
Remember you can customize the output format of the date command.
To set a variable equal to the output of a command you can use command substitution: $() or ` `
Task 4
1. Define a variable called backupFileName to store the name of the archived and
compressed backup file that the script will create.
For example, if currentTS has the value 1634571345, then backupFileName should have
the value backup-1634571345.tar.gz.
Task 5
1. Define a variable called origAbsPath with the absolute path of the current directory as the
variable's value.
You can get the absolute path of the current directory using the pwd command.
Task 6
1. Define a variable called destAbsPath whose value equals the absolute path of the
destination directory.
First use cd to go to destinationDirectory, then use the same method you used in Task 5.
Task 7
Task 8
You need to find files that have been updated within the past 24 hours. This means you need to
find all files whose last-modified date was 24 hours ago or less.
To do make this easier:
1. Define a numerical variable called yesterdayTS as the timestamp (in seconds) 24 hours
prior to the current timestamp, currentTS.
zero=$((3 * 5 - 6 - 9))
Thus, to get the timestamp in seconds of 24 hours in the future, you would use:
tomorrowTS=$(($currentTS + 24 * 60 * 60))
1Linux Commands and Shell Scripting - Final Project2Getting started34Checkpoint56Note on
arrays78Checkpoint91011Congratulations!
Note on arrays
1. 1
1. declare -a toBackup
Copied!
This line declares a variable called toBackup, which is an array. An array contains a list of values,
and you can append items to arrays using the following syntax:
1. 1
1. myArray+=($myVariable)
Copied!
When you print or echo an array, you will see its string representation, which is simply all of its
values separated by spaces:
1. 1
2. 2
3. 3
4. 4
5. 5
6. 6
1. $ declare -a myArray
2. $ myArray+=("Linux")
3. $ myArray+=("is")
4. $ myArray+=("cool!")
5. $ echo ${myArray[@]}
6. Linux is cool!
Copied!
This will be useful later in the script where you will pass the array $toBackup, consisting of the
names of all files that need to be backed up, to the tar command. This will archive all files at once!
Task 9
1. Within the $() expression inside the for loop, write a command that will return all files and
directories in the current folder.
Task 10
1. Inside the for loop, you want to check whether the $file was modified within the last 24
hours.
To get the last-modified date of a file in seconds, use date -r $file +%s then compare the value
to yesterdayTS.
if [[ $file_last_modified_date > $yesterdayTS ]] then the file was updated within the last 24 hours!
2. Since much of this wasn’t covered in the course, for this task you may copy the code below
and paste it into the double round brackets (()):
1. 1
Copied!
Task 11
1. In the if-then statement, add the $file that was updated in the past 24-hours to
the toBackup array.
2. Since much of this wasn’t covered in the course, you may copy the code below and place
after the then statement for this task:
1. 1
1. toBackup+=($file)
Copied!
Task 12
1. After the for loop, compress and archive the files, using the $toBackup array of filenames,
to a file with the name backupFileName.
Task 13
Congratulations! You have now done the coding portion of the lab!
Task 14
1. Save the current working file backup.sh with CTRL+s [Windows/Linux], CMD+s [MAC] or by
navigating to File->Save as seen below:
2. Download the file to your local computer by navigating to File->Download as seen below:
You may save the file as backup.sh
Task 15
1. Open a new terminal by clicking on the menu bar and selecting Terminal->New Terminal,
as in the image below:
This will open a new terminal at the bottom of the screen as seen below:
1. 1
1. ls -l backup.sh
Copied!
4. Take a screenshot of the output of the command above and save it as 15-
executable.jpg or .png.
Task 16
1. 1
1. wget https://cf-courses-data.s3.us.cloud-object-storage.appdomain.cloud/IBM-LX0117EN-
SkillsNetwork/labs/Final%20Project/important-documents.zip
Copied!
1. 1
Copied!
1. 1
1. touch important-documents/*
Copied!
1. 1
1. ./backup.sh important-documents .
Copied!
Task 17
1. Copy the backup.sh script into the /usr/local/bin/ directory. (Do not use mv.)
Note: You may need to use sudo cp in order to create a file in /usr/local/bin/.
2. Test the cronjob to see if the backup script is getting triggered by scheduling it for every 1
minute.
1. 1
Copied!
3. Please note that since the Theia Lab is a virtual environment, we need to explicitly start
the cron service using the below command:
1. 1
Copied!
4. Once the cron service is started, check in the directory /home/project to see if the .tar files
are being created.
5. If they are, then stop the cron service using the below command, otherwise it will continue
to create .tar files every minute:
1. 1
Copied!
Tip: When you are setting up cron jobs in a real-life scenario, ensure the cron service is running, or
start the cron service if needed.