UNIX Files and Directories1

Jeffrey D. Oldham

1999 Dec 20

You can avoid learning many of UNIX file and directory commands by using a file manager such as xfm.

Files

In UNIX, almost all data is stored in files. C++ programs, executable programs such as netscape and emacs, and even directories are files. Here are some operations you can perform on files:

create files:
Using many programs, e.g., text editor or compiler, you can create files.

You can also duplicate a file using the cp copy command. For example, to create a new file called bar whose contents is exactly the same as the file foo, use the command

cp foo bar
That is,

cp existing-file new-file.

renaming files:
Use the mv move command to give a file a different name.

mv current-name new-name

listing files:
Use the ls list command to list a file.

ls file

To obtain more information about the file, use the -l option to learn about the file's permissions, owner, group, size, date, and name.

ls -l file

destroying files:
Use the rm remove command to destroy files.

rm existing-file

Removed files cannot usually be recreated. If you remove files and suddenly discover you did not mean to do so, stop all your work and run around until you find a UNIX guru who can help you recover what remains of your files before more of them permanently disappear.

Directories

A directory is a collection of files and possibly other directories.

Here are some operations you can perform on directories:

create a directory:
Use the mkdir make directory command to create a directory.

mkdir new-directory

list directory's contents:
Use the ls list command to list all the files (and directories) in a directory.

ls

You can specify which directory to show: ls directory-name.

remove a directory:
Use the rmdir make directory command to create a directory.

rmdir new-directory

Note only a directory with no files can be destroyed.

Directory Hierarchy

Since a directory can be stored inside another directory, there is a tree of directories and files. For example, the file creating this document may be located at /users/joldham/CS1320/files.ltx. The directory / contains the directory users, which contains the directory joldham, which contains the directory CS1320, which contains the file files.ltx.

One can move files and directories around the directory hierarchy using the mv command. For example, mv file directory moves the specified file into the specified directory.

Current Directory

So that we do not have to specify this lengthy string to use a file, the shell remembers the current directory. For example, when I start a shell, the current directory is /users/joldham. This is also my home directory, where all my files are placed unless I take explicit action to move them elsewhere. ~ is a nickname for my current directory.

Operations you can perform on the current directory include:

changing the current directory:
Use the cd change directory command to move your current directory to another location. For example, to go to Fred's home directory, I can use

cd ~fred

printing the current directory:
Use the pwd change directory command to list the current directory's location. For example, pwd may yield /users/fred.

The dirs, pushd, and popd commands permit one to to maintain a stack of directories. The current directory is the one on top of the stack, but it is easy to add, remove, and rearrange the directories. Read the documentation for these commands if you are interested.

Further Information

Many of these commands can take multiple arguments or arguments with regular expressions. Read the GNU WWW pages or the info pages for more information.

For more information, read the following:



Footnotes

... Directories1
©1999 Jeffrey D. Oldham . All rights reserved. This document may not be redistributed in any form without the express permission of the author.



1999-12-20