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 tousername
'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
Important File Paths | |
---|---|
Path | Contents |
Absolute File Paths | |
/ | This is the root directory of the file system. |
~ | This is the absolute location of your home directory. |
~username | This is the absolute location of
username 's home directory. |
/home | This is the folder holding all user home directories. |
/bin | This is the folder holding all UNIX commands. |
~/public_html | This 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
Navigational Reference | ||
---|---|---|
Command | Option | Action |
cd |
| This 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. |
pwd |
| This prints your cwd. |
ls |
| This 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 --all | This 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
cp | ||
---|---|---|
Argument(s) | Last Argument | Action |
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
mv | ||
---|---|---|
Argument(s) | Last Argument | Action |
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
rm | ||
---|---|---|
Argument(s) | Last Argument | Action |
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.
Option | Argument(s) | Action |
---|---|---|
cp | ||
-i | This asks prior to clobbering any existing file. | |
-R | dir1 dir2 | This
copies the contents of dir1 into dir2 recursively.
|
-n | No existing file will be overwritten. | |
rm | ||
-i | This asks prior to clobbering any existing file. | |
-n | No existing file will be overwritten. | |
-r | dir | 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 | ||
-i | This asks prior to clobbering any existing file. | |
-n | No existing file will be overwritten. |
Who Sees What
UNIX gives you three rings of security.
u
You, the userg
Your groupo
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 2x
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.
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
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.
- 4 read
- 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 permissionchmod 644 filename(s)
user has read/write and others have read.chmod 755 filename(s)
user has all permissions, rest have read/execute