Getting Work Done

Help Center Introduction to Unix Getting Work Done

Given that a Unix system isn't very pretty to sit and look at, let's talk about actually getting work done. We want to create, edit, manipulate, and remove files and directories. We want to be able to see where we are in a system, and be able to see what is in a directory. So, let's start off by making sure we have something to look at.

pwd (Present Working Directory)

The pwd command displays the absolute path of your current directory. If you're ever uncertain of where you've wandered to on a Unix system, pwd will tell you where you are.

% pwd
/nfs/guido04/da43/d37/mstark

mkdir (Make Directory)

The mkdir command, as you might guess, makes a directory. The created directory may lie in the directory you are in when you create it, or anywhere else on the system that you specify. For instance, the below set of commands first makes a directory called "temporary" in your current directory, and then a directory called "temporary2" within the "temporary" directory.

% mkdir temporary
% mkdir temporary/temporary2

ls (List)

Lest you be in doubt that the previous commands actually did anything, let's talk about ls. The ls command lists all the files and subdirectories within the current directory.

% ls
temporary
Supplying ls with a specific directory will list files and subdirectories within that directory.
% ls temporary
temporary2
Adding the -l flag (for "long listing") to the command will list all the files and subdirectories, plus more information about each one: permission settings, number of hard links, owner, group, size in bytes, and date and time created.
% ls -l
total 2
drwxr-xr-x 3 mstark user 512 Sep 12 14:29 temporary
Adding the -a flag to the command will list all files and subdirectories (including hidden ones):
ls -a
. .. temporary

Those . and .. artifacts are actually pointers to locations above your current directory. These pointers exist in each and every directory.

chmod (Change Mode)

Now that we have seen a "long listing" of the files in our accounts, it would be a good time to talk about permission settings. Permissions allow you to give, or take access away from a given file to a variety of users/groups to read, write and/or execute a file. Lets do the "long listing" again.

% ls -l
total 2
drwxr-xr-x 3 mstark user 512 Sep 12 14:29 temporary

The first column in every row is the permissions for that file. The first letter tells you whether or not it is a directory (which really isn't a permission), the next three letters are the permissions for the owner of the file (listed in the 3rd column of the "long listing"), the second three letters are the permissions for your group, and the last three are the permissions for others, or everyone else. Each set of permissions has three characters, r (read), w (write), and x (execute).

chmod is a command that lets you modify those permissions. Before you begin to get excited about changing permissions on other people's files, however, you should note that only the owner of the file (or root, the system administrator) can alter the permissions for any given file. The format of the chmod command looks as follows:

% chmod [who][op][permissions] filename

You can change the permissions for the owner (u), the group (g), others (o), or all three categories (a). When you use chmod, it will normally only apply the changes to the file specified. If you want all files and folders within a folder to take on a certain set of permissions, add the recursive tag on the outermost folder: chmod -R.

Let's try it out, by removing (-) the read (r) permissions for users (u):
% chmod u-r temporary
Try to do a ls command again.
% ls -l
ls: temporary: Permission denied
total 0
Let's get those permissions back, and more, using a special command.
chmod 777 temporary

Do another ls and you will see that your permissions are restored. If you do this command on a file that is not a directory, permissions will be "maxed-out," meaning that anyone can read, change, delete, or execute your files. Be very, very careful with the 777 permission setting!

chmod takes either human-readable flags (such as +x for "make executable") or number sequences (such as 777 for "grant all permissions"). For more information on using chmod, please see its man pages.

cd (Change Directory)

The cd command allows you to manipulate your current location by changing your working directory. To change to a specific directory, type cd followed by a subdirectory or a path name. Let's test this out.

% pwd
/rc52/d05/mstark/
% cd temporary
% pwd
/rc52/d05/mstark/temporary
Just to really prove to ourselves that we're in a different directory, try running ls once more.
% ls
temporary2
Voila! We've changed into the temporary directory that we created. "But wait!" you cry. "I didn't really want to be here! I'm lost! Help!" Well, there's several ways to correct this situation. Recall the pointers that we know are in this directory: we can utilize them to return to the directory directly above us.
% pwd
/rc52/d05/mstark/temporary
% cd ..
% pwd
/rc52/d05/mstark/
Hooray! We're back to where we came from! Let's dig deeper :
% cd temporary/temporary2
% pwd
/rc52/d05/mstark/temporary/temporary2
% cd ../../
% pwd
/rc52/d05/mstark/

The nicest feature about cd is that if you don't supply it with any path or directory, it automatically takes you to your home directory. This ability is useful if you've gotten yourself completely lost in a maze of directories. As an example, let's go to the very top level directory, and then navigate back to our home directory.

% cd /
% pwd
/
% cd
% pwd
/rc52/d05/mstark
Home once more.

cp (Copy)

The cp command creates a copy of a file in either your working directory (if it is going to be copied into the current directory, you must supply a new name) or a specified other directory. To be able to demonstrate this command, we need to have a file available to copy. Normally you would have files already in your account for one purpose or another, but for this test, let's simply create one.

% touch testfile
Don't worry too much about what touch does right now (read its man file if you're curious). For our purposes, it's sufficient to know that we just created a file named "testfile" in our current directory.
% ls -l
total 2
drwxr-xr-x 3 mstark user 512 Sep 12 14:50 temporary
-rw-r--r-- 1 mstark user 0 Sep 12 14:50 testfile
Now that we have a file that we can copy, let's do so.
% cp testfile otherfile
% ls -l
total 2
-rw-r--r-- 1 mstark user 0 Sep 12 14:56 otherfile
drwxr-xr-x 3 mstark user 512 Sep 12 14:50 temporary
-rw-r--r-- 1 mstark user 0 Sep 12 14:50 testfile
"Otherfile" is an exact copy of "testfile," except that it was created at a different time. We can also copy a file into another directory, like so:
% cp otherfile temporary
% ls -l temporary
total 2
-rw-r--r-- 1 mstark user 0 Sep 12 14:59 otherfile
drwxr-xr-x 2 mstark user 512 Sep 12 14:50 temporary2

mv (Move)

Let's say that you don't want to copy a file into a directory, you want to move it there. To move a file, you use the mv command. Let's move "testfile" into our "temporary" directory.

% mv testfile temporary
% ls -l
total 2
-rw-r--r-- 1 mstark user 0 Sep 12 14:56 otherfile
drwxr-xr-x 3 mstark user 512 Sep 12 15:01 temporary
% ls -l temporary
total 2
-rw-r--r-- 1 mstark user 0 Sep 12 14:59 otherfile
drwxr-xr-x 2 mstark user 512 Sep 12 14:50 temporary2
-rw-r--r-- 1 mstark user 0 Sep 12 14:50 testfile
"Testfile" is no longer in the directory we've been working in... it's now located in the "temporary" directory. Let's move it back to our working directory.
% mv temporary/testfile testfile
% ls -l
total 2
-rw-r--r-- 1 mstark user 0 Sep 12 14:56 otherfile
drwxr-xr-x 3 mstark user 512 Sep 12 15:02 temporary
-rw-r--r-- 1 mstark user 0 Sep 12 14:50 testfile
% ls -l temporary
total 2
-rw-r--r-- 1 mstark user 0 Sep 12 14:59 otherfile
drwxr-xr-x 2 mstark user 512 Sep 12 14:50 temporary2
So now we're back to where we started—"testfile" is back in our working directory, rather than in the "temporary" directory.

A nice thing to remember is that all the ../../ syntax we learned for cd will also work for both cp and mv, and just about any other command that navigates the Unix file system.

rm (Remove)

Of course, destruction is just about as much fun as creation, if not more, so let's learn how to delete files and directories. The command to delete a file is rm.

% ls -l
total 2
-rw-r--r-- 1 mstark user 0 Sep 12 14:56 otherfile
drwxr-xr-x 3 mstark user 512 Sep 12 15:02 temporary
-rw-r--r-- 1 mstark user 0 Sep 12 14:50 testfile
% rm testfile
% ls -l
total 2
-rw-r--r-- 1 mstark user 0 Sep 12 14:56 otherfile
drwxr-xr-x 3 mstark user 512 Sep 12 15:02 temporary
You can remove a file in another directory by simply making sure to give the full path to the file you want to delete.
% ls -l temporary
total 2
-rw-r--r-- 1 mstark user 0 Sep 12 14:59 otherfile
drwxr-xr-x 2 mstark user 512 Sep 12 14:50 temporary2
% rm temporary/otherfile
% ls -l temporary
total 2
drwxr-xr-x 2 mstark user 512 Sep 12 14:50 temporary2

rmdir (Remove Directory) and rm -r (Recursive Remove)

rmdir will delete an empty directory. Let's remove the "temporary2" directory within our "temporary" directory, after checking to make sure there's nothing in it.

% ls -l temporary/temporary2
total 0
% rmdir temporary/temporary2
% ls -l temporary
total 0
To delete a directory that isn't empty, however, you have to use a recursive flag on the rm command. To demonstrate this, let's copy our "otherfile" into the "temporary" directory (so that "temporary" is no longer empty) and then get rid of the directory.
% cp otherfile temporary
% ls -l temporary
total 0
-rw-r--r-- 1 mstark user 0 Sep 12 15:40 otherfile
% rm -r temporary
% ls -l
total 0
-rw-r--r-- 1 mstark user 0 Sep 12 14:56 otherfile
No more temporary directory, and all the files in it are gone. Be very, very careful with the -r flag for rm. Very, very careful.

cat (Concatenate)

Let's move on to looking at what's actually in files, rather than just moving them about or deleting them. cat, short for concatenate, displays files on the screen and allows you to combine files. cat is best used with small files... for a demonstration why, try this command on Dante or Homer:

% cat /etc/passwd
Bet you wish you hadn't done that one.

If you want to look at a large file like this, you're better off using more or less.

less and more (Browse Files)

less and more do basically the same thing. They allow you to display as much of a file as will fit on the screen, and then use the spacebar to scroll down. Seeing as less is more, however, the less command actually has all the functionality of more, as well as backwards scrolling, bookmarking capability, searching, and other useful features. Try using more on the password file:

% cat /etc/passwd | more

grep (Global Regular Expression Parser)

grep is one of the most useful commands for finding a pattern (or patterns) in a file (or files). With this tool, you can quickly find where important information is located in files, and, if used correctly, which files contain the information. The example below would find the string "washington" in all files in the current directory:

grep washington *

* ? [] (Wildcards)

Wildcard characters have special significance at the command line. Most of you should be familiar with the asterisk (*). It's used in Unix exactly as it's used in Windows. Here's the rundown:

  • * - an asterisk can replace any character(s) in a filename.
  • ? - a question mark can replace any single character in a filename.
  • [] - within the brackets you can specify a range of characters that can be replaced in a filename.
Here are some examples of wildcard usage, using our "otherfile" and some copies of it.
% cp otherfile otherfile2
% cp otherfile otherfile3
% cp otherfile otherfile.old
% ls
otherfile otherfile.old otherfile2 otherfile3
% ls *old
otherfile.old
% ls otherfile?
otherfile2 otherfile3
% ls otherfile[2-3]
otherfile2 otherfile3
% ls otherfile*
otherfile otherfile.old otherfile2 otherfile3
%