Plain File Manipulation
The mv (move) Command
To move a file from one place to another, use the mv command. This has the effect of moving rather than copying the file, so you end up with only one file rather than two.
It can also be used to rename a file, by "moving" the file to the same directory, but giving it a different name.
mv file1 file2
This moves file1 to a file named file2. This is the Unix way of renaming a file.
mv file1 back/file1
This moves file1 to a file named file1 in the subdirectory back. This example could have been written mv file1 back/ as well, and the filename file1 would have been creaded in the back subdirectory.
After executing this command, there will no longer be a file1 file in the current directory.
The cp (copy) Command
cp file1 file2
This command which makes a copy of file1 in the current working directory and calls it file2
cp ABC.htm back/ABC.htm
This copies ABC.htm to a file named ABC.htm in the subdirectory back.
cp /vol/examples/tutorial/science.txt .
(Note: the dot "." at the end means the current directory in UNIX.)
The above command copies the file science.txt to the current directory, keeping the name the same.
The rm Command
rm file1
This removes (deletes) the file file1 from the current directory.
The pwd Command
pwd
This prints out your working directory where you are. When you give a filename to a command without specifying some other directory, the filename is looked up, manipulated, or created in your current working directory.
The mkdir Command
mkdir backdata
This makes a subdirectory of the current directory named backdata.
The cd Command
cd backdata
Changes to the backdata subdirectory.
cd
With no arguments, cd returns you to your home directory.
The rmdir Command
You can use the rmdir command to remove a directory (make sure it is empty first). Try to remove the backups directory. You will not be able to since UNIX will not let you remove a non-empty directory.
The ls (list) Command
When you first login, your current working directory is your home directory. Your home directory typically has the same name as your domain name.
To find out what is in your home directory, type
ls (short for list)
The ls command lists the contents of your current working directory.
There may be no files visible in your home directory, in which case, the UNIX prompt will be returned. Alternatively, there may already be some files inserted by the System Administrator when your account was created.
ls does not, in fact, cause all the files in your home directory to be listed, but only those ones whose name does not begin with a dot (.) Files beginning with a dot (.) are known as hidden files and usually contain important program configuration information. They are hidden because you should not change them unless you are very familiar with UNIX!!!
To list all files in your home directory including those whose names begin with a dot, type
ls -al
Related options are:
a - lists all files.
l - uses the long listing format.
R - lists subdirectories recursively
o - long listing without group info
The cat (concatenate) command
The command cat can be used to display the contents of a file on the screen. Type:
cat science.txt
The file lists until complete sometimes making it impossible to scroll to the beginning.
The less command
The command less writes the contents of a file onto the screen a page at a time. Type:
less science.txt
Press the space-bar if you want to see another page, type q if you want to quit reading. Less should be used in preference to cat for long files.
Account Management
The ln (Link) Command
Usage: ln [OPTION]... SOURCE DESTINATION
or: ln [OPTION]... SOURCE... DIRECTORY
Link SOURCE to DESTINATION (. by default), or multiple SOURCE(s) to DIRECTORY. Makes hard links by default, symbolic links with -s.
This command can create symbolic copies of files, merely linking to the original and not copying it. For example:
ls -al t?.htm
-rw-r--r-- 1 theearch theearch 1878 Jun 12 11:10 t1.htm
|
|
List the existing file
|
ln -s t1.htm t2.htm
|
|
t2.htm is created as a symbolic link
|
|
ls -al t?.htm
|
|
List the results
|
-rw-r--r-- 1 theearch theearch 1878 Jun 12 11:10 t1.htm
lrwxrwxrwx 1 theearch theearch 6 Jun 12 11:19 t2.htm -> t1.htm |
|
Note the l in the first column designating this a link
|
Type ln -s fred.html index.html to redirect all file accesses from index.html to the file fred.html.
The passwd Command
Type passwd to change your password.
The zip/unzip Command
Type zip to zip files and unzip to unzip files. This program is compatible with the zip program for DOS. For example:
zip myzip file1 file2 file3
This puts the files file1, file2, and file3 into a new zip archive called myzip.zip. On the other hand, if you had the archive myzip.zip and wanted to get back the files:
unzip myzip
Typing zip or unzip by itself will give you a usage
summary, showing nearly all the options availible.
The du Command
du -s (summarize)
This command shows how much disk space is used by a directory and everything below it.
You might find it useful to use the -c command and group the paths. Example:
du -sc . www/.
6725 .
24390 www/.
31115 total
The numbers reported are kilobytes.
File System Security
Basics
In your unix directory, type:
ls -l ( for long listing )
You will see that you now get lots of details about the contents of your directory, similar to the example below.
Each file (and directory) has associated access rights, which may be found by typing
Also, ls -lg gives additional information as to which group owns the file(beng95 in the following example):
-rwxrw-r-- 1 ee51ab beng95 2450 Sept29 11:52 file1
In the left-hand column is a 10 symbol ‘string’ consisting of the symbols d, r, w, x, -, and, occasionally, s or S. If d is present, it will be at the left hand end of the string, and indicates a directory: otherwise ‘-’ will be the starting symbol of the string.
The 9 remaining symbols indicate the permissions, or access rights, and are taken as three groups of 3.
- The left group of 3 gives the file permissions for the user that owns the file (or directory) (ee51ab in the above example);
- the middle group gives the permissions for the group of people to whom the file (or directory) belongs (eebeng95 in the above example);
- the rightmost group gives the permissions for all others.
The symbols r, w, etc., have slightly different meanings depending on whether they refer to a simple file or to a directory.
Access rights on files.
- r (or -), indicates read permission (or otherwise), that is, the presence or absence of permission to read and copy the file
- w (or -), indicates write permission (or otherwise), that is, the permission (or otherwise) to change a file
- x (or -), indicates execution permission (or otherwise), that is, the permission to execute a file, where appropriate
Access rights on directories.
- r allows users to list files in the directory;
- w means that users may delete files from the directory or move files into it;
- x means the right to access files in the directory. This implies that you may read files in the directory provided you have read permission on the individual files.
So, in order to read a file, you must have execute permission on the directory containing that file, and hence on any directory containing that directory as a subdirectory, and so on, up the tree.
Some examples
-rwxrwxrwx a file that everyone can read, write and execute (and delete).
-rw------- a file that only the owner can read and write - no-one else can read or write and no-one has execution rights (e.g. your mailbox file).
The chmod command (changing a file mode)
The chmod command changes the access rights on files and directories. Only the owner of a file can use chmod to change the permissions of a file. The options of chmod are as follows:
| Symbol | |
Meaning |
| u | |
user |
| g | |
group |
| o | |
other |
| a | |
all |
| r | |
read |
| w | |
write (and delete) |
| x | |
execute (and access directory) |
| + | |
add permission |
| - | |
take away permission |
For example, to remove read write and execute permissions on the file file1 for the group and others, type:
chmod go-rwx file1
This will leave the other permissions unaffected.
To give read and write permissions of file1 to all:
chmod a+rw file1
|