Advanced
Searching inSummaries
How to search
  • Words find lesson titles, text and code. Filter by type (guides, courses, programs, books) or stay in this book.
  • book:rhcsa and ch:8 narrow the search; #code finds a word only in commands of the lessons.
  • "two words" finds the exact phrase.
  • Plain words find names, texts and examples.
  • ↑ ↓ to move · Enter to open · Esc to close · / to search
Try:

Course 6Name in the course: Hands-on Introduction to Linux Commands and Shell Scripting

Hands-on Introduction to Linux Commands and Shell Scripting

Course 6 of 16 in the IBM DevOps and Software Engineering certificate. This course teaches Linux and UNIX shell commands.

Note

My result — Course completion: 100%; Grade: 100% (pass = 50%); Certificate: not offered on Cognitive Class for this edition.

Course page: Coursera · Cognitive Class

  • 11 Lessons
  • 38 Commands
  • 2 Labs

$ run as a normal user# run as root (or with sudo)

6.1

What the course covers

What Linux is and why servers use it.

  • How to move around the Linux file system.
  • Commands to create, copy, move, and delete files.
  • Networking, text, and archive commands.
  • How to write and run shell scripts.
  • A final project and a final exam.
ModuleMain focus
Module 1 · about 3 hoursIntroduction to Linux: the OS, the kernel, the shell, the file system
Module 2 · about 5 hoursIntroduction to Linux Commands: info, navigation, files, networking, archives
Module 3 · about 4 hoursIntroduction to Shell Scripting: variables, logic, loops, functions
Module 4 · about 6 hoursFinal Project and Final Exam: practice project, final project, exam

6.2

Module 1: Introduction to Linux

Introduction to Linux: the OS, the kernel, the shell, the file system.

  • Linux is an open source operating system family built on the Linux kernel. Torvalds started it in 1991, and anyone can read, change, and share the code.
  • The kernel is the core of the OS; it manages the CPU, memory, and hardware devices.
  • Shell: a program that reads and runs your commands. Bash is the most common shell. The terminal is its window.
  • The file system is a tree. It starts at the root / and shows a full path like /home/student/notes.txt.
  • Common directories: / root, /home user home folders, /etc config files, /bin programs, /var changeable data, /tmp temporary files.
  • Absolute path starts with /; relative path starts from the current folder. ~ is home, . is here, .. is the parent.
  • Linux is open source. Anyone can read, change, and share its code.
  • The kernel sits at the centre. It shares the CPU, memory, and devices.
  • Bash is a shell. It takes your typed line and runs it.
  • The terminal is the window that connects you to the shell.
  • Folders form a tree. The top is /.
  • /etc keeps settings, /bin keeps programs, /var keeps data that changes, /tmp keeps short-lived files.
  • A path from / is absolute. A path from where you stand is relative.
  • ~ means your home folder. . means this folder. .. means the folder above.

6.3

Module 2: Introduction to Linux Commands

Introduction to Linux Commands: info, navigation, files, networking, archives.

6.4

Module 3: Introduction to Shell Scripting

Introduction to Shell Scripting: variables, logic, loops, functions.

  • A script is a text file full of commands that you run with one call. Start it with a shebang: #!/bin/bash, which says which shell runs the file.
  • Make the file executable with chmod +x script.sh, then run it with ./script.sh (or bash script.sh).
  • Variables: name="Hosam" then echo $name; use $ to read a value and no spaces around =. Double quotes expand variables, single quotes do not.
  • Command substitution puts a command's output into a variable: today=$(date).
  • Arguments arrive as $1, $2, ...; $# counts them and $0 is the script name. Conditionals use if [ "$x" -eq 5 ]; then ...; fi. Strings compare with =, numbers with -eq, -gt, -lt.
  • Test a file with [ -f file ] (is a file), [ -d dir ] (is a folder), [ -z "$x" ] (empty string).
  • Loops: for f in *.txt; do echo $f; done, and while read line; do ...; done < file. Exit code 0 means success; read it with $?.
  • set -e stops the script when a command fails; exit 1 ends it with an error. A function groups commands: name() { echo hi; }.
  • Scheduling: crontab -e edits cron jobs; a cron line has five time fields, then the command.
  • A script is just a file of commands. Line one names the shell: #!/bin/bash.
  • Give it the execute bit with chmod +x, then call ./script.sh.
  • Set a value with name="Hosam" and read it with $name. Spaces around = break it.
  • Double quotes fill in variables. Single quotes keep the text as typed.
  • $(...) runs a command and keeps its output.
  • Inside a script, $1 is the first input, $# counts inputs, $0 is the file name.
  • if [ ... ]; then ...; fi guards a block. = compares text. -eq, -gt, -lt compare numbers.
  • -f asks for a file. -d asks for a folder. -z asks for an empty string.
  • for f in *.txt; do ...; done walks a list. while read l; do ...; done < f walks lines.
  • $? holds the last exit code. Zero means good.
  • set -e ends the run at the first failure. exit 1 reports a failure yourself.
  • A function gives a name to a group of lines.
  • crontab -e opens the job table. Five time fields come before the command.

6.5

Module 4: Final Project and Final Exam

Final Project and Final Exam: practice project, final project, exam.

  • A practice project comes first, then a graded final project with Linux tasks or a script. Plan, test, then submit.
  • Start with the practice project, which has no grade. Then the graded project: Linux work or a script. Test it first.
  • The exam covers all four modules.

6.6

Course topics and key ideas

A shell lets you control the computer with text.

  • The course covers Linux and UNIX shell commands.
  • Bash is the shell used for scripting in this course.
  • You can move through folders and edit files.
  • Cron runs jobs on a schedule.
  • Scripts let you repeat tasks.

Topics of the course

  • What a shell is.
  • Linux and UNIX basics.
  • Basic commands.
  • Finding your way in folders.
  • Viewing and editing files.
  • Command options and help.
  • Pipes and redirection.
  • Users and permissions.
  • Bash script basics.
  • Variables and loops.
  • Cron jobs.
  • Command line use in cloud and data work.

Why it matters

  • Many servers use Linux.
  • A shell works well over a remote connection.
  • Scripts save time on repeated work.
  • Cron can run routine jobs without a person.

6.7

Key terms

The words of this course in one table. Learn them before the quiz or exam.

TermMeaning
Absolute pathStarts at /. Relative path = starts where you are.
ArchiveMany files in one. tar -xvf opens it.
BashA common Linux shell used for scripts.
chmodChange rights. chown = change owner.
chmod +xAllow the file to run.
CommandA word or short line that tells the shell what to do.
CronTimed jobs. Five fields set the time.
df -hDisk space in plain units.
DirectoryA folder.
Environment variableA named value that programs can read.
FileA saved set of data or text.
grepFind matching lines.
KernelThe part that talks to hardware.
LinuxA free, open-source operating system.
Open sourceAnyone can read and change the code.
PathThe address of a file or folder.
PermissionA rule about who can read, write, or run a file.
PipeLeft output becomes right input.
Rights orderOwner, group, others.
rm -rRemove a folder with its contents.
Root/, the top folder.
rwxRead, write, execute.
ScriptA file with commands that run in order.
set -eStop on the first error.
ShebangThe #! line that picks the shell.
ShellThe program that runs your typed lines.
TerminalThe window for the shell.
UNIXAn older family of operating systems. Linux is like it.

6.8

Check yourself

Answer each question first, then open it to check the answer.

1What does the Linux kernel do? a) It hosts websites · b) It manages the CPU, memory, and hardware · c) It edits text files · d) It stores Git history

b) It manages the CPU, memory, and hardware. The kernel is the core of the OS and controls hardware and memory.

2Which command shows your current user name? a) pwd · b) whoami · c) uname · d) date

b) whoami. whoami prints the current user.

3Which command shows the current directory path? a) cd · b) ls · c) pwd · d) mv

c) pwd. pwd prints the working directory.

4You want to delete a folder and everything inside it. Which command is correct? a) rm folder · b) rm -r folder · c) mkdir folder · d) cp folder

b) rm -r folder. -r means recursive, so the folder and its contents go.

5In -rwxr-xr-x, what does the first group rwx describe? a) Permissions of the owner · b) Permissions of all other users · c) The file size · d) The file owner's name

a) Permissions of the owner. The first rwx is for the owner; the next r-x is for group and others.

6What does the pipe | do? a) Saves output to a file · b) Sends the output of one command into the next · c) Deletes a file · d) Runs a command in the background

b) Sends the output of one command into the next. A pipe connects the output of one command to the input of the next.

7Which command finds lines that match a pattern in a file? a) grep · b) cat · c) pwd · d) curl

a) grep. grep searches lines for a pattern.

8What does >> do? a) Reads input from a file · b) Adds output to the end of a file · c) Replaces the file with new output · d) Sends a file over the network

b) Adds output to the end of a file. > overwrites a file; >> appends to it.

9What is the first line of a Bash script usually called? a) The shebang, for example #!/bin/bash · b) The variable line · c) The comment block · d) The exit code

a) The shebang, for example #!/bin/bash. The shebang tells the system which interpreter runs the script.

10How do you make a script file runnable with ./script.sh? a) chmod +x script.sh · b) chmod -r script.sh · c) rm script.sh · d) cat script.sh

a) chmod +x script.sh. chmod +x adds the execute bit.

11Which command creates a zip archive of a folder? a) tar -xvf out.tar · b) zip -r out.zip folder · c) unzip out.zip · d) gzip -d out.zip

b) zip -r out.zip folder. -r means recursive, so the whole folder is included.

12Which part of Linux shares the CPU and memory between programs? a) The shell · b) The kernel · c) The terminal · d) The home folder

b) The kernel. The kernel sits between the programs and the hardware.

13You type whoami. What comes back? a) Your user name · b) The folder path · c) The kernel version · d) The free space

a) Your user name. whoami prints the account name and nothing else.

14Which path is relative? a) /etc/hosts · b) ../notes.txt · c) /home · d) /

b) ../notes.txt. It starts from the current folder, not from /.

15You are in /home/student/docs and run cd ... Where are you? a) /home/student · b) /home · c) /home/student/docs · d) /

a) /home/student. .. is the folder one level up.

16Which listing includes files that start with a dot? a) ls · b) ls -a · c) ls -l · d) ls -t

b) ls -a. -a adds hidden entries, which begin with a dot.

17Which command makes nested folders in one call? a) mkdir -p /data/logs · b) mkdir /data/logs · c) touch /data/logs · d) cp /data/logs

a) mkdir -p /data/logs. -p builds every missing parent folder.

18In -rw-r--r--, what may the owner do? a) Read only · b) Read and write · c) Execute only · d) Nothing

b) Read and write. The owner holds rw, so read and write.

19Which command lets the owner run deploy.sh? a) chmod u+x deploy.sh · b) chmod u-x deploy.sh · c) chown u+x deploy.sh · d) rm deploy.sh

a) chmod u+x deploy.sh. u+x adds execute for the user who owns the file.

20Which tool prints the lines of a file that contain ERROR? a) head · b) grep · c) touch · d) mv

b) grep. grep keeps only the lines that match.

21What does cat a.txt | sort do? a) Saves a sorted copy · b) Sorts the text and prints it · c) Deletes a.txt · d) Edits a.txt

b) Sorts the text and prints it. The pipe feeds the file text into sort.

22Which symbol replaces the old text in a file? a) >> · b) > · c) < · d) |

b) >. > clears the file first. >> appends.

23Why does a script start with #!/bin/bash? a) It chooses the shell that runs it · b) It prints a title · c) It sets the first variable · d) It adds a comment only

a) It chooses the shell that runs it. The #! line tells the system which shell to use.

24In a script, $# tells you what? a) The script name · b) How many inputs came in · c) The first input · d) The last exit code

b) How many inputs came in. $# is the count of arguments.

25Which test passes only when logs is a folder? a) [ -f logs ] · b) [ -d logs ] · c) [ -z logs ] · d) [ -x logs ]

b) [ -d logs ]. -d is true for a directory.

26Which line reads a file one line at a time? a) if read l; then · b) while read l; do ...; done < f · c) for f in *.txt · d) case l in

b) while read l; do ...; done < f. The while read loop takes one line per round.

27A command returns exit code 2. What does it mean? a) Success · b) Failure · c) Still running · d) No output

b) Failure. Any code other than 0 means a problem.

28What does set -e change? a) The script stops at the first failed command · b) The script runs twice · c) Every line is printed · d) Variables are cleared

a) The script stops at the first failed command. One failed command stops the whole run.

29In a crontab line, which field is first? a) Minute · b) Hour · c) Command · d) Day

a) Minute. A crontab line starts with minutes.

30Which command reports free space on each disk? a) du -sh · b) df -h · c) uptime · d) date

b) df -h. df means disk free. -h makes it readable.

31Which pair packs a folder and then shrinks the file? a) tar -cvf then gzip · b) tar -xvf then rm · c) unzip then cp · d) mv then chmod

a) tar -cvf then gzip. tar collects the files, then gzip compresses them.

32What does a shell let you do?

It lets you control the computer with text.

33What does Bash help you write?

It helps you write scripts.

34What does cron do?

It runs jobs on a schedule.

6.9

Study and exam tips

Practise the real commands in the lab; typing them beats reading.

  • Learn > vs >>, and -r vs no -r.
  • For permissions, remember the order: owner, group, others.
  • In scripts, watch the shebang, chmod +x, and $?.
  • Read any command as three parts: name, options, arguments.
  • Type the commands in the lab. Your hands learn faster than your eyes.
  • Keep the pairs apart: > vs >>, .. vs ., -f vs -d.
  • For rights questions, read the string in three blocks: owner, group, others.
  • In a script, check the shebang, the execute bit, and $? first.
  • If two commands look valid, pick the one that changes only what the question asks.

Commands in this lesson

Practice

All commands in this chapter