UNIX File Management

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.

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.

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.

There are three types of permission that can be controlled.

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.

Important Note About Directories

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

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.

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

Here are some examples