Linux Commands, Part 1
In Lab 8 you installed Ubuntu and explored it with the mouse: the desktop, the Files application and the Linux folder tree. This section keeps exploring Ubuntu as a normal user, but now from the terminal. You will learn to find out where you are, look around, move through the folder tree, read files, and create, copy, move, rename and delete files and folders by typing commands. Everything you did by right-clicking in Files last week has a command, and the commands are faster once your fingers know them. The last half hour is yours: four problems on paper, then a task list in your own Ubuntu machine.
Every command in this page was run on a real Ubuntu 24.04 system, as a normal user named student on a computer named ubuntu, and the output shown is the real output. Listings of system folders such as /usr/share and /etc are longer on a full desktop installation, so yours will show more names; the commands behave the same way. The test account was also new, so its home folder did not have the folders that the desktop creates (Desktop, Documents and the rest); in your VM, every listing of your home folder shows those names as well. New files and folders were created with the settings of a desktop session, so their permissions, such as -rw-rw-r--, are the ones you will see.
Objectives
By the end of the section you should be able to:
- Open a terminal, read the prompt, and split a command into the command name, its options and its arguments.
- Find where you are with
pwd, list a folder withls,ls -a,ls -landls -la, and read every column of a long listing. - Move around with
cdusing absolute and relative paths,..for the parent folder and~or plaincdfor your home folder. - View files with
less(scroll, search, quit),cat,headandtail. - Create files with
touchand folders withmkdirandmkdir -p. - Move and rename with
mv, copy withcpandcp -r, and delete withrm,rm -randrmdir, knowing that there is no undo. - Edit a small text file with
nano, and search inside files and command output withgrepandgrep -i.
Part 1: The terminal
Opening a terminal
The terminal is an application like any other. Press the Super key (the Windows key), type terminal, and press Enter. If you added Terminal to the dock in Lab 8, one click opens it. A window appears with a single line waiting for you:
student@ubuntu:~$This line is the prompt. It tells you three things before you type anything:
| Part | Meaning |
|---|---|
student | the user you are logged in as |
ubuntu | the name of the computer |
~ | the folder you are in now; ~ is short for your home folder |
The $ at the end marks the place where your command starts. Type a command, press Enter, and the output appears under it, followed by a new prompt. The prompt keeps showing your current folder, so after you move somewhere else it changes, for example to student@ubuntu:/usr/share$.
The shape of a command
Every command in this section has the same shape: the command name, then zero or more options, then zero or more arguments.
| Example | Command | Options | Arguments |
|---|---|---|---|
pwd | pwd | none | none |
ls -l | ls | -l (long output) | none |
mkdir -p some/other/directories | mkdir | -p (make parents) | the path to create |
cp -r some again | cp | -r (recursive) | a source and a destination |
- An option starts with a dash and changes how the command behaves.
- An argument is usually a file or folder name, or a path.
- Options can be combined:
ls -lameans the same asls -l -a.
One tree of folders
Linux keeps every file and folder in one tree. The top of the tree is called the root directory and is written as a single slash /. Each user's home folder is inside /home, so the home folder of student is /home/student. Your home folder is the place where you have full control: you can create, change and delete anything in it. Most of the rest of the tree belongs to the system, and a normal user can read it but not change it.
/ the root: top of the tree
|-- etc configuration files
|-- home
| `-- student the home folder of student (~)
`-- usr
`-- share
`-- localePart 2: Where am I? pwd, ls and cd
pwd: print the working directory
When you open a terminal you are dropped into your home folder. pwd prints the folder you are in now, as a full path from the root:
student@ubuntu:~$ pwd
/home/studentRead the answer from left to right: inside / there is home, and inside home there is student. Knowing where you are is the key to knowing where to go next.
ls: list the contents of a folder
ls lists the files and folders inside the current folder, sorted by name. In the home folder of your VM it shows the folders that the desktop created:
student@ubuntu:~$ ls
Desktop Documents Downloads Music Pictures Public snap Templates Videossnap keeps the data of apps installed as snaps, such as Firefox. The test account used for this page was new and had none of these folders, so there ls printed nothing at all. Printing nothing is a normal, successful answer: there is simply nothing visible to list. Keep in mind that the listings of your own home folder also show these nine names. A more crowded folder is /usr/share. The first command below moves there (the next pages explain cd), and ls shows its contents in columns, sorted by name:
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 pixmapsls -a: show hidden files too
In Lab 8 you saw that a name starting with a dot is hidden in Files. ls hides those names too. The -a option (all) shows them:
student@ubuntu:~$ ls -a
. .. .bash_logout .bashrc .profileSo even the new account's home folder, which ls showed as empty, holds three hidden files. They are configuration files for the shell, and hiding configuration files behind a leading dot is very common. In your VM, ls -a also lists the nine desktop folders and more hidden items that the desktop created, such as .cache and .config. The two remaining names are special:
.(one dot) means this folder, the current directory...(two dots) means the parent folder, the one that contains this folder.
They are not real files; they are built-in ways of referring to nearby folders, and you will use both with cd, mv and cp.
ls -l: the long listing
The -l option (long) prints one line per item with extra details. Here is a home folder holding 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 testRead each line from left to right:
| Column | Example | Meaning |
|---|---|---|
| Type and permissions | drwxrwxr-x | the first character is the type: d for a directory, - for a normal file; the rest are permissions (covered later in the course) |
| Links | 3 | the number of hard links to this item |
| Owner | student | the user who owns the item |
| Group | student | the group that owns the item |
| Size | 4096 | the size in bytes; the three new files are empty, so 0 |
| Modified | Sep 25 11:08 | the last time the item was changed |
| Name | some | the name of the file or folder |
Options combine, so ls -la gives the long listing including the hidden items.
Worked example 1: read a long listing line
Question. After a few edits, ls -l file1 prints the line below. Is file1 a file or a folder, who owns it, how big is it, and when was it last changed?
-rw-rw-r-- 1 student student 46 Sep 25 11:10 file1Answer. The first character is -, so it is a normal file. The owner is student (and the group is also student). It holds 46 bytes and was last modified on September 25 at 11:10.
cd: change directory with an absolute path
cd moves you to another folder. The move to /usr/share above used an absolute path: a path that starts with / and describes the location from the root. Because it starts at the top of the tree, an absolute path means the same folder no matter where you are when you type it. Every absolute path begins with that slash.
student@ubuntu:~$ cd /usr/share
student@ubuntu:/usr/share$ pwd
/usr/shareNotice that cd itself prints nothing when it succeeds; the prompt changes to show the new folder.
cd with a relative path
A relative path has no leading slash. It describes a location starting from the folder you are in now. Any folder inside the current folder can be reached by its name alone, and you can go several levels down at once by writing the rest of the path:
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_MESSAGEScd locale worked only because locale is inside /usr/share, where we were standing. The same command typed in your home folder fails, because the home folder has no locale inside it.
| Absolute path | Relative path | |
|---|---|---|
| Starts with | / | a name, . or .. |
| Measured from | the root / | the current folder |
| Example | /usr/share/locale | locale (from /usr/share) |
| Works from anywhere? | yes | no, it depends on where you are |
Going up, and going home
Use the special .. to go up to the parent of the current folder:
student@ubuntu:/usr/share/locale/ar/LC_MESSAGES$ cd ..
student@ubuntu:/usr/share/locale/ar$ pwd
/usr/share/locale/arTo go back to your home folder from anywhere, type cd with no argument. The tilde ~ stands for your home folder in any command, so cd ~ does the same thing, and so does the full absolute path cd /home/student:
student@ubuntu:/usr/share/locale/ar$ cd
student@ubuntu:~$ pwd
/home/student
student@ubuntu:~$ cd /etc
student@ubuntu:/etc$ pwd
/etc
student@ubuntu:/etc$ cd ~
student@ubuntu:~$ pwd
/home/student/etc is where most of the system's configuration files are stored.
Worked example 2: two levels up
Question. You are in /usr/share/locale/ar/LC_MESSAGES and you type cd ../... Where are you now?
Answer. Each .. moves up one level, and the slash between them chains the two moves: the first .. goes to /usr/share/locale/ar, the second to /usr/share/locale.
student@ubuntu:~$ cd /usr/share/locale/ar/LC_MESSAGES
student@ubuntu:/usr/share/locale/ar/LC_MESSAGES$ cd ../..
student@ubuntu:/usr/share/locale$ pwd
/usr/share/localeWorked example 3: when cd fails
Question. In your home folder you type cd nothere, but no folder with that name exists. What happens?
Answer. The shell prints an error and you stay where you were; the prompt still shows ~.
student@ubuntu:~$ cd nothere
bash: cd: nothere: No such file or directory
student@ubuntu:~$Read error messages from left to right: the program that complained (bash, the shell), the command (cd), the name it was given, then the reason.
Interactive: resolve a path step by step
Pick a starting folder and type a path after cd (or press one of the examples), then press Play or Step. The marker walks the tree one path part at a time: an absolute path starts at /, a relative path starts where you are, ~ jumps to the home folder, .. climbs one level and . stays put. The final folder is what pwd prints after the cd. If any part of the path fails, the whole cd fails and you stay in the folder you started from. The widget holds the real folder names of the test system, and its answers are the ones bash gave for the same commands.
Part 3: Looking inside files
Linux keeps a large part of the system in plain text files, so reading files is an everyday task.
less: a pager
less opens a file and shows one screen of it at a time. Unlike the commands so far, it does not return to the prompt immediately: it keeps running and occupies the terminal until you quit. Open the file /etc/services, a configuration file that lists the network services the system knows about:
student@ubuntu:~$ less /etc/servicesThe top of the file fills the window, and the last line shows the file name:
# Network services, Internet style
#
# Updated from https://www.iana.org/assignments/service-names-port-numbers/service-names-port-numbers.xhtml .
#
# New ports will be added on request if they have been officially assigned
# by IANA and used in the real-world or are needed by a debian package.
# If you need a huge list of used numbers please install the nmap package.
tcpmux 1/tcp # TCP port service multiplexer
echo 7/tcp
echo 7/udp
discard 9/tcp sink null
discard 9/udp sink null
systat 11/tcp users
daytime 13/tcp
daytime 13/udp
netstat 15/tcp
qotd 17/tcp quote
chargen 19/tcp ttytst source
chargen 19/udp ttytst source
ftp-data 20/tcp
ftp 21/tcp
/etc/servicesThe keys inside less:
| Key | Action |
|---|---|
| Up and Down arrows | scroll one line |
Space, Page Down or Ctrl+f | one page down |
Page Up or Ctrl+b | one page up |
/ then a word, then Enter | search forward for the word |
n / N | next match / previous match |
q | quit and return to the prompt |
Worked example 4: search inside less
Question. Inside less /etc/services you type /mail and press Enter. Which line jumps to the top of the screen? Then you press n. Where do you land?
Answer. The search moves forward to the first line containing mail and puts it at the top of the screen. That is the SMTP (mail) service on port 25:
smtp 25/tcp mail
time 37/tcp timserver
time 37/udp timserver
whois 43/tcp nicnamePressing n jumps to the next match, further down the file:
mailq 174/tcp # Mailer transport queue for Zmailer
xdmcp 177/udp # X Display Manager Control Protocol
bgp 179/tcp # Border Gateway ProtocolWhile less waits for a key after a search, its last line shows a colon : instead of the file name. Press q to quit.
cat, head and tail
Three commands print a file 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 |
For a long file like /etc/services, head and tail are much more useful than cat:
student@ubuntu:~$ head /etc/services
# Network services, Internet style
#
# Updated from https://www.iana.org/assignments/service-names-port-numbers/service-names-port-numbers.xhtml .
#
# New ports will be added on request if they have been officially assigned
# by IANA and used in the real-world or are needed by a debian package.
# If you need a huge list of used numbers please install the nmap package.
tcpmux 1/tcp # TCP port service multiplexer
echo 7/tcpstudent@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 servicesCount the lines of each output: exactly 10, and the empty line counts as one of them. A file shorter than 10 lines is printed whole by both commands.
Part 4: Creating files and folders
The next parts work in the home folder, the one place where you are sure to have permission to create files.
touch: create an empty file
The simplest way to create a file is touch. It creates an empty file with the name you give. You can give several names at once, and absolute paths work too:
student@ubuntu:~$ cd
student@ubuntu:~$ touch file1
student@ubuntu:~$ ls
file1
student@ubuntu:~$ touch /home/student/file2 /home/student/file3
student@ubuntu:~$ ls
file1 file2 file3Worked example 5: touch a file that already exists
Question. file3 already exists. A few minutes later you run touch file3 again. Does it create a second file3, empty the file, or do something else?
Answer. Two items in one folder can never share a name, so there is no second file. On an existing file, touch only updates its last modified time. The long listing before and after shows it:
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: make a directory
mkdir creates an empty folder. A folder can be created inside another folder that already exists:
student@ubuntu:~$ mkdir test
student@ubuntu:~$ mkdir test/exampleWorked example 6: a path whose parents do not exist
Question. No folder named some exists yet. What does mkdir some/other/directories do?
Answer. It fails: mkdir creates only the last folder of the path, and it needs the folders before it to exist already.
student@ubuntu:~$ mkdir some/other/directories
mkdir: cannot create directory 'some/other/directories': No such file or directory(Your terminal draws the quotes around the name in this message as curved quotes.) The -p option (parents) tells mkdir to create every missing folder along the path, in one step: first some, then other inside it, then directories inside that.
student@ubuntu:~$ mkdir -p some/other/directories
student@ubuntu:~$ ls
file1 file2 file3 some testThe long listing of this folder is the one read in Part 2: some and test are directories (d), and each has 3 links because each already holds one folder of its own.
Part 5: Moving, copying and deleting
mv: move
mv moves a file to a new location. The syntax reads "move from here to there":
student@ubuntu:~$ mv file1 test
student@ubuntu:~$ ls
file2 file3 some test
student@ubuntu:~$ ls test
example file1Giving a folder name to ls lists that folder instead of the current one. To bring the file back, use the one-dot name for "this folder" as the destination:
student@ubuntu:~$ mv test/file1 .
student@ubuntu:~$ ls
file1 file2 file3 some testmv: rename
Moving and renaming are the same operation: both change where an item lives and what it is called. Moving an item to the same place with a new name renames it:
student@ubuntu:~$ mv test testing
student@ubuntu:~$ ls
file1 file2 file3 some testingWarning. The shell does not stop you from destroying files by accident. If you rename a file to a name that already exists, the old file is overwritten by the one you moved, and there is no way to get it back.
cp: copy a file
mv cannot duplicate anything; cp makes a new copy and keeps the original. It uses the same "from here to there" order:
student@ubuntu:~$ cp file3 file4
student@ubuntu:~$ ls
file1 file2 file3 file4 some testingAfter an mv the source name is gone; after a cp both names exist. The same overwrite warning applies: if file4 had existed already, its content would have been replaced completely by the content of file3.
Worked example 7: copying a folder
Question. some is a folder. What does cp some again do?
Answer. Nothing is copied. A folder is copied only with the -r option (recursive), which copies the folder together with everything inside it:
student@ubuntu:~$ cp some again
cp: -r not specified; omitting directory 'some'
student@ubuntu:~$ cp -r some again
student@ubuntu:~$ ls again
otherCopying into an existing folder
When the destination is an existing folder, the file is copied into it rather than replacing anything:
student@ubuntu:~$ cp file1 again
student@ubuntu:~$ ls again
file1 othermv behaves the same way: mv file1 test earlier moved the file into the existing folder test.
rm and rmdir: delete
Warning. There is no undo and no Trash in the shell. A file removed with
rmis gone for good.
rm removes files. rmdir removes empty folders only:
student@ubuntu:~$ rm file4
student@ubuntu:~$ rmdir testing/exampleWorked example 8: rmdir on a folder that is not empty
Question. some still contains other/directories. What does rmdir some do, and how do you remove some with everything inside it?
Answer. rmdir refuses, because the folder is not empty:
student@ubuntu:~$ rmdir some
rmdir: failed to remove 'some': Directory not emptyrm with the -r option removes a folder, all of its contents, and the folder itself, with no questions. -R is the same option in capitals:
student@ubuntu:~$ rm -r again
student@ubuntu:~$ ls
file1 file2 file3 some testingrm -i and rm -f
Two more options change how careful rm is:
-i(interactive) asks before each removal. Answeryto remove,nto keep the file. Some distributions makermbehave asrm -iby default, but Ubuntu does not.-f(force) removes without asking and stays silent even when a name does not exist. It is convenient for deleting many files and dangerous for the same reason.
In the Part 7 walkthrough, the folder projects holds the empty file labs:
student@ubuntu:~/projects$ rm -i labs
rm: remove regular empty file 'labs'? n
student@ubuntu:~/projects$ ls
labs tasklistThe answer n kept labs.
Part 6: Editing and searching
nano: a text editor in the terminal
touch creates empty files; to put text into a file, use an editor. nano is a simple one for beginners. Like less, it occupies the whole terminal until you exit. Open (or create) file1:
student@ubuntu:~$ nano file1The top line shows the program and the file name, the middle is the text, and the two bottom lines list shortcuts. In those shortcuts ^ means the Ctrl key, so ^X is Ctrl+X. Type two 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 LineThe empty rows between the text and the shortcuts are left out here. The * after the file name means there are changes that are not saved yet. The keys you need:
| Keys | Action |
|---|---|
Ctrl+O | write out (save): nano asks File Name to Write: file1; press Enter to confirm |
Ctrl+X | exit; if there are unsaved changes it asks Save modified buffer? |
Y / N | at that question: save, or throw the changes away |
Ctrl+C | cancel the question and stay in the editor |
Ctrl+G | help |
After Ctrl+O and Enter, the status line confirms [ Wrote 2 lines ]. Add a third line, Another line., press Ctrl+X, answer Y, then Enter at the file name, and you are back at the prompt. cat shows the result:
student@ubuntu:~$ cat file1
Hello there.
Here is some text.
Another line.less file1 shows the same three lines with file1 (END) at the bottom, because the whole file fits on one screen.
Worked example 9: how big is the file?
Question. Predict the size that ls -l file1 shows for these three lines.
Answer. Every character is one byte here, and every line ends with an invisible newline character that also takes one byte. Hello there. is 12 characters plus the newline, 13 bytes; Here is some text. is 18 plus 1, 19 bytes; Another line. is 13 plus 1, 14 bytes. The total is 13 + 19 + 14 = 46:
student@ubuntu:~$ ls -l file1
-rw-rw-r-- 1 student student 46 Sep 25 11:10 file1That is the line you read in Worked example 1.
grep: search inside a file
grep looks inside a file and prints every line that contains a piece of text (a pattern). The pattern does not have to be a whole word. In the walkthrough below, the file tasklist holds the single line Hello World, written with nano:
student@ubuntu:~/projects$ cat tasklist
Hello World
student@ubuntu:~/projects$ grep ll tasklist
Hello Worldll is found inside Hello, so the whole line is printed.
Worked example 10: capital letters
Question. What do grep hello tasklist and grep -i hello tasklist print?
Answer. grep is case sensitive: hello is not the same text as Hello, so the first command finds nothing and prints nothing. The -i option makes it ignore case:
student@ubuntu:~/projects$ grep hello tasklist
student@ubuntu:~/projects$ grep -i hello tasklist
Hello Worldgrep on the output of another command
grep can also search the output of another command. The vertical bar | is a pipe: it sends the output of the command on its left into the command on its right. To show only the names in /etc that contain net, in any case:
student@ubuntu:~/projects$ ls /etc | grep -i net
issue.net
networksA desktop installation has more matching names in /etc, so your list will be longer.
Part 7: Ten commands in one session
This walkthrough uses the commands in a logical order, the way you would organize a real project. It starts in the home folder of a new account; in your VM the desktop folders are there too, and each ls of the home folder lists them.
1. Create folders. One folder for projects, a year folder with a first-quarter folder inside it (in one step), and a resources folder:
student@ubuntu:~$ pwd
/home/student
student@ubuntu:~$ mkdir projects
student@ubuntu:~$ mkdir -p 2022/Q1
student@ubuntu:~$ mkdir resources
student@ubuntu:~$ ls
2022 projects resources2. Change your mind. resources is still empty, so rmdir removes it. Once a folder holds a file, rmdir refuses and rm -R is needed:
student@ubuntu:~$ rmdir resources
student@ubuntu:~$ mkdir resources
student@ubuntu:~$ touch resources/notes
student@ubuntu:~$ rmdir resources
rmdir: failed to remove 'resources': Directory not empty
student@ubuntu:~$ rm -R resources
student@ubuntu:~$ ls
2022 projects3. Three ways home. From /etc, the absolute path, cd ~ and a plain cd all return to the home folder:
student@ubuntu:~$ cd /etc
student@ubuntu:/etc$ cd /home/student
student@ubuntu:~$ cd /etc
student@ubuntu:/etc$ cd ~
student@ubuntu:~$ cd /etc
student@ubuntu:/etc$ cd
student@ubuntu:~$ pwd
/home/student4. Create files, including a hidden one.
student@ubuntu:~$ cd projects
student@ubuntu:~/projects$ touch tasklist
student@ubuntu:~/projects$ touch activities
student@ubuntu:~/projects$ touch content
student@ubuntu:~/projects$ touch .secretfile
student@ubuntu:~/projects$ ls
activities content tasklist
student@ubuntu:~/projects$ ls -a
. .. activities content .secretfile tasklist
student@ubuntu:~/projects$ ls -la
total 8
drwxrwxr-x 2 student student 4096 Sep 25 11:12 .
drwxr-x--- 4 student student 4096 Sep 25 11:11 ..
-rw-rw-r-- 1 student student 0 Sep 25 11:11 activities
-rw-rw-r-- 1 student student 0 Sep 25 11:12 content
-rw-rw-r-- 1 student student 0 Sep 25 11:12 .secretfile
-rw-rw-r-- 1 student student 0 Sep 25 11:11 tasklist5. Move, rename, copy, delete. The tilde saves typing in cp and mv too:
student@ubuntu:~/projects$ mv content /home/student/2022
student@ubuntu:~/projects$ ls /home/student/2022
content Q1
student@ubuntu:~/projects$ mv activities labs
student@ubuntu:~/projects$ ls
labs tasklist
student@ubuntu:~/projects$ cp tasklist ~/2022/Q1
student@ubuntu:~/projects$ ls ~/2022/Q1
tasklist
student@ubuntu:~/projects$ rm .secretfile
student@ubuntu:~/projects$ ls -a
. .. labs tasklistAt the end, projects holds labs and tasklist, 2022 holds content and Q1, and Q1 holds a copy of tasklist. Before you change an important file, copying it first as a backup, exactly like this, is a good habit.
Interactive: the terminal sandbox
The widget below is a small terminal connected to a pretend Ubuntu system with a fresh home folder. Type any command from this section (pwd, ls, cd, mkdir, touch, cp, mv, rm, rmdir, cat, head, tail, less, nano, grep) and watch the folder tree beside it update: new items flash green, deleted ones fade out in red, and your current folder is highlighted. A box under the tree says in plain words what the last command did.
Pick a script (One session, ten commands: the walkthrough above; Managing files and folders: the commands of Parts 4 and 5; or Where am I? Moving around: the moves of Part 2): Play runs it one command at a time, Step runs just the next command, and Reset starts again from a fresh home folder. The outputs and error messages are the ones the real terminal printed for the same commands (with straight quotes). Anything the sandbox does not simulate, such as an option this section does not teach, gets a gray line that starts with sandbox:.
Practice
About 30 minutes. Problems 1 to 4 are on paper (answers at the end). Problem 5 is done in the terminal of your own Ubuntu VM. Every answer below was checked by running the commands.
Practice 1: trace the prompt
You start in your home folder /home/student. Write what each pwd prints.
cd /usr/share
cd locale/ar
cd ..
pwd
cd ../..
pwd
cd
pwdPractice 2: draw the tree
In your home folder you run the commands below, in order. Draw the final tree of lab9: every folder and every file.
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.txtPractice 3: predict the error
Still inside lab9, after Practice 2. Each command below fails. Say why, and which command would have done the job.
rmdir backupcp notes copymkdir x/ycd c.txtrm backup
Practice 4: write the command
Write one command for each task.
- Rename
draft.txttofinal.txt. - Create the folder
reports/2026/sepwhen not evenreportsexists yet. - Make a full copy of the folder
sitenamedsite-backup. - You are in
~/lab9/reports/2026. Go up to~/lab9/reports. - List everything in the current folder, hidden items included, in long format.
- Print the lines of
todothat containfinish, whether it is writtenfinishorFinish.
Practice 5: hands-on in your VM
Open a terminal and do each task, checking every step with ls or pwd:
- Run
pwd, thenls -ain your home folder. Which hidden files do you have? - Create
lab9/notes/week9in one command, then create the empty filesa.txtandb.txtinsidelab9. - Inside
lab9, run the rest of the Practice 2 commands, frommv a.txt notestorm notes/a.txt, and compare whatlsshows with your drawing. - Go back to your home folder. With
nano, create the filetodothere with the four linesbuy milk,Finish OS lab,book the lab,finish report. Save and exit. - Run
grep finish todo, thengrep -i finish todo, thengrep lab todo. Explain every line of output. - Open
/etc/serviceswithless, search forhttp, pressntwice, then quit. - Remove the whole
lab9folder and the filetodo, and confirm withlsthat both are gone.
Answers
Practice 1
cd locale/ar goes two levels down to /usr/share/locale/ar, and cd .. goes back up one level:
student@ubuntu:/usr/share/locale$ pwd
/usr/share/locale
student@ubuntu:/usr/share/locale$ cd ../..
student@ubuntu:/usr$ pwd
/usr
student@ubuntu:/usr$ cd
student@ubuntu:~$ pwd
/home/studentThe second pwd is /usr: two levels above /usr/share/locale. The last one is the home folder, because cd with no argument always goes home.
Practice 2
lab9
|-- backup
| |-- a.txt
| `-- week9
| `-- b.txt
|-- c.txt
`-- notes
`-- week9
`-- b.txtStep by step: a.txt moves into notes; a copy of b.txt goes into notes/week9; the original b.txt is renamed c.txt; backup becomes a full copy of notes while it still holds a.txt; finally a.txt is removed from notes only, so the copy in backup stays. The real listings:
student@ubuntu:~/lab9$ ls
backup c.txt notes
student@ubuntu:~/lab9$ ls notes
week9
student@ubuntu:~/lab9$ ls notes/week9
b.txt
student@ubuntu:~/lab9$ ls backup
a.txt week9
student@ubuntu:~/lab9$ ls backup/week9
b.txtPractice 3
student@ubuntu:~/lab9$ rmdir backup
rmdir: failed to remove 'backup': Directory not empty
student@ubuntu:~/lab9$ cp notes copy
cp: -r not specified; omitting directory 'notes'
student@ubuntu:~/lab9$ mkdir x/y
mkdir: cannot create directory 'x/y': No such file or directory
student@ubuntu:~/lab9$ cd c.txt
bash: cd: c.txt: Not a directory
student@ubuntu:~/lab9$ rm backup
rm: cannot remove 'backup': Is a directorybackupis not empty;rmdirremoves only empty folders. Userm -r backup.notesis a folder; copying a folder needs-r. Usecp -r notes copy.- The parent
xdoes not exist. Usemkdir -p x/y. c.txtis a file, andcdonly enters folders. To read the file usecat c.txt(it is empty, so nothing is printed).- Plain
rmdoes not remove folders. Userm -r backup.
Practice 4
mv draft.txt final.txtmkdir -p reports/2026/sepcp -r site site-backupcd ..ls -la(orls -al, orls -l -a)grep -i finish todo
Run in lab9, the answers give:
student@ubuntu:~/lab9$ ls
backup c.txt final.txt notes reports site site-backup
student@ubuntu:~/lab9$ cd reports/2026
student@ubuntu:~/lab9/reports/2026$ cd ..
student@ubuntu:~/lab9/reports$ pwd
/home/student/lab9/reportsPractice 5
Task 1: a new account shows .bash_logout, .bashrc and .profile, plus the special . and ... In your VM the desktop has added more hidden items, such as .cache and .config, and the nine desktop folders appear too. Programs also add their own: in the test account, less and nano created .lesshst and .local for their history. The test account used for this page, after the Part 7 walkthrough and task 4, showed:
student@ubuntu:~$ ls -a
. .. 2022 .bash_logout .bashrc .lesshst .local .profile projects todoTask 5 prints:
student@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 labThe first command matches only the lowercase finish; -i also accepts Finish; lab appears in two lines. Task 7 is rm -r lab9 and then rm todo in your home folder, after which ls shows neither of them.