The Linux Command Line
Lab design credit goes to Jack Applin
Description
-
This lab will cover many common tasks using
bash
. -
You will accumulate a file of commands and show that file to the TA for credit.
-
When you see a numbered question (101, 102, …) do the command, then copy & paste that command into your file.
-
It might be easier to keep a separate window where you edit the file of commands.
-
If you don’t know how to use an editor, use
gedit
ornano
. Otherwise, use whatever editor you prefer. Avoidnotepad
—that’s a Windows program. -
To obtain more information about a command, use
man
commandname orhelp
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. Usecat
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 thanfoobar
. -
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 directoryfoo
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 toinfo
’s home directory. -
What command will tell you where you are? Do it.
-
Use
cd
to go to thebin
directory undercs165
’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~cs165/pub/hamlet.txt
. Did you get all that? -
Use
less
to view~cs165/pub/hamlet.txt
. Useq
to exitless
. -
Use
head
to view the beginning of~cs165/pub/hamlet.txt
. -
Use
tail
to view the end of~cs165/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~cs165/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~cs165/pub
to your home directory. -
How many lines are in
dwarfs
? Use a command do do the counting. Do not justcat
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 filenow
. -
Use
cat
to add the file~cs165/pub/dwarfs
to the end ofnow
. -
Use
grep
to show the three words in~cs165/pub/common-words.txt
that contain “xy”, and then send that output tosort
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 lettera
, the letterq
, or the percent sign. -
[3-9]
: Any digit3
through9
, inclusive.
-
-
A few examples:
-
*
: all files -
a*b
: any file whose name begins witha
and ends withb
. -
zulu[aeiou]
: any file whose name is withzulu
followed immediately by a vowel, which ends the name:zulue
,zulua
,zuluo
, but notzuluooo
orzuluax
.
-
-
Change your current directory to
~cs165/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 agrad
or two. -
Each file has nine permission bits. Consider this, where
%
is my prompt:
% ls -l ~cs165/pub/dwarfs -rw-r--r-- 1 cs165 class 45 Jan 2 2018 /s/bach/a/class/cs165/pub/dwarfs
-
The initial
-
indicates that this is a plain file. -
The next three bits are for the owner (cs165, 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).
-
Copy the
dwarfs
file to your home directory. -
In your home directory, execute
chmod ug=rw dwarfs
to give yourself (the user) and others in your group (other students) read & write access to this file. -
Verify those permissions using
ls
.
-