## Files

All of your data are stored in files. What is a file? It is a sequence of bytes. There are two primary types of files.

• directories These are known in common parlance as folders. They contain a list of links to their contents.
• regular files These come in two principal types, text files (human-readable) and binary files (not human-readable).

If we say file here, we mean a file or a directory. A path is a location in the file system. Paths can be specified two ways.

• absolute pathThese come in three types. The most common is that the path begins with a / and it is describing a file's location starting at the root directory. The path ~ is an absolute path to your home directory. The path ~username is an absolute path to username's home directory.

• relative pathThese are relative to your currrent working directory.

Even your screen is a file; its name is stdout and the keyboard is a file known as stdin

PathContents
Absolute File Paths
/This is the root directory of the file system.
~This is the absolute location of your home directory.
~usernameThis is the absolute location of username's home directory.
/homeThis is the folder holding all user home directories.
/binThis is the folder holding all UNIX commands.
~/public_htmlThis is the folder holding your website.
Relative File Paths
.This is your current working directory.
..This is the parent of your current working directory.
-This is your previous cwd.

## Navigating the file system

CommandOptionAction
cdThis is the change directory command. If given a path to a directory as an argument, it takes you to that directory If given no argument, it takes you to your home directory.
pwdThis prints your cwd.
lsThis is the "list stuff" command. If given no argument it lists the content of your cwd. If given a path to a directory, it lists the directory's contents. If given the path to a file, it lists the file. If given seveal arguments, it performs its task on each argument. This accepts wildcards as arguments.
-a --allThis lists all files including hidden files (files beginning with .).
-i This lists files with their inode (serial) number.
-l This lists files in long format, showing file metadata
-d This suppresses the listing of directory contents and instead lists only the directory's name.
-r This reverses the order of listings.
-R This lists contents of all directories, and their contents and the contents of their subdirectories...
-t This lists files in chronological order by last-modified date.

## Copying Files

Argument(s)Last ArgumentAction
one regular file donor name of nonexistenat file recipient A copy of donor is made and placed in file recipient
one regular file donor name of existing file recipient The file recipient> is truncated and A copy of donor is made and placed in file recipient. This can cause data loss.
Several regular files; a wildcard can be used. name of existing directory recipient The specified files are copied into recipient.

## Renaming and Moving Files

Argument(s)Last ArgumentAction
one regular file donor name of nonexistenat file recipient The file donor is renamed to recipient
one regular file donor name of existing file recipient The file recipient is truncated and the contents donor replace its contents. This can cause data loss.
Several regular files; a wildcard can be used. name of existing directory recipient The specified files are moved into recipient.

## Killing Files

Argument(s)Last ArgumentAction
One or more regular files; a wildcard can be used The specified files are unlinked from the file system and are only recoverable at big expense. Removal is for keeps here.
one regular file donor name of existing file recipient The file recipient is truncated and the contents donor replace its contents. This can cause data loss.
Several regular files and directories; a wildcard can be used. name of existing directory recipient The specified files are moved into recipient.

## Options

Below, dir* indicates directory and file indicates regular file.

OptionArgument(s)Action
cp
-iThis asks prior to clobbering any existing file.
-Rdir1 dir2This copies the contents of dir1 into dir2 recursively.
-nNo existing file will be overwritten.
rm
-iThis asks prior to clobbering any existing file.
-nNo existing file will be overwritten.
-rdir This removes the contents of dir. It is very dangerous. When in doubt use with -i so the shell asks as each item is removed.
mv
-iThis asks prior to clobbering any existing file.
-nNo existing file will be overwritten.

## Who Sees What

UNIX gives you three rings of security.

• u You, the user
• g Your group
• o Everyone else logged into the server

There are three types of permission that can be controlled.

• r read, which has an octal value of 4.
• w write, which has an octal value of 2
• x execute, which has an octal value of 1

For files you own, you have total control over these permissions. To see permissions on a file, use ls -l

\$ ls -l fileManagement.php
-rw-rw-r-- 1 morrison morrison 10821 Mar 23 15:15 fileManagement.php


The permission string for this file is -rw-rw-r--. The first character is usually a -, indicating the file is a regular file, or d, indicating the file is a directory.

The next three characters are permissions held by the user. Here we see read and write, but not execute. Next, we see the same permissions given to the group. Finally, others have only read permission.

If a directory has no execute permission for a ring, that ring cannot see through the directory.

If a directory has no read permission for a ring, that ring cannot see the directory's contents.

By giving a ring only execute permission, that ring can see through your directory but cannot read its contents. Such a diretory can contain a directory with read permissions and the contents of that directory can be seen.

## Changing Permissions with (u|g|o|a)[+-](r|w|x)

You can revoke permissions as follows. You can see file permissions using ls -l

chmod (u, g or o)-(r or w or x)


The "or"s are inclusive.

You change permissions using the chmod (change mode) command. Here are

• chmod ug-x filename(s) revoke execute permissions for user and group for the named file(s)
• chmod go-rw filename(s)  revoke read and write from group and others for the named file(s)
• chmod a-x filename(s) revoke execute from all for the named file(s)
• chmod -x filename(s) revoke execute from all for the named file(s)

You add permissions using + instead of - here. If a permission is presnet and you add it, there is no action; likewise if no permission exists and you revoke it, there is no action.

## Octal Permission System

This is a second way to manage permissions. It is very commonly used. The usage is

chmod abc filename(s)


Each of a, b and c are octal digits (0-7). The digit a descibes user permissions, b group permissions, and c others' permissions. Each permission has this numerical value.

• 2 write
• 1 execute
0 ---
1 --x
2 -w-
3 -wx
4 r--
5 r-x
6 rw-
7 rwx


Here are some examples

• chmod 000 filename(s) nobody has any permission
• chmod 644 filename(s) user has read/write and others have read.
• chmod 755 filename(s) user has all permissions, rest have read/execute