UNIX: Working with files
Listing files
The ls command lists all files in the current directory (except for files that start with ., use ls -a to list these too). The ls -l command lists files and some details about them.
Example
I am in a directory with three files in it: file1, file2, file3, and a directory d.
ls
d file1 file2 file3
ls -l
drwxr-xr-x 2 mary users 4096 2003-08-05 20:40 d -rw-r--r-- 1 mary users 0 2003-08-05 20:34 file1 -rw-r--r-- 1 mary users 1628 2003-08-05 20:37 file2 -rw-r--r-- 1 mary users 353 2003-08-05 20:37 file3
The first column of the ls -l output is the permissions (more on that shortly), the third column shows who owns the files (mary in this case), the fourth column the group that owns the files (users), the fifth column the file size, the sixth column the date that the file last changed, and the seventh column the file name.
Copying files
The cp command copies files. If we wanted to copy file1 to file4, we would use the command cp file1 file4. If we wanted to copy file1 into directory d, we would use the command cp file1 d
Moving files
The mv command moves files. If we wanted to move file1 to file4, we would use the command mv file1 file4. If we wanted to move file1 into directory d, we would use the command mv file1 d
Renaming files
Use the mv command to rename files.
Deleting files
Caution: Many UNIX machines have no 'undelete' function. If you delete files and do not have backups they will not be recoverable.
Use the rm command to remove a file. For example, to delete the file1 file, use the command rm file1.
To delete an entire directory and everything in it, permanently use the command rm -rf directory1.
File ownership
Unix is a multi-user system. At any time you have an identity while using the system (the name you log in as). To find out your identity, run id.
Unix also assigns ownership to files - files are owned by a user. If you create a file it will generally be owned by you, which means you control who else can read it, write to it and execute it.Files also have a group. You are a member of whichever groups you are assigned to by root, and one of these groups will be the file's group. You can control whether the group can read the file.
File permissions
If you execute the command ls -l you will see all files in the directory listed something like this:
Permissions User Group Size Date Name -rw-r--r-- 1 mary users 6876 Jul 21 11:15 index.html
The ten positions in the permissions column are divided like this:
- The first column (-) indicates things like whether the file is a link to another file, or a directory.
-
The next nine columns can be divided into three groups of three: rw-, r-- and r--.
The three groups have the following meanings:
- The first three (rw-)
- indicate user permissions, in this case for the user mary;
- the second three (r--)
- indicate group permissions, in this case for users in the group users; and
- the last three (r--)
- world (anyone) permissions — permissions for any other user on the machine.
The first column within any group of three indicates 'readable' — an r there will mean readable. The second column will contain a w if writable, and the third x if executable. (There are other, less commonly needed, permissions, see man chmod.)
So some examples:
-
drwxr-xr-x 2 mary users 1024 Sep 1 12:43 public_html
is a directory (d) that is readable, writable and executable by me (rwx, note that an executable directory is one you can change into, so you will normally want them to be executable), and readable and executable by anyone in the users group (r-x) and anyone else on the machine (r-x).
-
-rw-rw-r-- 1 mary users 6876 Jul 21 11:15 index.html
is a file (-) that is readable and writable by myself (rw-) and the users group (rw-), but only readable by anyone else on the machine (r--).
-
-rwx--x--x 1 mary users 6876 Jul 21 11:15 index.html
is a file (-) that I can read, write and execute (rwx), and that others on the machine, including those in the users group (--x--x) can't read or write but can run (you can't copy it if you can't read it).
Note that this isn't possible on some *N*X variants:, --x is the same thing as --- on Linux for example, something must be r-x before you can run it at all.
Directory permissions affect what can be done to files inside that directory - having read access means you can find out what files are in the directory and having write access means you can delete them, or put some there yourself. As noted above, execute permissions mean that you can change into the directory. Directories should therefore normally be executable at least by the user.
Symbolic link (l) permissions are always:
lrwxrwxrwx 1 mary users 6876 Jul 21 11:15 index.html -> ../index.html
but actually have the permissions of the file they link to.
Be careful with file permissions! Take especial care that files and
directories aren't
writable by anyone except yourself without good reason, and that files
whose content you don't want copied (ie assignments) aren't readable either.
Permissions should generally be:
-rw------- 1 mary users 6876 Jul 21 11:15 index.html
Changing file permissions
The command chmod changes file permissions.
Examples
- Letting all users on the system read a file named bob
- chmod a+r bob (which means (a)ll can have (r)ead access)
- Denying a group write access
- chmod g-w bob (which means deny (g)roup (w)rite permission)
- Giving the user and all other users execute permission
- chmod uo+x bob (which means (u)ser and w(o)rld can have e(x)ecute access)
Keep in mind these don't affect permissions that aren't explicitly mentioned, for example, a+r won't take write and exceute permissions away that already exist.
See man chmod for a complete list of permissions and codes.