Hands-On: Unix Shell Basics
Introduction to HLRS Systems - Day 1 Afternoon
Work through the exercises below at your own pace. Each section lists the
relevant commands as a quick reference. For details use
man <command> or refer to the slides.
Use Ctrl+Shift+F to search for keywords on the lecture slides.
Before you begin
-
Tab completion. The shell can finish what you are typing. Type
cd /etand press Tab. Then typels /home/and press Tab twice to list all options. It is typically faster than typing and catches typos before you make them. -
Command history. Press ↑ a few times to walk back through your previous commands. Now press Ctrl+R and type
cd; the shell finds the most recent matching command. Press Enter to run it, or → to edit it first. Usehistoryto see your full session, and!!to repeat the last command.
1 Navigation
. are hidden; they don't show up in ls by default. These are called "dotfiles" and typically hold configuration (e.g., .bashrc, .ssh/).
-
Print your current working directory. What is the path to your home directory?
-
List the contents of your home directory, including hidden files (hint: use
man lsto find the right flag). -
Navigate to
/tmp, confirm your location, then return to your home directory.Running
cdwith no argument takes you home.cd ~andcd $HOMEwork too. -
List the contents of
/etcwithout leaving your home directory.
2 Files and Directories
-
Create an empty file with
touch notes.txt. Open the file innanoand write a short note. Save withCtrl+O, confirm the filename, then exit withCtrl+X. Print the contents ofnotes.txtto the terminal. Then create a second file with a space in its name (my notes.txt) and remove both files.Filenames containing spaces must be quoted.
-
Create the following directory structure inside your home directory:
workshop/ ├── data/ └── results/
-
Copy the file
/etc/os-releaseintoworkshop/data/ -
View the file with
less(pressqto quit). Then print only the first 5 lines using a single command. -
Rename the file to
system-info.txt -
Create a second copy of
system-info.txtinworkshop/results/ -
Delete the copy in
workshop/results/ -
Remove the entire
workshop/tree recursively in one command.Check
man rm. ⚠ There is no recycle bin on the command line. -
Wildcards: use a single
lscommand to list every file in/etcwhose name ends in.conf.The shell expands
*to match any sequence of characters before the command runs. -
Now list every file in
/etc/ssh/whose name starts withssh.
3 Finding Files and Getting Help
--help for a quick usage summary, while man <command> opens the full manual page (press q to quit).
-
Open the manual page for
cp. Which flag enables recursive copying? Close the manual when done. -
Find out where the
python3executable lives on this system.Note: Scripts executed by
cron, batch systems, or other automation often run with a minimalPATHand may not find commands the way your interactive shell does. In such cases, using the full path (whichwhichcan give you) avoids surprises. -
Find all files in
/etcwhose name ends in.conf.You will see some "Permission denied" lines; that is normal. Append
2>/dev/nullto the command to discard them.
4 Permissions
-
Run
ls -l /etc/ssh/. How many regular files and how many directories are listed? Who owns them?The first character of each line is
-for a regular file anddfor a directory. -
Look at the permissions of
/etc/passwdwithls -l. Can every user on the system read it? Can they modify it? -
Create a small script file:
echo '#!/bin/bash' > hello.sh echo 'echo "Hello, Hunter!"' >> hello.sh
Try to run it with./hello.sh. What happens and why? -
Make the script executable for yourself only, then run it successfully.
chmod u+x <file>adds execute permission for the user (owner). -
Which groups do you belong to? Try both
groupsandid. What extra information doesidprovide? -
Create a directory
shared/and a fileshared/notes.txtwith a line of text. You want these permissions:- you can read and write the file
- group members can read but not write
- others have no access at all
chmod 640 shared/notes.txt chmod 750 shared
Verify withls -l sharedandls -ld shared.Why
750on the directory, not640? The execute bit on a directory means "may enter it". Without it, even you could not reachnotes.txtinside; so a directory needsxwherever you grantr. This is also why a singlechmod -R 640 shared/would lock you out. See the lecture slides on file permissions. -
Use
namei -l <path>on the full path toshared/notes.txtto inspect permissions at every level.
5 Environment Variables
-
Have a look at the
PATHvariable. How many directories does yourPATHcurrently contain?Use:
echo $PATH | tr ':' '\n' | wc -l -
Add a directory to your
PATHso you can run a script from anywhere:mkdir ~/mybin echo 'echo Custom command works' > ~/mybin/hello chmod u+x ~/mybin/hello hello # command not found export PATH="$PATH:$HOME/mybin" hello # now prints: Custom command works
The shell findshellothe second time because$HOME/mybinis now on yourPATH, the list of directories it searches for commands. We useexportbecause it is the form you will reuse in~/.bashrc, and it makes sure programs you launch from this shell inherit the updatedPATHtoo, not just the shell itself. -
Prepend
$HOME/mybinto yourPATHfor this session and verify the change.Note: The operating system searches
PATHfrom left to right and uses the first match. Prepending therefore lets your version take precedence over system commands; appending acts as a fallback. New shells will not remember previous exports; if you want to make your exports permanent, you have to write them into~/.bashrc -
Compare the output of
echo "$HOME"andecho '$HOME'. Why are they different? -
Set
EMPTY="", then runls $EMPTY /tmpandls "$EMPTY" /tmp. The first lists/tmpwhile the second gives an error. Explain the difference.Without quotes the shell removes empty arguments before
lssees them.
6 Pipelines & Redirection
-
Find all files in
/etcand count how many there are withwc -l, discarding permission errors.Use
findwith the option-type f -
Write that file list to
etc-contents.txt. Then append today's date to the end of the same file.Use
>to write and>>to append. Thedatecommand prints the current date and time. -
Find the 5 longest lines in
etc-contents.txtHint:
awk '{print length, $0}'adds the line length as a prefix. Combine withsort -n(numeric sort) andheadortail.
7 Searching with grep
grep --help . See the lecture slide for examples.
-
Search
etc-contents.txtfor lines containingconf, case-insensitively. -
Count how many lines in
/etc/os-releasedo not contain an=sign.You'll need two flags: one to invert the match and one to count.
-
Find lines containing
NAMEin/etc/os-releaseand show 2 lines of context after each match. Also try the other context options.-A <n>shows n lines after each match. -
Search recursively through
/etcfor any file containing the wordhostname. Suppress permission errors.grep -rsearches recursively. Append2>/dev/nullto discard errors.
8 Processes
-
Start a long-running command in the foreground:
sleep 120. Cancel it with Ctrl+C. -
Start
sleep 120again, but this time suspend it with Ctrl+Z. Usejobsto confirm it is stopped, then resume it in the background withbg. Verify withjobsagain. -
Start a job in the background with
sleep 300 &, bring it to the foreground withfg, then cancel it with Ctrl+C.fgis the counterpart tobg:bgsends a job to the background,fgpulls it back. With several jobs, usefg %<n>and the job number fromjobs. -
Start a background job directly:
sleep 300 &. Find its process ID withps(or note the PID printed when you started it) and terminate it withkill <PID>. -
Run
ps -fu $USERto see all your processes. What command started the shell you are typing into?
9 Archives
-
Create a compressed archive
backup.tar.gzof the/etc/sshdirectory.You will see a few
Permission deniedlines for the private host keys, which only root can read. That is expected, and the archive still contains everything you are allowed to read. -
List the contents of the archive without extracting it.
-
Extract the archive into a new directory
ssh-backup/in your home directory.mkdir ssh-backupfirst, then extract withtar -xzf <archive> -C ssh-backup. -
How large is the archive compared to the unpacked directory?
10 Putting It Together
-
Largest files: Find the 5 largest files in
/usr/binby size.Hint:
ls -lSsorts by size and adding-hmakes it human-readable. -
Line count comparison: Find which of these system files has the most lines:
/etc/passwd,/etc/group,/etc/services.wc -laccepts multiple files at once and prints one line per file (plus atotalrow at the end). Strip the total withgrep -v total, then pipe throughsort -nrto put the longest file first. -
Free exploration: Use any combination of tools to answer: how many distinct file extensions exist in
/etc?Hint:
grep -o '\.[^./]*$'extracts the last extension from each filename. Combine withfind,sort, anduniq(orsort -u) to count distinct extensions.
Errors you may see
-
Permission denied- you do not have the rights for that operation. Check withls -land review Section 4. -
No such file or directory- typo, wrong directory, or the file genuinely does not exist. Check withlsandpwd. -
command not found- the program is not on yourPATH. On HLRS systems this often means you need tomodule loadit (covered in a later session).
When you are finished, feel free to clean up any files created during the exercises.