These classnotes are depreciated. As of 2005, I no longer teach the classes. Notes will remain online for legacy purposes

UNIX01/Entering Commands I

Classnotes | UNIX01 | RecentChanges | Preferences

When you enter commands into a shell interface, you will typically see a command prompt similar to this:
 [hart@einstein hart]$

The basic format of these lines is as follows:

 user@hostname path [$|#]

where

  • user is whatever user you are logged in as.
  • hostname is the hostname of whatever computer you are logged in to.
  • path is the path of your current working directory.
  • $ or # determines whether you are logged in as a normal user ($) or an administrator (#).

Thus, in our example above, user "hart" was logged into the machine "einstein" and was sitting in the "hart" directory. The "$" indicated that user "hart" was not and administrator.

This command prompt is actually programmable, and you can make it look however you desire (including using colors). Here are some example alternative command prompts from various UNIXes:

From Debian GNU/Linux?:

 sam@rygel:~/work$

From Sun Solaris:

 (Fri/Jul/11)-(11:26PM)-(\l:root@tux4kids)-(/opt/home/shart)
 #

Entering Commands

To run a program or enter a command, you simply type it at the command line. For example, if I were to run the Mozilla web-browser from the command line, I would type (excluding the shell prompt):
 $ mozilla

This command would start Mozilla and occupy the console. If I were to pass a control character to the running program (such as "CTRL-C", or "interrupt") then the program would deal with it. However, I would not be able to type any additional commands in the console. The reason for this (as we shall see when we examine pipes) is because the shell is really just piping the output from the running program to the console.

If you wanted to run a program in the background (meaning that the program does not lock the console), you would append the "&" symbol:

 $ mozilla &

Be careful using the "&" as some very simple CLI commands do not run well in the background.

Tab completion

One of the best features of the UNIX CLI is "tab completion". When you begin to type a command, you can use "TAB" to have UNIX attempt to complete the command for you. For example, if I were to type "moz" and hit "TAB" I may be given the following:
 $ mozilla

If there is more than one command or program with the beginning characters, then hitting TAB will result in a beep (or possibly a console flash). Hitting TAB again will provide with you the possible choices. For example, typing "net" and hitting TAB may provide the following possible programs:

 sam@rygel:~/work/temp$ net    
 netcardconfig        netkit-ftp           netris-sample-robot
 netcat               netris               netstat

Tab completion also works on directories and any file on your system.

Basic Commands : Navigation

We will now examine some of the more common commands and programs which you can run from the command line to navigate around the file system. Many of these do have correlations with commands in DOS or CP/M, however all of them have more rich options and abilities.

ls

The ls command is the one which you should become the most familier with. It "lists directory contents". The DOS equivalent is "DIR". Simply typing "ls" will produce a short, truncated, listing of the files and directories inside your current working directory. It will not display any hidden files or directories by default.

If you want a more extensive listing of files and directories in your current working directory, then you will want to run something similar to:

 $ ls -la

Where the "l" option indicates you want a "long" listing, and the "a" option indicates you want to see all of the files (even the hidden ones and implied "." and ".." directories).

If you would like to see all of "ls"'s options, then issue a

 $ ls --help

Compatability Note: In recent versions of Red Hat, the "ls" program has been broken!

cd

"cd" is "change directory", and it functions very similarily to "cd" under DOS. cd's usage is very basic:
 cd [dir]

If "cd" is run without an option, then you return to the current user's home directory. If I wanted to "cd" to "/home/frank/pantry/cookies" I would issue:

 $ cd /home/frank/pantry/cookies

cp

"cp" is "copy" and can be used to copy files and directories. It is like DOS's "copy" command, except on steroids.
 $ cp file1 file2

will copy the file "file1" into the file "file2". You can also specify paths thusly

 $ cp /path/to/file1 /some/other/path/to/file2

You can copy directories recursively by using the "-R" or "-r" option. For example,

 $ cp -r /some/directory /another/place

Will recursively copy the directory "/some/directory" to the directory "/another/place".

If you wanted to preserve the attributes of the file, files, or directories being copied, then you would use the "-p" option:

 $ cp -rp /home/tom /home/tom-backup

You can also force a copy to proceed even if there are questions that arise (such as "should I overwrite?") by using the "-f" option:

 $ cp -frp /home/susan /backups/susan-home

cp has many other options which we will not cover at the moment.

mv

"mv" is "move", and is used to move files or directories. It really does not have a DOS equivalent, although it is what is used to rename files, so it does take the place of "ren".

If you wanted to "move" (or rename) a file "bob-car-loan.txt" to "bob-bankruptcy-payment.txt" you would issue the following command:

 $ mv  bob-car-loan.txt  bob-bankruptcy-payment.txt

You can also move across directories. For example, if I wanted to move the file "bob-car-loan.txt" from the "debt" directory into the "foreclosure" directory, I would issue:

 $ mv  debt/bob-car-loan.txt  foreclosure/bob-car-loan.txt

or, more simply

 $ mv  debt/bob-car-loan.txt  foreclosure/

Additionally, mv can be used to rename or move entire directory trees. Let's say that I have a directory tree under "/home/bob" which I would like to move to "/deleted-users", I would issue the following command:

 $ mv /home/bob /deleted-users/

mv can also accept the "-f" option for "force" in case there are any problems or questions:

 $ mv -f /home/bob /deleted-users/

rm

"rm" is for "remove", and is what you use to remove files or directories. It is sort of like DOS's "del", except on steroids. If I simply wanted to remove the file "frank-taco-addict.txt", then I would issue:
 $ rm frank-taco-addict.txt

You can use rm to delete directories recursively, if you use the "-r" or "-R" options. For example, if I wanted to delete "/home/frank" and all files underneath it, I would use:

 $ rm -r /home/frank

If I wanted the delete to occur without prompting me for permission to do so, I could pass the "-f" command:

 $ rm -rf /home/frank

This will forcably delete everything underneath /home/frank.

NOTE: You should never issue a "rm -rf /", as this will delete your entire system out from underneath you.

rmdir

"rmdir" is for "removing directories", and its use is largely depreciated. Virtually everything you can do with rmdir you can do with the "rm" command. Plus, much of what you can do with "rm" cannot be done with rmdir, so there really is no benefit for using rmdir over rm.

Basically, rmdir will remove directories if they are empty. If they are not, you will get an error.

mkdir

"mkdir" is the "make directory" command. If you simply wanted to make a simple directory such as "stuff" in the current directory, you would issue the command
 $ mkdir stuff

If you wanted to make an entire tree of directories, you would use the "-p" option. For example, if you wanted the tree:

 /home/frank/notes/calls/tucson
but only "/home/frank" existed, you would issue:
 $ mkdir -p /home/frank/notes/calls/tucson

pwd

"pwd" stands for "print working directory" and can be used to display your current directory:
 [hart@einstein temp]$ pwd
 /home/hart/work/temp

ln

"ln" allows you to create symbolic and hard links within your filesystem. For example, if I had the file frank_doc.txt and I wanted to make a link to that file called franks_main_doc the command would be:
 ln -s franks_main_doc frank_doc.txt


Classnotes | UNIX01 | RecentChanges | Preferences
This page is read-only | View other revisions
Last edited July 24, 2003 6:16 pm (diff)
Search:
(C) Copyright 2003 Samuel Hart
Creative Commons License
This work is licensed under a Creative Commons License.