Common Terminal Commands
Here is a very brief collection of some commands that you can use in a shell.
The man command
One of the best and easiest ways to learn more about these commands, or to figure out how to use them or what options they support is to use the man command (short for "manual pages").
Just type:
man <theCommand>
into a shell to open the man pages for a command. It will temporarily "take over" your terminal window, and you can navigate up and down using the arrow keys or the u and d keys. When you're finished, press q to quit. The usefulness of man pages varies, but they may include:
- Descriptions of what the command does
- Examples of how to use the command
- Dictionary of flags and arguments that work with the command
Type:
man pwd
to see the man pages for the pwd command. Navigate up and down using the arrow keys or the u and d keys. When you are finished, press q to quit.
A Brief Discussion of File Paths
The path to a file or directory (i.e. its location in the file system) can be specified as either absolute or relative. An absolute path leaves no ambiguity about its location, and is defined with the root directory "/" as the reference point. For example, the path:
/home/hvjackso/aFile.doc
would be an absolute path.
A relative path, however, is defined from the current directory as a reference point. For example, if you're in a shell and your current directory is /course/cs015/, you could omit that directory when referring to a file that's already in your current directory. So you could refer to a file called:
/course/cs015/guide.txt
as just:
guide.txt
You can carry the usefulness of relative paths a little further by digging into directories. For example, if you're in your home directory "/home/<yourlogin>/" and you want to refer to a file called cs015Homework within a directory called cs015files, the absolute path would be:
/home/<yourlogin>/cs015files/cs015Homework
but the relative path from your current location would just be:
cs015files/cs015Homework
Note that an absolute path always starts from the root directory /, while a relative path does not.
Special Directories: "." and ".."
Linux also provides a way for you to refer to the current directory and it's parent directory when working with relative paths. The symbol . refers to whatever the current directory you're working in is, and .. refers to its parent. For instance, if you want to access a file that is one folder higher than you are right now, you could use the relative path:
../someHigherFile
It is okay if the different types of file paths seem confusing. The only way to get a good understanding is to use it and ask questions as you go along. You can always refer back to this lab in the future if you want more clarification on a topic.
Short List of Useful Linux Commands
| Command | What it does | Arguments | Flags |
ls
|
Lists the contents of a directory | The path of the directory to list. If none is listed, the contents of the current directory will be listed. |
-a: Include hidden files-l: List in "long format" with extra information
|
cd
|
Change directory (to a new working directory) |
The path of the directory to change to. A path that starts with a /
is called "fully qualified", meaning there is no ambiguity about the location
specified. If the path does not start with a /, the path is said to be
"relative," and is determined relative to the current location. If no location
is specified as an argument, then the new directory will be the home directory.
|
(no common flags) |
rm
|
Removes a file | The file to be removed |
-r: Recursively delete files (i.e. allow the deletion of a directory
and everything it contains. Directories cannot be deleted with rm without
the r flag
|
mkdir
|
Makes a new directory | The name (or full path) of the new directory | (no common flags) |
rmdir
|
Removes an empty directory | The name (or full path) of the directory to be deleted | (no common flags) |
zwrite
|
Send a message to a user (sort of like IM) |
The username to send a message to. E.g.:zwrite hvjackso |
(no common flags) |
touch
|
Creates a new empty text file (among other things) |
The name (or full path) of the new file. E.g.:
touch MyNewFile.txttouch /u/hvjackso/course/cs015/NewFile.java |
(no common flags for making new files) |
mv
|
Moves a file from one location to another. This can be used to rename the file. |
The full or relative path of the existing file, followed by the full or relative path of the new file.
Note that the directory can be the same, effectively renaming the file. E.g. mv MyOldFile.java
MyNewFile.java |
(no common flags) |
cp
|
Copies a file from one location to another (similar to mv)
|
The full or relative path of the existing file, followed by the full or relative path of the new file.
Syntax is similar to mv.
|
(no common flags) |
finger
|
Look up information about a user |
The username to look up. E.g.:finger hvjackso |
(no common flags) |
Don't be intimidated by this list. You definitely do not have to memorize this (that's what the man command is for!).
Try these steps in a terminal window, and follow along to see what's happening.
Open a terminal window and type:
cdThe
cdcommand (short for "Change Directory") switches the "current directory" of the shell window you're in. If you specify a path after the command you will be switched to that location; otherwise, you'll switch to your home directory. You should see a tilda as part of the command prompt to show you where you are.-
Type:
lsThis command will show you the contents of a directory. If you specify a directory, it will show you the contents of that directory, but otherwise it will show you the contents of the current one.
In your home directory you should already have a bunch of files. That's fine.
-
Type:
mkdir NewDirectoryThis will make a new directory in the current location called "NewDirectory"
-
Type:
lsNow you can see it is there. You can tell that it is a directory because it has a
/after the name. -
Type:
cd NewDirectoryNotice that the command prompt will change to reflect the new location. Note: file names in Linux are case sensitive; "newdirectory" is not the same as "NewDirectory".
-
Type:
lsThis time you will notice that you get no results. Obviously your new directory is empty, so you don't get anything back.
-
Type:
touch MyNewFilethen type:
lsNow you can see that your new file called "
MyNewFile" is there. -
Type:
rm MyNewFileThis gets rid of the new file you created
-
Type:
cd ..This moves the current directory up a level. You'll see in the command prompt that you're back at your home directory.
-
Type:
rmdir NewDirectoryThis will remove the directory you originally made.
That's it!