Chapter 5. Files and directories

Table of Contents
5.1. Introduction
5.2. The basics
5.3. Permissions
5.4. Mounting filesystems

5.1. Introduction

Unix-like operating systems use a hierarchical filesystem to store files and directories. Directories can contain files and other directories, the top directory (/) is named the root directory (not to be confused with the /root directory). Most filesystems also support file links (which provide alternative names for a file) and soft links. Other filesystems can be "connected" to an arbitrary directory. This process is named "mounting", and the directory in which the filesystem is mounted is named the "mount point".

This chapter covers the basic navigation of the filesystem, commands which are used to remove and create directories, filesystem permissions, links and mounting.

5.2. The basics

pwd

pwd(1) is a simple utility which shows the directory you are currently working in. The pwd does not require any parameters. This is an example output of pwd:


$ pwd
/home/danieldk

ls

ls is similar to the dir command in DOS and Windows. ls can be used to display files and directories located in specific directories. Running the ls command without any parameters shows the contents of the current directory:


$ ls
slackware-beginselen  slackware-beginselen-20december2002.tar.gz

Naturally it is also possible to show the contents of other directories. You can do this by specifying the path as a parameter tot the ls command:


$ ls /
bin   dev  home  lost+found  opt   root  tmp  var
boot  etc  lib   mnt         proc  sbin  usr

A disadvantage of the default output is that it provides little information about files and directories. For example, it is not possible to see whether some entry is a file or directory, what size a file is, or who the owner of the file is. The ls has the "-l" parameter to show more information:


$ ls -l
total 20
drwxr-xr-x    7 danieldk users        4096 Dec 21 09:24 slackware-beginselen
-rw-r--r--    1 danieldk users       14317 Dec 21 08:35 slackware-beginselen-20december2002.tar.gz

cd

Another important command is the cd command. It can be used to change the current working directory:


$ cd /home/danieldk/

With the pwd command you can see it worked:


$ pwd
/home/danieldk

mkdir

As you might have guessed, the mkdir(1) command can be used to create directories. For example:


$ pwd
/home/danieldk
$ mkdir test
$ cd test
$ pwd
/home/danieldk/test

It might happen that you want to create a directory in a parent directory which does not exist yet. For example, if you want to create the test2/hello/ directory, but the test2 directory does not yet exist. In this case you can make both directories with only one mkdir command:


$ mkdir -p test2/hello

rm

The rm(1) is used to remove files and directories. Let's look at a simple example:


$ rm hello.c

This commands removes the file hello.c. Sometimes the rm asks for a confirmation. You can ignore this with the '-f' parameter:


$ rm -f *

This command removes all files in the current directory without asking for confirmation. It is also possible to delete directories or even whole directory trees. rm provides the '-r' parameter to do this. Suppose we want to delete the ogle directory and all subdirectories and files in that directory, this can be done in the following way:


$ rm -r -f ogle/

Many commands allow you to combine parameters. The following example is equivalent to the last one:


$ rm -rf ogle/

5.3. Permissions

A short introduction

Every file in GNU/Linux has permissions. As you might have noticed, file permissions can be shown with the ls -l command:


$ ls -l logo.jpg
-rw-r--r--    1 danieldk users        9253 Dec 23 19:12 logo.jpg

The permissions are shown in the first column. Permissions that can be set are read(r), write(w) and execute(x). These permissions can we set for three classes: owner(u), group(g) and others(o). The permissions are visible in the second to ninth character in the first column. These nine characters are divided in three groups. The first three characters represent the permissions for the owner, the next three characters represent the permissions for the group, finally the last three characters represent the permissions for other users. Thus, the example file shown above can be written to by the owner and can be read by all three classes of users (owner, group and others).

Each GNU/Linux system has many distinct users (a list of users can be found in /etc/passwd), and a user can be a member of certain groups. This kind of user management provides makes it possible to manage detailed permissions for each file. In the example shown above danieldk is the owner of the file and group permissions apply to the group users. In this example groups rights do not differ from the rights of other users.

chown

The chown(1) command is used to set the file owner and to which group group permissions should apply to. Suppose we want to make danieldk the owner of the file logo2.jpg, this can be done with the chown:


$ chown danieldk logo2.jpg

Using the ls we can see that the owner is now danieldk:


$ ls -l logo2.jpg 
-rw-r--r--    1 root     root        9253 Dec 29 11:35 logo2.jpg
$ chown danieldk logo2.jpg
$ ls -l logo2.jpg 
-rw-r--r--    1 danieldk root        9253 Dec 29 11:35 logo2.jpg

But group permissions still apply for the root group. This group can be changed by adding a dot after the owner, followed by the name of the group (in this example the group is nedslackers):


$ chown danieldk.nedslackers logo2.jpg
$ ls -l logo2.jpg
-rw-r--r--    1 danieldk nedslackers        9253 Dec 29 11:35 logo2.jpg

It is also possible to change ownership recursively, this can be done with the recursive (-R) parameter:


$ chown -R danieldk.users oggs/

chmod

File permissions can be manipulated using the chmod(1) command. The most basic syntax of chmod is chmod [u,g,o][+/-][r,w,x] filename. The first parameter consists of the following elements: 1. which classes this manipulation permission applies to, 2. if the permissions should be added (+) or removed (-), and 3. which permissions should be manipulated. Suppose we want to make the file memo writable for the owner of the file and the groups for which the group permissions apply. This can be done with the following command:


$ chmod ug+w memo

As you can see below the memo is now writable for the file owner and group:


$ ls -l notities 
-r--r--r--    1 daniel   users          12 Mar  9 16:28 memo 
bash-2.05b$ chmod ug+w memo
bash-2.05b$ ls -l notities 
-rw-rw-r--    1 daniel   users          12 Mar  9 16:28 momo 

Just like the chown command it is also possible to do recursive (-R) operations. In the following example the secret/, including subdirectories and files in this directory, is made unreadable for the group set for this directory and other users:


$ chmod -R go-r geheim/

5.4. Mounting filesystems

5.4.1. Introduction

Like most Unices Linux uses a technique named "mounting" to access filesystems. Mounting means that a filesystem is connected to a directory in the root filesystem. One could for example mount a CD-ROM drive to the /mnt/cdrom directory. Linux supports many kinds of filesystems, like Ext2, Ext3, ReiserFS, JFS, XFS, ISO9660 (used for CD-ROMs), UDF (used on some DVDs) and DOS/Windows filesystems, like FAT, FAT32 and NTFS. These filesystems can reside on many kinds of media, for example hard drives, CD-ROMs and Flash drives. This section explains how filesystems can be mounted and unmounted.

5.4.2. mount

The mount(8) is used to mount filesystems. The basic syntax is: "mount /dev/devname /mountpoint". The device name can be any block device, like hard disks or CD-ROM drives. The mount point can be an arbitrary point in the root filesystem. Let's look at an example:


# mount /dev/cdrom /mnt/cdrom

This mounts the /dev/cdrom on the /mnt/cdrom mountpoint. The /dev/cdrom device name is normally a link to the real CD-ROM device name (for example, /dev/hdc). As you can see, the concept is actually very simple, it just takes some time to learn the device names ;). Sometimes it is necessary to specify which kind of filesystem you are trying to mount. The filesystem type can be specified by adding the "-t" parameter:


# mount -t vfat /dev/sda1 /mnt/usbflash

This mounts the vfat filesystem on /dev/sda1 to /mnt/usbflash.

5.4.3. umount

The umount(1) command is used to unmount filesystems. umount accepts two kinds of parameters, mount points or devices. For example:


# umount /mnt/cdrom
# umount /dev/sda1

The first command unmounts the filesystem that was mounted on /mnt/cdrom, the second commands unmounts the filesystem on /dev/sda1.