Class Notes: UNIX command line
Remember, try using the TAB key to auto-complete command names and file names.
This is a cool feature of the tcsh shell. Mis-type a command? Try using
tcsh's history. Use the up and down arrow keys to scroll through your most
recently issued commands. Also, remember that typing ctrl-c (Hold the
control key and type c) will cause an interrupt signal and should normally
return you to the command prompt.
- ssh username@linux.gl.umbc.edu Here, username is
your GL account username. This connects you remotely to one of the
linux servers. Alternatively, you could use the command ssh
linux.gl.umbc.edu, but then you would have to enter your username in
addition to your password. Finally, you might be asked if you wish to
continue connecting. If you type yes and press enter, you will connect
to the remote host.
- tcsh This switches you to the tcsh shell, as opposed to
bash, csh, etc. If you are already using the tcsh shell, you can skip this.
- pwd This will tell you what your current working
directory is, that is, the directory you are "in". You begin in your home
directory.
- date Display the system date and time. This is the time
Professors will use when deciding whether or not the projects you submit are
late.
- who Show the users currently logged on to the machine you
are working on.
- who | sort Display the same list of users in
alphabetical order. The sort command can be used in this way to do many
useful things. Can you think of some other uses? Also featured in this
command is the debut of the "pipe" operator. The pipe | (on most
keyboards hold shift and type the backslash key) sets the output of the
command preceding it to be the input of the command that follows it.
Thus, in this example, the output of the who command is sorted. Why can't we say "sort who"...?
- ls List the contents of the current working directory.
- ls -a Include hidden files (files that begin with .) in
the directory listing. For example, the tcsh shell configuration file can be
found in your home directory, and it is named .cshrc.
- clear Clear the screen. This command doesn't do
anything other than cleaning up the screen for you.
- history List a history of the commands you've issued
recently.
- alias h history Create the alias h. Now, when you issue
the command h, it will have the same result as issuing the command history.
- h Now the same as the command history.
- !2 Issue the command numbered 2 in the list of commands
given by the history command. If you have followed this example precisely,
that would be the date command, as date was the second command entered after
changing to the tcsh shell. In general, !n repeats the command numbered n in
the history. The ! operator is a tcsh shell utility.
- ls -p Like ls, but each directory listed is
followed by a forward slash (/).
- ls -l
"Long" directory listing. Displayed are file permissions, file owner, file
size (in bytes, or number of characters), last modification date, file name.
- ls -R
Recursive version of ls. That is, ls will be performed on each directory in
the directory tree that has the current working directory at its root.
- ls -1
List one file/directory per line.
- ls -t
List files, sorted by modification date, latest at the top.
- ls -r
The -r option reverses the sorting order. Here, this will cause a reversed
alphabetical listing. Combined with -t, as in "ls -rt", files will be sorted
by modification date with the most distantly modified file at the top.
- ls -pal
An example of combining command options. This does ls with all of the effects
of the -p, -a, and -l options.
- mkdir cmsc121
Assuming you were still in your home directory when you issued this command
and that you don't already have a directory named cmsc121 there,
the directory named cmsc121 is created within your home directory. That is,
the directory with full path is:
/afs/umbc.edu/users/u/n/username/home/cmsc121, where u, n, and username are
determined by your username.
- pwd
"Print working directory" displays the current working directory. At this
point, it should output the name of your home directory.
- cd cmsc121
"Change directory" from current_directory to current_directory/cmsc121. Here,
current_directory happens to be your home directory. Alternatively, we could
have given the full path, as in: cd
/afs/umbc.edu/users/u/n/username/home/cmsc121. Also, we could have
issued any of cd ~/cmsc121, cd ~username/cmsc121, cd $HOME/cmsc121, or cd
./cmsc121. This is
because ~, ~username (your username), and $HOME are equivalent to your home
directory. Also, the chracter "." is a special "directory" found in all
directories that is equivalent to your current working directory. The
directory named "..", incidentally is also in every directory and is
equivalent to the parent directory of the current working directory. For
example, your home directory is the parent directory of the cmsc121 directory
you created in step 21. Note that $HOME is the way of using the environment
variable named HOME. There is also an environment variable named cwd, which
stands for "current working directory", and thus if you so desired, you could
have used cd $cwd/cmsc121 in this step.
- cd ..
Change to the directory one level up, which here is your home directory.
- cd ~/cmsc121
Once again, change directories to your newly created cmsc121 directory.
- ls
If you just created the directory cmsc121, it will be empty, and this ls
command will reflect that.
- touch foo1
Create an empty file named foo1. If a file in this directory named foo1
already exists, this touch command will update its time stamp (date and time
of last modification).
- ls -l
If foo1 was just created, you will see that it has size 0.
- touch foo2 foo3
Create two more empty files named foo2 and foo3, respectively.
- ls
Will show all three foo files.
- rm foo1
Permanently remove file foo1. If rm is an alias for "rm -i", you will be
asked if you want to delete foo1. Type y for yes or n for no and then
hit enter. This time, choose y and go ahead and delete the file foo1.
- alias rm 'rm -i'
You must use the single quotes here, as there is a space in the
aliased command. Now, for the remainder of the session, when you issue the rm
command (with no other options), the shell will actually carry out the command
with the -i option, which forces the user prompt described in the previous
step. This is a much safer way to delete files. There is a way to modify
your .cshrc file so that this alias is in use every time you log in.
- cd ..
- ls cm*
List all files/directories whose names begin with cm. * is the wildcard
symbol, so it can be replaced by anything. Among those files/directories
listed should be cmsc121. Here, "sc121" is substituted for the *. If there
was also a file named cm.txt, then ".txt" would be substituted for the *,
resulting in another match, and so cm.txt would be listed along with cmsc121
and anything else beginning with cm. You should note also, that * can be
replaced by nothing as well, so if there were a file simply named cm, then it
would have been included in the listing as well.
- ls *msc*
List everthing with msc somewhere in its name. Among those listed should be
the directory cmsc121.
- rm cmsc121
Attempts to remove the directory cmsc121. Here, it fails because the cmsc121
is not empty - it contains files (the foo files).
- echo $HOME
Display the value of the environment variable $HOME.
- echo $cwd
Display the value of the environment variable cwd. This is the same as the
command pwd. In this case, the result should be your home directory, since
that's where you are if you've been following along with all of these steps.
- echo ~
Same as echo $HOME.
- echo $PATH
Display the value of the environment variable PATH. PATH is a list of
directories, each separated by a colon (:). When an executable command or
file is given at the command line, the shell looks in the current directory to
find the command/file. If not found, then each directory in PATH is searched,
one at a time, until the command/file is found, and then that is the
command/file that is executed. Of course, it's possible that the command is
not found. Adding a directory to PATH is useful when you wish to be able to
execute a file from any directory, without having to qualify it with the full
path.
- which emacs
Shows the full path of the shell command "emacs".
- which which
The which command is built into the shell.
- man which
Displays the UNIX manual entry for the which command. Use the man command
along with any UNIX command as argument to find help information. Type q to
exit the man page. Collectively, these help files are known as the "man
pages." They are loathed by most beginner UNIX users, but once you get used
to them, they're not quite as bad.
- cd ~/cmsc121
- ls f*
List files beginning with f.
- clear;pwd
The semicolon (;) is used to issue multiple commands on one line. The
commands are issued in order, from left to right. This example series of
commands clears the screen and proceeds to print the current directory.
Spaces surrounding the semicolon are unimportant - we could have typed clear
; pwd
- ls > foo_list
The right arrow character (>) is the redirection operator. Rather than
displaying the output of the ls command on the screen (which is normally the
"standard output device", the output is written to the file named foo_list.
If foo_list did not exist prior to this command, then it has been created. If
foo_list did exist prior to this command, then it has now been overwritten
completely. That is... unless you have safeguarded against accidentally
overwriting files via standard output redirection. Try doing a Google search
for the term noclobber and see what you find.
- ls -l
The file foo_list has appeared.
- cat foo_list
The cat command simply outputs the file argument to the screen. An
interesting thing here is that foo_list is inluded in the output. This is
because when we redirected the output of ls to foo_list, the file foo_list
first had to be created before being written to, so it existed when the ls
command was executed. This is why foo_list is included in the directory
listing.
- ls -aR ~/.. | more
List all files in your GL account and pipe the output into the command more.
Since there is so much output, the more command allows you to see the output
one screenful at a time. Type q to exit back to the command prompt. You can
look at the man page for more to find out more about more. For this, enter
the command "man more".
- ls -aR ~/.. | less
The less command works like the more command, only it allows you to scroll
both forward and backward through the displayed text. Read about less with
"man less".
- who | wc
The wc command stands for "word count." wc outputs 3 numbers for each file or
body of text given as input (here, its input is the output of the who
command). The first number is the number of lines in the input, the second
number is the number of whitespace-separated words in the input, and the third
number is the number of characters in the input (each character is exactly one
byte). Remember, whitespace includes things like spaces and tabs. Now, since
who outputs one user per line, the first number output by this command (who |
wc) is actually the number of users currently logged on to the machine on
which you are working.
- ls -1ap | grep /
The grep command searches for occurences of its first argument within its
second argument. Here, its second argument is supplied via the pipe from the
ls command. Each line containing the character sequence being searched for is
displayed. Here, we end up listing only directories. Do you see why?
Definitely look up grep in the man pages.
- ls -l ~ | sort -nr -k 5,5 | less
Here, I assume that the file size is the 5th whitespace-separated item on a
single line of output from the long directory listing. That's where the 5,5
in this command comes from. If this assumption is false in your case, make
changes accordingly. This command displays the files in your home directory
in order from largest to smallest.
- cd ~/cmsc121
To make sure you are in the cmsc121 directory.
- cp foo_list bar
Makes a copy of foo_list and saves it to a file named bar. foo_list is left
unaltered. Remember, the first argument (here, foo_list) is always the
source, and the second argument (here, bar) is always the destination. Don't
mix these up! As you can see, there would be terrible consequences if you did
that. Similarly, if the file bar already exists, you could overwrite it
without ever knowing. So, be careful with the cp command. Read about cp in
the man pages very thoroughly before using it extensively. There is an
option
for helping you to think before overwriting a file with a cp command. As
before when we created the alias rm for 'rm -i', we can create alias cp for
'cp -i'.
- mkdir bar_dir
- cp bar bar_dir
Since the last argument is a directory, it is assumed that we want to copy the
file bar to a file with the same name in the given directory. That is, this
command is equivalent to cp bar bar_dir/bar. Again, read about cp in the man
pages!
- cd bar_dir
- mv bar foo
The mv command works the same as the cp command, except that the source
argument (here, bar) is removed. Essentially, we are renaming bar foo. The
same dangers exist for mv as for cp. Read carefully about mv, and always
remember Source First Destination Second! You can move a file into a
directory the same way you copy a file into a directory. The difference, of
course, is that the moved file is removed. You might want to create an
alias with the command alias mv 'mv -i'.
- ls > foo
- diff foo foo
Display the differences between foo and itself. The output should be nothing
at all since there are no differences.
- touch bar
- diff foo bar
Since bar is created with touch, it is empty, and so the differences between
foo and bar are the contents of foo.
- whoami
Displays your username. 2-4-6-0-1...! (If this last bit doesn't make sense,
don't worry about it.)
- ls -aR ~/.. > foo1
- head foo1
View only the beginning of file foo1.
- tail foo1
View only the end of file foo1.
- exit
This command exits the shell. If you started by issuing the tcsh command,
then you were running a shell within a shell and now must enter the command
exit a second time in order to exit the "outer shell." The logout command can
be used here, but exit is more universal. ctrl-d should also log you out.
We also talked about the script command that produces a file named
typescript. Read about the commands ps and kill in the man pages.
We also discussed how to use the & operator to put processes in the background
so that you can continue to issue shell commands. Also, know
how to use the passwd command to change your password. We discussed
using pico and pine for editing files and managing email, respectively.
Other very important commands that I will add to the end of this
exercise in the future include: tar, gzip, gunzip, chmod, talk, write, lpr,
and lpq (to name a few).