Unix/Linux Shell Cheat Sheet

This document was originally created by Carlos Ovalle for INF 312, Information in Cyberspace, in 2003.

This page is tended to give you a brief introduction to navigating a command line in case you're ever faced with the blinking cursor. We'll discuss listing files in a directory, moving to a directory, and copying and renaming files. This is just the tip of the iceberg when it comes to working with the command line in a Unix and Linux environment. If you want to learn more about Unix and Linux systems, check out the popular line of O'Reilly books.

To access the command line, you'll need to have access to a server running Unix or Linux. Once you have access, you'll need to connect to the system using a client.
NOTE: At the School of Information, our main server is found at login.ischool.utexas.edu.

  • On a Mac: Go to Applications -> Utilities -> Terminal. At the command prompt, type
    ssh [your_login_name]@[your_server_name]
    NOTE: OS X is Unix-based; you can actually use all of these commands on the Mac itself by running Terminal, without connecting to a Unix or Linux server

  • On a PC: Use a secure terminal client. UT provides SSH Secure Shell for students, faculty and staff at its Bevoware site.


In the following examples, the server is at "cyberspace.ischool.utexas.edu" and the login name is "faux."

Once you've logged in, you'll be greeted with something that looks like the following:

Last login: Wed Mar 17 17:12:27 CDT 2010 from 173.172.98.241 on pts/7
Last login: Wed Mar 17 17:26:32 2010 from 173.172.98.241
faux@cyberspace ~ $


Contents

pwd

The first command we'll use is "pwd," which stands for "print working directory." That tells us what folder we're currently in. When you log in to the server, you begin in your "home" directory. Type:


pwd

And you should see something like this:

/home/faux

Your home folder has the same name as your login name. This command shows you that you are currently in a folder (named your login name). Your home folder is probably nested in the server hierarchy along with other user's folders.

ls

The next command we'll use is "ls." ( lower case LS )

On a Windows system, just having a folder open will show you the files in your directory. On a command-line system, you need to use a command to list the files. In Unix and Linux, the command is "ls."

The syntax for the "ls" command is:

ls -[option] [nothing, filename, or directoryname]

First, type "ls." You should see your public_html directory listed, which is where all your i312 assigments are stored.

public_html

This command lists all of the files and folders in the current directory. "ls" by itself doesn't give us a lot of information, even when there are files in the directory, as you can see. We will add the "a" and the "l" options to get more information. "a" shows us hidden files and "l" gives us a long list. The long list provides more information. Try:

ls -la

You will probably end up seeing something like this:

drwxr-xr-x  4 faux   users  4096 Feb  3  2009 .
drwxr-xr-x 45 apache apache 4096 Jan 17 12:30 ..
-rw-------  1 faux   users   326 Mar 17 17:26 .bash_history
-rw-r--r--  1 faux   users   127 Jun  5  2008 .bash_logout
-rw-r--r--  1 faux   users   193 Jun  5  2008 .bash_profile
-rw-r--r--  1 faux   users   551 Jun  5  2008 .bashrc
drwx------  2 faux   users  4096 Jun  5  2008 .ssh
drwxrwxr-x  2 faux   users  4096 Jul 22  2009 public_html

If your directory had more files in it, you might see more information similar to this:

<img src="images/ls.gif" alt="ls"/>

For reference, the first part shows us directories and permissions. Anything beginning with a "d" is a directory. This long list also shows us the owner of the file - in this case, cjovalle - and the group the owner belongs to - in this case, instructor. Next is the size of the file, in bytes, followed by the last time the file was accessed or modified. Finally, it lists the name of the file or directory. For our purposes in this module, the important information is the "d," indicating a directory, and the name.

You will also see something like this:

<img src="images/periods.gif" width="371" height="32" alt="periods"/>

The single period "." always refers to the directory you are currently in.

The double period ".." always refers to the enclosing directory.

You will also see two files named .bash_profile and .bashrc. Why couldn't you see them before? The "." in front of a file name means that the file is hidden. Using the "a" option shows us those hidden files. These two files are installed by default, so don't worry about them right now.

Moving to a directory

The command to move to another directory is called "cd," which stands for change directory.
The syntax is:

cd [directoryname] 

Try:

cd public_html

After that, use the "pwd" command and the "ls -la". It should show you that you are in your public_html directory and should show a listing of all the files you have worked on for the class so far. You should also see your images directory. Feel free to navigate over there and see a listing of your images.
From there, we want to go back up one level to your home directory. Recall that ".." always refers to the enclosing directory. Type:

cd ..

and you should be back in your initial directory. Use "pwd" again to check. (FYI, "cd" by itself also always takes you back to your home directory, no matter which directory you are in.)

Copying a file

The command to copy a file is "cp," which stands for copy. The syntax is:

cp [file] [newfile]

Try

cp firstfile copyfile

Use "ls -la" and you should see two identical files with different names.

Renaming and moving a file

The command to both rename and move a file is "mv," which stands for move. The syntax to rename a file is:

mv [file] [newfilename]

Try:

mv copyfile secondfile

Type "ls -la" to see if the copyfile has been renamed to secondfile.

The syntax to move a file is:

mv [file] [directory]

You already know how to use the "ls" command to list files in a directory. Now we will learn how to create a text file using pico, copy a file, rename and move a file, and delete a file. As in the last section, you have different resources to learn from. You can view the videos and/or read the text below.

pico

pico is a popular command-line text editor. The syntax to use it is:

pico [filename]

Try:

pico firstfile

You should see a screen that looks similar to this:

<img src="images/pico.gif" alt="pico screenshot" width="400" height="289"/>

Type a small message, such as "This is my first file." Hit enter.

We want to exit now. The "^X Exit" listed at the bottom of the screen means to use the control key combined with the X key at the same time to exit. Go ahead and use Control and X, and say "yes" when it asks if you want to save the file. Hit enter to save the file. (Pico indicates that you should use the tab key, but you must hit enter to save.) Use the "ls -la" command to verify that your file was created. If you want to go back and edit your file, type "pico firstfile" and it should open the file where you left off.

Pico can be a fairly powerful text editor, but it takes some getting used to if you're unfamiliar with it. If you need extra help you should check the tutorial <a href="http://www.helpdesk.umd.edu/documentation/unix/pico.shtml"> Using the Pico Text Editor</a> created by the University of Maryland Office of Information Technology.

Copying a file

The command to copy a file is "cp," which stands for copy. The syntax is:

cp [file] [newfile]

Try

cp firstfile copyfile

Use "ls -la" and you should see two identical files with different names.

Renaming and moving a file

The command to both rename and move a file is "mv," which stands for move. The syntax to rename a file is:

mv [file] [newfilename]

Try:

mv copyfile secondfile

Type "ls -la" to see if the copyfile has been renamed to secondfile. Now let's recreate the temp directory, and then use "mv" to move the second file there. The syntax to move a file is:

mv [file] [directory]

First, recreate the temp directory:

mkdir temp 

Next, move your secondfile to the temp directory:

mv secondfile temp

Now we can go to the temp directory by using "cd temp" and then use "ls -la" to make sure that the file was moved.

Deleting a file

The command to delete a file is "rm" which stands for remove. The syntax is:

rm [file]

Try:

rm secondfile

Now try the "ls -la" command to make sure that the file has been deleted.
NOTE: ONCE YOU USE THE RM COMMAND ON A FILE, IT IS GONE, PERIOD. Really. No joke. It's gone. Anyway, be careful. ^_^

Go ahead and go back to your home directory now, by typing "cd .." or just cd by itself.


Assignment

Part 1: Use pico to create a .plan file.

A .plan file is a special file readable by other people. It's the equivalent to your AIM Buddy profile. We'll show you how to read others' .plan files in a later section. To create your .plan, type:

pico .plan

Then type whatever information you'd like others to see (such as your major, hobbies, etc.), and ^X to save the file. This file should be in your home directory.

Part 2: copy a file from another directory into your public_html directory (hint: the directory you made in assignment 1). To do this type:

cd

This command brings you back to your home directory if you were not already in it and will keep you there if you already were.

Now type the following command to copy the file called index.html and put it into your public_html directory. Note: You must type this command exactly as it appears, including the word "example" (i.e., do not replace "example" with your eid). This will copy the template you will use later in the module to create your home page. It is taking a file called 'index.html' from a class directory on the cyberspace server called 'example' and moving it to your public_html directory in your part of the cyberspace server. Note: Remember that the cp command will not warn you if it is going to overwrite a file--so if you have already created an index.html file, be very careful with this step.

cp /home/example/index.html public_html/index.html

If you are getting an error, type:

pwd

You should see /home/instructor/your_cyberspace_login_name after you type pwd and hit enter. If not repeat the two commands above.

</div>

Here are a few other commands that are kind of interesting.

finger [user]

The finger command will provide information about a user. Part of the information is whatever is placed in the .plan file. Type "finger [username]" to take a look at your plan.

who

The who command lists everyone that is currently logged on.

write [user]

"write" requires another person's help. Write will let you chat with another person on the server. Use "who" or "finger" to see if anyone else is logged on. Type "write [user]" and you should be able to start a chat with that user. Everything you type from that point will be seen by the other user, until you hit Control and C to quit the "write" application.

less [file]

The "less" command allows you to read a file. Other options within the less command allows you to search for text, scroll back and forth, and a few other things. Try "less .plan" to read your .plan file. Type q when you are done.

clear

Clears the screen.

man [command]

The command "man [topic]" will allow you to pull up the help file on a certain subject, if a page exists. In all honesty, man pages are often cryptic, so you might have better luck with a Google search for the command.

exit

or

logout

Use the "exit" or "logout" command to quit. Or, you can simply close the window or quit the terminal program.