Follow Techotopia on Twitter

On-line Guides
All Guides
eBook Store
iOS / Android
Linux for Beginners
Office Productivity
Linux Installation
Linux Security
Linux Utilities
Linux Virtualization
Linux Kernel
System/Network Admin
Scripting Languages
Development Tools
Web Development
GUI Toolkits/Desktop
Mail Systems
Eclipse Documentation

How To Guides
General System Admin
Linux Security
Linux Filesystems
Web Servers
Graphics & Desktop
PC Hardware
Problem Solutions
Privacy Policy





Files and Directories

Files are a facility for storing and organizing information, analogous to paper documents. They're organized into directories, which are called folders on some other systems. Let's look at the organization of files on a Debian system:

A simple / represents the root directory. All other files and directories are contained in the root directory. If you are coming from the DOS/Windows world, / is very similar to what C:is for DOS, that is the root of the filesystem. A notable difference between DOS and Linux however, is that DOS keeps several filesystems: C: (first hard disk), A: (first floppy disk), and D: (either CD-ROM or second hard disk), whereas Linux has all its files organized above the same / root.
This is the home directory of user ``janeq.'' Reading left to right, to get to this directory you start in the root directory, enter directory home, and then enter directory janeq.
This is the configuration file for the X Window system. It resides in the X11 subdirectory of the /etc directory. /etc is in turn a subdirectory of the root directory, /.
Things to note:

  • Filenames are case-sensitive. That is, MYFILE and MyFile are different files.
  • The root directory is referred to as simply /. Don't confuse this ``root'' with the root user, the user on your system with ``super powers.''
  • Every directory has a name, which can contain any letters or symbols except /. The root directory is an exception; its name is / (pronounced ``slash'' or ``the root directory''), and it cannot be renamed.
  • While you can use almost any letters or symbols in a filename, in practice it's a bad idea. It is better to avoid characters that often have special meanings on the command line, including: { } ( ) [ ] ' ` " \/ > < | ; ! # & ^ * %
  • Also avoid putting spaces in filenames. If you want to separate words in a name, good choices are the period, hyphen, and underscore. You could also capitalize each word, LikeThis.
  • Each file or directory is designated by a fully-qualified filename, absolute filename, or path, giving the sequence of directories which must be passed through to reach it. The three terms are synonymous. All absolute filenames begin with the / directory, and there's a / before each directory or file in the filename. The first / is the name of a directory, but the others are simply separators to distinguish the parts of the filename.
  • The words used here can be confusing. Take the following example: /usr/share/keytables/ This is a fully-qualified filename; some people call it a path. However, people will also refer to alone as a filename.
  • There is also another use for the word ``path.'' The intended meaning is usually clear from the context.
  • Directories are arranged in a tree structure. All absolute filenames start with the root directory. The root directory has a number of branches, such as /etc and /usr. These subdirectories in turn branch into still more subdirectories, such as /etc/init.d and /usr/local. The whole thing together is called the ``directory tree.''
  • You can think of an absolute filename as a route from the base of the tree (/) to the end of some branch (a file). You'll also hear people talk about the directory tree as if it were a family tree: Thus subdirectories have ``parent,'' and a path shows the complete ancestry of a file.
  • There are also relative paths that begin somewhere other than the root directory. More on this later.
  • No directory corresponds to a physical device, such as your hard disk. This differs from DOS and Windows, in which all paths begin with a device name such as C:\. The directory tree is meant to be an abstraction of the physical hardware, so you can use the system without knowing what the hardware is. All your files could be on one disk - or you could have 20 disks, some of them connected to a different computer elsewhere on the network. You can't tell just by looking at the directory tree, and nearly all commands work just the same way no matter what physical device(s) your files are really on.
Don't worry if all this isn't completely clear yet. There are many examples to come.

Using Files: A Tutorial

To use your system, you'll have to know how to create, move, rename, and delete files and directories. This section describes how to do so with the standard Debian commands.

The best way to learn is to try things. As long as you aren't root (and haven't yet created any important personal files), you cannot mess up too seriously. Jump in - type each of these commands at the prompt and press Enter.

One directory is always considered the current working directory for the shell you're using. You can view this directory with the pwd command, which stands for Print Working Directory. pwd prints the name of the directory you're working in - probably /home/yourname.

ls stands for ``list,'' as in ``list files.'' When you type ls, the system displays a list of all the files in your current working directory. If you've just installed Debian, your home directory may well be empty. If your working directory is empty, ls produces no output, because there are no files to list.

cd /
cd means ``change directory.'' In this case, you've asked to change to the root directory.

This verifies that you're working in the root directory.

Lets you see what's in /.

Typing cd with no arguments selects your home directory - /home/ yourname - as the current working directory. Try pwd to verify this.

Before continuing, you should know that there are actually two different kinds of filenames. Some of them begin with /, the root directory, such as /etc/profile. These are called absolute filenames because they refer to the same file no matter what your current directory is. The other kind of filename is relative.

Two directory names are used only in relative filenames: . and ... The directory . refers to the current directory, and .. is the parent directory. These are ``shortcut'' directories. They exist in every directory. Even the root directory has a parent directory - it's its own parent!

So filenames that include . or .. are relative, because their meaning depends on the current directory. If I'm in /usr/bin and type ../etc, I'm referring to /usr/etc. If I'm in /var and type ../etc, I'm referring to /etc. Note that a filename without the root directory at the front implicitly has ./ at the front. So you can type local/bin, or ./local/bin and it means the same thing.

A final handy tip: The tilde ~ is equivalent to your home directory. So typing cd ~ is the same as typing cd with no arguments. Also, you can type things like cd ~/practice/mysubdirectory to change to the directory /home/yourname/practice/mysubdirectory. In a similar way, ~myuser is equivalent to the home directory of the user ``myuser,'' which is probably something like /home/myuser; so ~myuser/docs/ is equivalent to /home/myuser/doc/

Here are some more file commands to try out, now that you know about relative filenames. cd to your home directory before you begin.

mkdir practice
In your home directory, make a directory called practice. You'll use this directory to try out some other commands. You might type ls to verify that your new directory exists.

cd practice
Changes the directory to practice.

mkdir mysubdirectory
Creates a subdirectory of practice.

cp /etc/profile .
cp is short for ``copy.'' /etc/profile is just a random file on your system, don't worry about what it is for now. We've copied it to . (recall that . just means ``the directory I'm in now,'' or the current working directory). So this creates a copy of /etc/profile and puts it in your practice directory. Try typing ls to verify that there's indeed a file called profile in your working directory, alongside the new mysubdirectory.

more profile
This lets you view the contents of the file profile. more is used to view the contents of text files. It's called more because it shows one screenful of the file at a time, and you press the space bar to see more. more will exit when you get to the end of the file, or when you press q (quit).

more /etc/profile
Verifies that the original looks just like the copy you made.

mv profile mysubdirectory
mv stands for ``move.'' You've moved the file profile from the current directory into the subdirectory you created earlier.

Verifies that profile is no longer in the current directory.

ls mysubdirectory
Verifies that profile has moved to mysubdirectory.

cd mysubdirectory
Changes to the subdirectory.

mv profile myprofile
Note that unlike some operating systems, there is no difference between moving a file and renaming it. Thus there's no separate rename command. Note that the second argument to mv can be a directory to move the file or directory into, or it can be a new filename. cp works the same way.

As usual, you can type ls to see the result of mv.

mv myprofile ..
Just as . means ``the directory I'm in now,'' .. means ``parent of the current directory,'' in this case the practice directory you created earlier. Use ls to verify that that's where myprofile is now.

cd ..
Changes directories to the parent directory - in this case practice, where you just put myprofile.

rm myprofile
rm means ``remove,'' so this deletes myprofile. Be careful! Deleting a file on a GNU/Linux system is permanent - there is no undelete. If you rm it, it's gone, forever. Be careful! To repeat, deleting a file on a GNU/Linux system is permanent - there is no undelete. If you rm it, it's gone, forever.

rmdir mysubdirectory
rmdir is just like rm, only it's for directories. Notice that rmdir only works on empty directories. If the directory contains files, you must delete those files first, or alternatively you can use rm -r in place of rmdir.

cd ..
This moves out of the current directory, and into its parent directory. Now you can type the following:

rmdir practice
This will delete the last remnants of your practice session.

So now you know how to create, copy, move, rename, and delete files and directories. You also learned some shortcuts, like typing simply cd to jump to your home directory, and how . and .. refer to the current directory and its parent, respectively. You should also remember the concept of the root directory, or /, and the alias ~ for your home directory.

Dot Files and ls -a

When you type ls, files beginning with a dot are not listed. Traditionally, files that contain configuration information, user preferences, and so on begin with a dot; these are hidden and out of your way while you do your day-to-day work. Sample dot files are ~/.emacs, ~/.newsrc, ~/.bashrc, ~/.xsession, and ~/.fvwmrc. These are used by Emacs, news readers, the Bash shell, the X Window system, and the fvwm window manager, respectively. It is conventional to end the dot filename with rc, but some programs don't. There are also directories beginning with a dot, such as ~/.gimp and ~/.netscape, which store preferences for the Gimp and Netscape.

Sometimes a program will create a dot file automatically; for example, Netscape allows you to edit your preferences with a graphical dialog box and then it saves your choices. Other times you will create them yourself using a text editor; this is the traditional way to do it, but you have to learn the peculiar format of each file - inconvenient at first, but it can give you a lot of power.

To see dot files, you must use the -a option to ls. The long form of -a is -all, if you find that easier to remember. You can also use -A or -almost-all, which displays all dot files except . and ... Remember that . is the current directory, and .. is the parent of the current directory; because these are guaranteed to be in every directory, there is no real reason to list them with ls. You already know they are there.

John Goerzen / Ossama Othman

  Published under the terms of the GNU General Public License Design by Interspire