Commands Lab
How Labs Work
- All of these labs follow the same basic format.
- You will do a bunch of small tasks, in order to gain practice in
whatever the subject is.
- To prove that you’ve done the small tasks, you will accumulate a file
called
results.txt
and turn that file in for credit via
Canvas.
- It might be easier to keep a separate window where you
edit
results.txt
.
- If you don’t know how to use an editor, use gedit or nano.
Otherwise, use whatever editor you prefer. Avoid notepad—that’s a
Windows program. Don’t use any Windows editor, because they produce
files with strange line endings, that will annoy the TA grading your
work.
What gives people feelings of power
Money | |
Status | |
Using the terminal in front of non-programmers | |
Description
- Do this lab on a CS Deptartment computer.
If you can’t get to one physically, connect to one via the internet.
- This lab will cover many common tasks using bash.
- When you see a numbered question (101, 102, …):
- Add a line starting with, e.g., 101 to
results.txt
.
- Do the command.
- Copy & paste that command into
results.txt
.
- Generally, I want the command itself, not the output of the
command.
date
, not Thu 21 Nov 2024 07:00:06 AM MST
.
- To obtain more information about a command, use man
commandname or
help
commandname (for built-in commands).
- Once you can view a file with cat, use it to check your work.
For example, if you’re making a copy of a file, don’t just have faith
that you did it correctly. Use cat to verify your success.
Part 1 — Paths
- A Linux path is a series of components,
separated by slashes (
/
).
- UPPER and lower case are different;
FOOBAR
is different
than foobar
.
- If it starts with
/
or ~
, it’s an absolute path.
- Otherwise, it’s a relative path—relative to the current directory.
- Your current directory is where you are. When you log in, you’re in
your home directory.
- The
cd
command changes your current directory. With no argument,
it takes you back to your home directory.
- The pwd commands tells you where you are.
- Special directories:
~
is your home directory.
~bob
is bob’s home directory.
~/foo
is the directory foo
underneath your home directory.
.
(dot) is the current directory—think “here”.
..
(dot-dot) is the parent directory—one level up from
where you are.
- Use the
cd
command to go to info
’s home directory.
- What command will tell you where you are? Do it.
- Use
cd
to go to the bin
directory under cs253
’s
home directory.
- What command will tell you where you are? Do it.
- Use
cd
to go back to your home directory.
You are copy & pasting all your commands into another file,
to show the TA, right? It would be a shame to do all this and not
have anything to show to the TA.
                
Part 2 — Viewing Files
- Use cat to view
~info/printers
.
- Use cat to view
~cs253/pub/hamlet.txt
. Did you get all that?
- Use more to view
~cs253/pub/hamlet.txt
.
Use q
to exit more.
- Use head to view the beginning of
~cs253/pub/hamlet.txt
.
- Use tail to view the end of
~cs253/pub/hamlet.txt
.
Part 3 — Listing files, making files & directories
A few file commands:
- ls: list files
ls -l
: a long listing of files in the current directory
ls -l
filename: a long listing of some file somewhere
ls -l
directory: a long listing of another directory
ls -l -d
directory: a long listing of the directory itself,
not what it contains.
ls -ld
directory: same thing
- cp old new: copy a file
- mv old new: move (rename) a file
- rm file: remove (delete) a file
- mkdir directory: create a directory
- rmdir directory: remove an empty directory
rm -r
directory: remove an directory, and the files
it contains, and all the directories underneath it, etc.
Be careful!
- Go to your home directory.
- Copy the file
dwarfs
from ~cs253/pub
to your home directory.
- Rename it to
seven
.
- Make a new directory called
Zulu
.
- Move
seven
to that new directory.
- Generate a long listing of
seven
.
- Get rid of that new directory.
Part 4 — Commands
- date: show the current date & time
- diff file1 file2: show the differences between two files
- grep pattern file: show lines that match a pattern
- wc file: count lines, words, and characters
- Copy the file
dwarfs
from ~cs253/pub
to your home directory.
- How many lines are in
dwarfs
?
Use a command do do the counting.
Do not just cat the file and count it yourself.
- Use your favorite editor to alter the names of some of the dwarfs
in your copy of
dwarfs
.
- Use diff to show the changes you’ve made.
- Use grep to find the dwarfs whose names contain a “u”.
Part 5 — Redirection
- There are several ways to re-route the output of a command.
>
sends the output to a file, overwriting any previous contents.
>>
appends the output to a file, adding to any previous contents.
|
sends the output to another program.
- Run the date command, sending the output to the file
now
.
- Use cat to add the file
~cs253/pub/dwarfs
to the end of now
.
- Use grep to show the three words in
~cs253/pub/common-words.txt
that contain “xy”, and then send that output to sort
to put them in alphabetical order.
Part 6 — Filename Patterns
- You can use filename wildcards, or filename patterns, to select
certain files.
- Don’t confuse these patterns with regular expressions, which are
used by grep.
- A few filename pattern characters:
?
: matches any single character
*
: matches anything of any length
[aq%]
: The letter a
, the letter q
, or the percent sign.
[3-9]
: Any digit 3
through 9
, inclusive.
- A few examples:
*
: all files
a*b
: any file whose name begins with a
and ends with b
.
zulu[aeiou]
: any file whose name is with zulu
followed immediately by a vowel, which ends the name:
zulue
, zulua
, zuluo
, but not zuluooo
or zuluax
.
- Change your current directory to
~cs253/pub
.
- List all the files whose names contain a dot.
- Copy all the files with eleven-letter names to your home directory.
Do not do the counting yourself. This is a job for a computer.
Imagine a directory with hundreds of files in it.
Part 7 — File Permissions
- Each file has an owner and a group.
- Most of you are in the group
under
, for undergrads.
We might have a grad
or two.
- Each file has nine permission bits. Consider this, where
%
is my prompt:
% ls -l ~cs253/pub/dwarfs
-rw-r--r-- 1 cs253 class 45 Aug 28 2022 /s/bach/a/class/cs253/pub/dwarfs
- The initial
-
indicates that this is a plain file.
- The next three bits are for the owner (cs253, in this case).
rw-
means that the owner can read, write, but not
execute this file.
- The next three bits are for the group (class, in this case).
r--
means that other users in this group can read,
but not write or execute this file.
- The next three bits say the same thing for everybody else (like you).
- From your current location,
copy the
dwarfs
file to your home directory.
- Go to your home directory.
- In your home directory, execute
chmod ug=rw dwarfs
to give
yourself (the owner/user) and others in your group
(other students) read & write access to this file.
- Verify those permissions using ls.
How to submit your work:
In Canvas, check in the
file
results.txt
to the assignment
“Lab01”.
                
How to receive negative points:
Turn in someone else’s work.