UNSW   Faculty of Engineering PRINT VERSION SITE MAP  
cse | School of Computer Science and Engineering (CRICOS Provider No. 00098G)
    #About CSE     #Undergraduate Study     #Postgraduate Study     #Timetables & Courses     #Research & Publications     #People & Work Units     #Help & Resources     #News & Events     #High School Portal

Last updated 19.02.08

Manipulating Files and Directories

Now that you have a few files and a directory in your home directory we will use these to explore some simple UNIX commands that you will grow to know and love.


\begin{commandlist} % latex2html id marker 1955\item[pwd] Print the name of... ...st be empty. \par usage: \unix{rmdir} \file{directory-names} \end{commandlist}

An example session to demonstrate these commands follows:

    % ls
    junk.txt     myfile.txt     session1.txt    stuff.txt

List all the files in the current directory, note that files beginning with a `.' are not listed. The -a option allows you to list, rather than hide, the entries starting with a `.' and the -l option allows you to use a long listing format, shown below.

    % ls -al
    total 20
    drwx------   2 cathy        4096 Jan 21 16:45 .
    drwx------  32 cathy        4096 Jan 21 16:39 ..
    -rw-------   1 cathy         120 Jan 21 16:43 junk.txt
    -rw-------   1 cathy         176 Jan 21 16:45 myfile.txt
    -rw-------   1 cathy           0 Jan 21 16:45 session1.txt
    -rw-------   1 cathy          75 Jan 21 16:40 stuff.txt

In this listing, all files are listed. The permissions of each file are listed along with information about the owner of the file, the size of the file and the date the file was last touched. Note that the listing for your account will probably show different files to those in this list.

    % mkdir temp
    % ls
    junk.txt   myfile.txt   session1.txt   stuff.txt   temp

Now you have a directory called temp.

    % ls -al
    total 24
    drwx------   3 cathy        4096 Jan 21 16:46 .
    drwx------  32 cathy        4096 Jan 21 16:39 ..
    -rw-------   1 cathy         120 Jan 21 16:43 junk.txt
    -rw-------   1 cathy         176 Jan 21 16:45 myfile.txt
    -rw-------   1 cathy           0 Jan 21 16:45 session1.txt
    -rw-------   1 cathy          75 Jan 21 16:40 stuff.txt
    drwx------   2 cathy        4096 Jan 21 16:46 temp

Notice the permissions on the temp directory. The first character is a `d', indicating that this is a directory. The next three characters make up the owner's permissions. In this case the owner of temp has read, write and execute permissions to the temp directory, but permissions to group and other users is denied.

The permissions on the file junk.txt indicate that it is a file (as the first character in the permissions list is blank). The owner of the file has read and write permissions to the file.

Change directory to the temp directory.

    % cd temp
    % ls -al
    total 8
    drwx------   2 cathy        4096 Jan 21 16:46 .
    drwx------   3 cathy        4096 Jan 21 16:46 ..

Notice that this directory is empty (not surprisingly!). The directories listed here are `.' which is the current directory, and `..' which is the parent directory.

Change directory back up to the parent directory `..'.

    % cd ..

Copy the file junk.txt to the temp directory.

    % cp junk.txt temp

Change directory to temp.

    % cd temp
    % ls -al
    total 12
    drwx------   2 cathy        4096 Jan 21 16:48 .
    drwx------   3 cathy        4096 Jan 21 16:46 ..
    -rw-------   1 cathy         120 Jan 21 16:48 junk.txt

The temp directory contains a copy of the file junk.txt.

Now copy the file stuff.txt from the parent directory to the current directory, which is still temp. Remember that `..' is the parent directory, and `.' is the current directory.

    % cp ../stuff.txt .
    % ls
    junk.txt        stuff.txt

The directory temp now also contains a copy of the file stuff.txt.

Use the move command to rename the file junk.txt to other.txt.

    % mv junk.txt other.txt
    % ls
    other.txt       stuff.txt

The directory temp has the file other.txt now.

Use the mv command to move the file other.txt up to the parent directory.

    % mv other.txt ../other.txt

Change directory up to the parent directory.

    % cd ..
    % ls
    junk.txt        other.txt       stuff.txt
    myfile.txt      session1.txt    temp

The parent directory now has the file other.txt.

Remove the file other.txt.

    % rm other.txt
    % ls
    junk.txt    myfile.txt    session1.txt    stuff.txt    temp

The file other.txt has been removed.

Attempt to remove the temp directory.

Note that you are not able to remove a directory that is not empty.

    % rmdir temp
    rmdir: temp: Directory not empty

Use the recursive option of the remove command to remove the entire contents of the directory temp, and then remove the directory. (Be very careful with this option!)

    % rm -rf temp

Change to the temp directory ...

    % cd temp
    Can't cd to temp: No such file or directory

...but as the directory no longer exists, you can't change to it.

    % ls
    junk.txt        myfile.txt      session1.txt    stuff.txt

The temp directory is no longer listed.

Make a directory called letters.

    % mkdir letters
    % ls -al
    total 24
    drwx------   3 cathy        4096 Jan 21 16:58 .
    drwx------  32 cathy        4096 Jan 21 16:39 ..
    -rw-------   1 cathy         120 Jan 21 16:43 junk.txt
    drwx------   2 cathy        4096 Jan 21 16:58 letters
    -rw-------   1 cathy         176 Jan 21 16:45 myfile.txt
    -rw-------   1 cathy           0 Jan 21 16:45 session1.txt
    -rw-------   1 cathy          75 Jan 21 16:40 stuff.txt

There is now an empty directory called letters.

Use the rmdir commands to remove the empty directory.

    % rmdir letters
    % ls -al
    total 20
    drwx------   2 cathy        4096 Jan 21 16:58 .
    drwx------  32 cathy        4096 Jan 21 16:39 ..
    -rw-------   1 cathy         120 Jan 21 16:43 junk.txt
    -rw-------   1 cathy         176 Jan 21 16:45 myfile.txt
    -rw-------   1 cathy           0 Jan 21 16:45 session1.txt
    -rw-------   1 cathy          75 Jan 21 16:40 stuff.txt

The letters directory is no longer listed.

Loc Van Huynh 2007-03-15
Top Of Page

 ###
Site maintained by webmistress@cse.unsw.edu.au
Please read the UNSW Copyright & Disclaimer Statement