Carnegie Mellon University Website Home Page
 

Commonly Used UNIX Commands

There are a few things you need to remember when entering UNIX commands:

  • UNIX commands are typed at a prompt. Very often the prompt is a percent sign %. Sometimes it is the name of the machine followed by the %. The cursor may be a blinking white square on a black background, a blinking black square on a white background, or a blinking color square on a color background.
  • UNIX commands are case sensitive and are usually lower case. This means that ls and LS are completely different commands. At the end of this section is a chart of some commonly used UNIX commands.
  • Spaces are very important. There is always a space between the command and the file or directory it acts upon. In the following example of a command line, cd is the command and /afs/andrew/course/76/271-Summer/dheagy is the directory.
    Example: cd /afs/andrew/course/76/271-Summer/juser
  • In order for a UNIX command to be executed, you must press Enter at the end of a command line. If all goes well, the prompt and cursor appear on the next line waiting for the next command. It often looks like you haven’t done anything, but unless there is an error message, your command has been executed. If there is a problem, you will generate an error message, such as “Command not found.” If you generate this type of error, you probably typed something incorrectly. Try again.
  • If you type something and you want to get out of it, press Ctrl + C.

Command

 

Action

cat <file>

 

Print contents of file in the command window

cd <directory>

 

Change directories 

cp <file> <file2> 

 

Copy the contents of file into file2 

history 

 

List history of all commands issued at system prompt 

ls 

 

List the files and subdirectories in a directory  

ls -F

 

List the difference between files and directories (directories will have a /)

ls -l 

 

List files with status information (list the long way)  

ls -lt

 

List file information in long format, sorted by time with newest files (or newly changed files appearing first

ls -a 

 

List all the files in a directory, including dot files  

mkdir <directory> 

 

Make a directory 

mv <file> <file2>

 

Move file to file 2

pwd

 

Print the pathname of the current directory

rm <file>

 

Remove, or delete, files

rmdir <directory>

 

Remove directory

fs la <directory>

 

List access rights for <directory> (for more information on access rights, read File and Directory Protections)

Moving Around the File System (cd)

To move to your home directory:

  1. At the system prompt, type cd and press Return.

    After a moment, the system prompt returns, indicating that you have moved to your home directory (if you were already in your home directory, the prompt will still return as normal).

    No matter where you are in the file system, you can use the cd (change directory) command to get you back to your home directory immediately. Even if you have not changed directories since you logged in, do the following action as a check:

To move to a subdirectory of your own:

  1. Type cd <path> and press return.

    Note: When you are changing directories down from your current working directory, it is not necessary to type the full pathname.

Example
To change from your home directory to your public directory, in your home directory, at the system prompt, you would type:

cd public

To move to someone else's home directory:

  1. Type cd <path> and press Return.

    In the above command, <path> is the full path to the other users directory.

Example
To move to Joe User's home directory, you would use the cd command and specify Joe's home directory:

cd /afs/andrew.cmu.edu/usr11/juser

For more information on cd see the on-line help for cd .

The Tilde (~)

The tilde (~) can be used to abbreviate the pathname to any user's home directory, including your own. It is helpful when you don't know another user's full pathname, or if you just want to save time typing.

Example
Instead of typing cd /afs/andrew.cmu.edu/juser as shown in the previous section, you could use the tilde to take the place of the path to Joe's directory (everything up until the user ID "juser"). The command would look like:

cd ~juser

Note: The tilde can be used with any UNIX command, however, you should never use the tilde in command files such as.login or in your preferences file (see the .login or preferences help files for more information). In special files such as those, the tilde may not be recognized and can prevent Andrew and UNIX from working properly for you.

The Dot and Dot Dot

The dot(.) stands for your current working directory. You can use it in commands like cp or mv . For more information, see copying files and moving files.

The dot dot (..) stands for the directory above your current working directory. If you are in your sample directory, then the dot dot represents your home directory. It can be used with commands like cp , cd or mv.

Knowing Where You are in the File System (pwd)

As you move around in the file system, UNIX does not tell you that you have moved to any specific place. You can ask UNIX to tell you where you are by using the pwd (print working directory) command. To use this command, at the system prompt:

  1. Type pwd and press Return:

    The pathname of your current working directory is printed out on your screen. You will see something like the following at the system prompt:
    /afs/andrew.cmu.edu/usr<user#>/<youruserID

Seeing the Contents of a Directory (ls)

To see the names of the files and subdirectories stored in a directory, use the ls (list) command. The ls command has a number of options, known as switches, which can be added to the command to make it perform a specific action.

To list your file and directory names:

  1. Type ls at the system prompt and press Return:

    A list of the files in your home directory appears. This list does not include dot files (files that begin with a dot). See the section below for more information about what dot files are.

To list your files with status information:

  1. Type ls -l at the system prompt and press Return:

    A list of the files in your home directory appears. For each file you should see its name, its owner, the date it was last changed, and its size in kilobytes. Those files that are directories have a d before their names; those that are plain files have an -rw- . This list does not include dot files (files that begin with a dot). See the section below for more information about what dot files are.

To list your file names and see the difference between file and directories:

  1. Type ls -F at the system prompt and press Return:

    A listing of all the files and directories appears. Directories will have a "/."

To list all your files, including dot files:

  1. Type ls -a at the system prompt and press Return:

    A listing of all the files in your home directory appears, including dot files.

Dot files

Dot files are used primarily to control system functions. They are called dot files because their names begin with a dot (.). Although you can modify some of them if you like, it is not necessary for you to edit or change them in any way. In fact, unless you are following the directions in a help document, or know a great deal about UNIX and how it functions with Andrew, you should not add or delete anything from a dot file.

There is also a file called simply " . " and one called " .. ". These represent your current directory and its parent directory, respectively.

Create a New Directory (mkdir)

There may be times when you want to create a new directory in which to store information. By placing files with common subjects into their own directories, you can organize your Andrew files.

To create a new directory:

  1. Type mkdir <directoryname> at the system prompt and press Return.
    After a pause, the system prompt returns, indicating that a directory named sample has been created as a subdirectory of your home directory.
  2. Use the ls command to verify that the new directory was created.

Copy Files (cp)

You may want to make a copy of a file, either in the same directory, to another directory, or from someone else's account into yours. You copy a file using the cp command. Whenever you copy a file into the same directory, you must give it a new name. When copying to or from another directory, you can change the name if you like, but it is not necessary.

To copy a file in the same directory:

  1. Type cp <file> <file.copy> at the system prompt and press Return.
    After a moment, the system prompt returns, indicating that the file has been copied.

To copy a file into another directory.

  1. Type cp <file> <directory> and press Return.
    After a moment, the system prompt returns, indicating that the file has been copied into the directory specified.

    Note: In this case you did not rename the file. This is not necessary because the files, are in separate directories and therefore have different pathnames by which Andrew can distinguish them.

To copy a file from another user's account:

  1. Type cp <path>/<file> <path>/<file> and press Return.
    Where <path>/<file> is the full path and file name of the file you want to copy and <path>/<file> is the path and name of where you would like the file to be copied to.

    After a moment, the system prompt returns, indicating that the files has been copied.

Example
If you wanted to copy a file called notes from your friend Joe's account into your sample directory and name that file notes.joe, you would type:

cp ~juser/notes sample/notes.joe

For more information on copying files see the on-line help for cp.

Move Files (mv)

Another way of getting files from one place to another is to move them using the mv command. The difference between mv and cp is that cp puts a copy of the file in a new location without disturbing the original copy, but mv deletes the file from its old location after saving it in the new location. You can use mv anyplace that you would use cp.

To move a file a to new file in the same directory (i.e rename a file):

  1. Type mv <file> <file2> at the system prompt and press Return.

Example
If you want to move a file named notes.joe to a file named notes.working, type:

mv notes.joe notes.working

Note: Using mv in this case is simply renaming the file.

To move a file to a new file in a different directory:

  1. Type mv <file> <path>/<file> and press Return.

Example
If you want to move a file called notes from your home directory into your public directory, you would use the following command while in your home directory, at the system prompt:

mv notes public/notes

For more information on moving files see the on-line help for mv .

Move Directories

Moving a directory is exactly the same as moving a file. You would use the mv command to move or rename a directory.

To move a directory to a new directory in the same directory (i.e rename a directory):

  1. Type mv <directory> <directory2> at the system prompt and press Return.

To move a directory to a new directory in a different directory:

  1. Type mv <directory> <path>/<directory> and press Return.

For more information on moving directories see the on-line help for mv.

Remove Files and Directories

You may want to get rid of a file, or an entire directory. Before you delete anything, make sure you really want to get rid of it permanently, because it is difficult to retrieve deleted files (though it is possible, see the help document Your Andrew Account.

Removing a file

To permanently remove a file, you need to use the rm command. To remove a file:

  1. Type rm <file> at the system prompt and press return.

Example
Suppose you are finished with the notes.working file and you want to remove it. To do so, you would type:

rm notes.working

For more information on removing files see the help document on rm .

Removing a Directory

To remove a directory you would use the rmdir command. To remove a directory:

  1. Use the cd command to move up one level from the directory you want to remove.
  2. Type rmdir <directory name> at the system prompt and press Return.

    This will permanently remove the specified directory.

If you receive an error message telling you the specified directory is not empty, this means you must not have removed all the files from it. To remove a directory that contains files, use the rm -r command; see the rm help file for more information.

Example,
If you have a directory called sample, which is a subdirectory of your home directory, you would first change to your home directory by typing:


cd

and then you would type:


rmdir sample

and press Return to remove the directory, sample.

For more information on removing directories see the help document on rmdir.

Last Updated: 8/24/07