Lesson 4 Commands¶
The page has the commands for lesson 4. The commands for this lesson are:
||View a text file.|
||View a large text file one page at a time.|
||View a large text file using scrolling.
||View the first few lines of a text file.|
||View the last few lines of a text file.|
||Count the lines in a text file.|
||View the contents of a binary file.|
||Change the working directory.|
||List files in the working directory.|
||Show the working directory.|
The Current Directory¶
There’s no place like home!
cdwith no arguments takes you to your home directory.
When you use the file tool on your operating system the graphics on the screen show you files and folders at the same time and you pick what you want. On the command line you navigate folders by “walking” from one directory to another. The key concept that you have to remember is that of the
working directory. That’s the folder you’re working in. Think of it like the place you’re standing.
To help you understand the place you are standing let’s play a game from my childhood: Beyond Zork: The Coconut of Quendor
In the game you walk from place to place. The graphic on the screen helps you see the effect of moving in different directions. When you use the command line the
cd command moves you from place to place, the
ls command shows you what’s around and the
pwd command shows you where you are.
Arguments and File Names¶
Many commands take arguments that contains the name of a file or a directory. So how you do you tell a command like
cat where to find a file? In the previous example we named a file in the current directory and cat showed us the contents. Try this starting in your home directory.
$ cd $ cd Poems $ cd Angelou $ cat bird
But what if we want to print the bird file from another place? There are two options.
A relative path is tells a command how to find a file starting from the current directory.
$ cd $ cat Poems/Angelou/bird
In the example you start in the in your home directory. The relative path to a file changes when you change directories. Here are a few different ways to print the contents of the
bird poem. Try running these commands:
$ cd $ cat Poems/Angelou/bird $ cd Poems $ cat Angelou/bird $ cd Angelou $ cat bird
The special directory
.. refers to the directory above the current directory. Start from the
Angelou directory and try the following commands:
$ ls .. $ ls ../.. $ ls ../../..
Absolute paths are not relative to the current working directory. Absolute paths begin with the
/ character which signifies the root directory. The
pwd command always shows you an absolute path:
$ pwd /home/cis90/simben90
Here’s the absolute path of my
$ cat /home/cis90/simben90/Poems/Angelou/bird
The nice thing about an absolute path is that it works no matter where you are. The down side is that they are generally longer than a relative path.
$PATH Environment Variable¶
Where do commands come from? Commands are files that are located in Linux’s system directories. A special environment varialbe
$PATH controls where the shell looks for commands when you enter one. Use the
echo command to show you the path:
$ echo $PATH /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin
The path is a list of directories separated by a colon (
:). The above path has the following parts:
||Extra administrator commands.|
||Commands installed by snap packages.|
$PATHis searched in order!
What happens when you delete your path? Try it.
Most commands are now unavailable! With no
$PATH only shell built-in commands work. You can still use
echo and you can still set a variable. If you find that you have a broken
$PATH you can fix it by ensuring at least the following directories are present:
Run a command to restore your path.
Trapped on the Island¶
During the midterm you’ll login to a special server. When you login you’ll find a broken path. Can you restore your path and escape the island?
Try for yourself by logging in to: