In the print dialog, choose "Save as PDF" as the destination.
Operating Systems 1, Lab 9
Explore Ubuntu from the terminal: find your way around, read files, then create, copy, move and delete them.
pwd and look around with ls, ls -a and ls -lcd using absolute and relative paths, .. and ~less, cat, head and tailtouch, mkdir, mv, cp, rm, rmdirnano and search in it with grepGetting Started
A prompt, a command, an answer
Press Super, type terminal, press Enter: a window opens with one line
student@ubuntu:~$| Part | Meaning |
|---|---|
student | the user you are logged in as |
ubuntu | the name of the computer |
~ | the folder you are in now: ~ is your home folder |
$ | your command starts here |
Name first, then options, then arguments
| Command line | Name | Options | Arguments |
|---|---|---|---|
pwd | pwd | none | none |
ls -l | ls | -l long output | none |
mkdir -p some/other/directories | mkdir | -p make parents | one path |
cp -r some again | cp | -r recursive | source and destination |
Navigation
pwd, ls and cd
A new terminal starts in your home folder
student@ubuntu:~$ pwd
/home/student/, then home, then student/ is the root, the top of the one tree of folders/ the root
|-- etc configuration files
|-- home
| `-- student your home folder (~)
`-- usr
`-- share
`-- localels prints the names in the current folder, sorted by name. At home in your VM:
student@ubuntu:~$ ls
Desktop Documents Downloads Music Pictures Public snap Templates Videossnap keeps the data of apps installed as snapsA desktop installation has even more here, so your list is longer
student@ubuntu:~$ cd /usr/share
student@ubuntu:/usr/share$ ls
apport dot.profile locale polkit-1
base-files dot.profile.md5sums locales profile
base-passwd dpkg man profile.md5sums
bash-completion gcc menu sensible-utils
bug gdb misc staff-group-for-usr-local
common-licenses i18n motd tabset
debconf info nano terminfo
debianutils info.dir networks util-linux
dict keyrings pam zoneinfo
doc libc-bin pam-configs
doc-base libgcrypt20 perl5
dot.bashrc lintian pixmapsA new account holds only the shell's files:
student@ubuntu:~$ ls -a
. .. .bash_logout .bashrc .profile.cache and .config. is this folder; .. is the parent folderOne line per item: three empty files and two folders
student@ubuntu:~$ ls -l
total 8
-rw-rw-r-- 1 student student 0 Sep 25 11:07 file1
-rw-rw-r-- 1 student student 0 Sep 25 11:07 file2
-rw-rw-r-- 1 student student 0 Sep 25 11:07 file3
drwxrwxr-x 3 student student 4096 Sep 25 11:08 some
drwxrwxr-x 3 student student 4096 Sep 25 11:08 test| Column | Example | Meaning |
|---|---|---|
| Type and permissions | drwxrwxr-x | d directory, - file; permissions come later in the course |
| Links | 3 | the number of hard links |
| Owner and group | student student | who owns the item |
| Size | 4096 | in bytes; the empty files show 0 |
| Modified | Sep 25 11:08 | the last change; the name comes last |
Predict: file or folder? Owner? Size? Last change?
-rw-rw-r-- 1 student student 46 Sep 25 11:10 file1| Absolute path | Relative path | |
|---|---|---|
| Starts with | / | a name, . or .. |
| Measured from | the root / | the current folder |
| Example | /usr/share/locale | locale, typed in /usr/share |
| Works from anywhere? | yes | no: it depends on where you are |
One absolute move, then two relative ones
student@ubuntu:~$ cd /usr/share
student@ubuntu:/usr/share$ pwd
/usr/share
student@ubuntu:/usr/share$ cd locale
student@ubuntu:/usr/share/locale$ cd ar/LC_MESSAGES
student@ubuntu:/usr/share/locale/ar/LC_MESSAGES$ pwd
/usr/share/locale/ar/LC_MESSAGESstudent@ubuntu:/usr/share/locale/ar/LC_MESSAGES$ cd ..
student@ubuntu:/usr/share/locale/ar$ pwd
/usr/share/locale/ar
student@ubuntu:/usr/share/locale/ar$ cd
student@ubuntu:~$ pwd
/home/student.. is the parent: one level upcd alone, cd ~ and cd /home/student all go home~ means your home folder in any commandYou are in /usr/share/locale/ar/LC_MESSAGES and type cd ../... Predict the new folder.
student@ubuntu:/usr/share/locale/ar/LC_MESSAGES$ cd ../..
student@ubuntu:/usr/share/locale$ pwd
/usr/share/localeIn your home folder you type cd nothere, and no such folder exists. Predict what happens.
student@ubuntu:~$ cd nothere
bash: cd: nothere: No such file or directory
student@ubuntu:~$Text Files
Linux keeps much of the system in plain text files
student@ubuntu:~$ less /etc/servicesless is a pager: it shows a file one screen at a time:/etc/services lists the network services the system knows| Key | Action |
|---|---|
| Up and Down arrows | one line |
Space, Page Down or Ctrl+f | one page down |
Page Up or Ctrl+b | one page up |
/ word, then Enter; n and N | search forward; next and previous match |
q | quit and return to the prompt |
Inside less /etc/services you type /mail and press Enter. Predict which line lands on top.
smtp 25/tcp mail
time 37/tcp timserver
time 37/udp timserver
whois 43/tcp nicnamePredict: where does n take you?
mailq 174/tcp # Mailer transport queue for Zmailer
xdmcp 177/udp # X Display Manager Control Protocol
bgp 179/tcp # Border Gateway ProtocolThey print and return to the prompt at once.
| Command | Prints |
|---|---|
cat file | the whole file |
head file | the first 10 lines |
tail file | the last 10 lines |
Count the lines below: exactly 10, the empty one included.
student@ubuntu:~$ tail /etc/services
sgi-cad 17004/tcp # Cluster Admin daemon
binkp 24554/tcp # binkp fidonet protocol
asp 27374/tcp # Address Search Protocol
asp 27374/udp
csync2 30865/tcp # cluster synchronization tool
dircproxy 57000/tcp # Detachable IRC Proxy
tfido 60177/tcp # fidonet EMSI over telnet
fido 60179/tcp # fidonet EMSI over TCP
# Local servicesCreate, Copy, Delete
Work in your home folder, the one place you may create files
student@ubuntu:~$ touch file1
student@ubuntu:~$ ls
file1
student@ubuntu:~$ touch /home/student/file2 /home/student/file3
student@ubuntu:~$ ls
file1 file2 file3file3 already exists. Predict what a second touch file3 does.
student@ubuntu:~$ ls -l file3
-rw-rw-r-- 1 student student 0 Sep 25 11:07 file3
student@ubuntu:~$ touch file3
student@ubuntu:~$ ls -l file3
-rw-rw-r-- 1 student student 0 Sep 25 11:10 file3mkdir test, then mkdir test/example both work. Predict mkdir some/other/directories, when no some exists.
student@ubuntu:~$ mkdir some/other/directories
mkdir: cannot create directory 'some/other/directories': No such file or directory
student@ubuntu:~$ mkdir -p some/other/directories
student@ubuntu:~$ ls
file1 file2 file3 some testRead it as: move from here to there
student@ubuntu:~$ mv file1 test
student@ubuntu:~$ ls
file2 file3 some test
student@ubuntu:~$ ls test
example file1
student@ubuntu:~$ mv test/file1 .
student@ubuntu:~$ ls
file1 file2 file3 some test
student@ubuntu:~$ mv test testing
student@ubuntu:~$ ls
file1 file2 file3 some testingstudent@ubuntu:~$ cp file3 file4
student@ubuntu:~$ ls
file1 file2 file3 file4 some testingsome is a folder. Predict what cp some again does.
student@ubuntu:~$ cp some again
cp: -r not specified; omitting directory 'some'
student@ubuntu:~$ cp -r some again
student@ubuntu:~$ ls again
other
student@ubuntu:~$ cp file1 again
student@ubuntu:~$ ls again
file1 otherrm deletes files and rmdir deletes empty folders. Predict rmdir some, while some still holds other/directories.
student@ubuntu:~$ rm file4
student@ubuntu:~$ rmdir testing/examplestudent@ubuntu:~$ rmdir some
rmdir: failed to remove 'some': Directory not emptyBelow, the folder ~/projects holds two empty files, labs and tasklist
-i (interactive) asks before each removal: y removes, n keepsrm ask by default; Ubuntu does not-f (force) never asks: handy for many files, dangerous for the same reasonstudent@ubuntu:~/projects$ rm -i labs
rm: remove regular empty file 'labs'? n
student@ubuntu:~/projects$ ls
labs tasklistText Tools
nano, grep, and one full session
nano file1, then two typed lines
GNU nano 7.2 file1 *
Hello there.
Here is some text.
^G Help ^O Write Out ^W Where Is ^K Cut ^T Execute ^C Location
^X Exit ^R Read File ^\ Replace ^U Paste ^J Justify ^/ Go To Line* after the name: changes not saved yet| Keys | What happens |
|---|---|
Ctrl+O, then Enter | write out (save): nano confirms [ Wrote 2 lines ] |
Ctrl+X | exit; with unsaved changes it asks Save modified buffer? |
Y or N | save and exit, or throw the changes away |
Ctrl+C | cancel the question and stay in the editor |
A third line was added, then saved. Predict the size ls -l shows.
student@ubuntu:~$ cat file1
Hello there.
Here is some text.
Another line.In ~/projects, tasklist now holds one line, written with nano
student@ubuntu:~/projects$ cat tasklist
Hello World
student@ubuntu:~/projects$ grep ll tasklist
Hello Worldgrep prints every line that contains the patternll is inside Hello: part of a word is enoughgrep prints nothingPredict grep hello tasklist, then grep -i hello tasklist.
student@ubuntu:~/projects$ grep hello tasklist
student@ubuntu:~/projects$ grep -i hello tasklist
Hello WorldSearch the output of another command
student@ubuntu:~/projects$ ls /etc | grep -i net
issue.net
networksFrom a new account's home folder: the session that builds the ~/projects of the rm -i and grep slides
mkdir projects and mkdir -p 2022/Q1: folders for the workrmdir an empty folder; rm -R a folder that holds a filecd /etc, then home three ways: cd /home/student, cd ~, cdcd projects, touch three files and .secretfile, compare ls with ls -amv a file into 2022, mv to rename, cp into ~/2022/Q1, rm the hidden filenano a line into tasklist, then search it with grepYour Turn
About 30 minutes: four problems on paper, then tasks in your own VM
About 4 minutes
Start in /home/student. Write what each pwd prints.
cd /usr/share
cd locale/ar
cd ..
pwd
cd ../..
pwd
cd
pwdstudent@ubuntu:/usr/share/locale$ pwd
/usr/share/locale
student@ubuntu:/usr$ pwd
/usr
student@ubuntu:~$ pwd
/home/studentAbout 7 minutes
Run in your home folder, in order. Draw every folder and file of lab9.
mkdir lab9
cd lab9
mkdir -p notes/week9
touch a.txt b.txt
mv a.txt notes
cp b.txt notes/week9
mv b.txt c.txt
cp -r notes backup
rm notes/a.txtlab9
|-- backup
| |-- a.txt
| `-- week9
| `-- b.txt
|-- c.txt
`-- notes
`-- week9
`-- b.txtAbout 5 minutes
Still in lab9 after Practice 2. Each command fails: say why, and what would work.
rmdir backupcp notes copymkdir x/ycd c.txtrm backup| Command | Why it fails | Use instead |
|---|---|---|
rmdir backup | Directory not empty | rm -r backup |
cp notes copy | -r not specified; omitting directory | cp -r notes copy |
mkdir x/y | x does not exist: No such file or directory | mkdir -p x/y |
cd c.txt | Not a directory: cd enters folders only | cat c.txt to read it |
rm backup | Is a directory | rm -r backup |
About 5 minutes
One command for each task
draft.txt to final.txtreports/2026/sep; not even reports existssite to site-backup~/lab9/reports/2026, go up to ~/lab9/reportstodo with finish or Finish1. mv draft.txt final.txt
2. mkdir -p reports/2026/sep
3. cp -r site site-backup
4. cd ..
5. ls -la
6. grep -i finish todoAbout 9 minutes
Check every step with ls or pwd
pwd, then ls -a at home: which hidden files do you have?mkdir -p lab9/notes/week9, then touch a.txt and b.txt in lab9lab9, run the rest of Practice 2 and check your drawing with lsnano todo: buy milk, Finish OS lab, book the lab, finish reportgrep finish todo, grep -i finish todo, grep lab todo: explain each lineless /etc/services, search http, n twice, q; then rm -r lab9 and rm todostudent@ubuntu:~$ grep finish todo
finish report
student@ubuntu:~$ grep -i finish todo
Finish OS lab
finish report
student@ubuntu:~$ grep lab todo
Finish OS lab
book the lab.bash_logout, .bashrc and .profile, plus . and ..; the desktop adds more, such as .cache and .configgrep finish: only the lowercase line; -i also matches Finish OS labgrep lab: the two lines that contain labpwd says where you are, ls what is there, cd moves: .. up, ~ home/; a relative one starts where you aretouch and mkdir -p create; mv moves and renames; cp -r copies foldersrm and rm -r delete for good: there is no undoMahmoud Abas|Linux Commands, Part 1