Working with Files

Note :- in this documentation, an example username "ug12abc" is given. You should use your own username in its place.

Like any other computer system, a file in Linux is just a named collection of data.  However the commands for working with files are somewhat different.  As with other computer systems files are organised within directories, and directories are nested within other directories etc.

Every user has a directory, their home directory, in which they can store their files.  This directory has the same name as the username, so a user called ug12abc would have a directory called ug12abc.  The location of this directory is fairly complicated to remember; its actually

/home/students/ug/ug12abc

All this means is that the directory ug12abc is inside a directory called ug, which is inside a directory called students, etc.

Whenever you need to refer to someone's directory then you can also use a shortcut like this:

~ug12abc

If it is your own directory you can just use ~

Basic File Operations

To practise working with files, we need a file to work with.  To do this we'll create a very simple file.  Don't worry too much about what we are doing here, it's just the simplest way to create a file to work with.  Type the following -

echo "This is a simple file" > test.txt

To check it worked, type ls. This is the Linux equivalent of dir in MS-DOS and will display the contents of the current directory.  You should see test.txt and maybe some other files.  To get more information from ls try typing ls -l.

You should see something like this

-rw-r--r--   1 ug12abc      ug          22 Jun 28 12:33 test.txt
1 2  3  4    5    6         7           8     9     10    11

The information looks quite confusing at first so here's what it all means.
 

  1. If there is a d in this space then the item is a directory
  2. Your permissions for the file - see section about permissions
  3. Group permissions for the file. A group is just a subset of the system's users who have separate permissions.  They are not used very often in the School.
  4. Everyone else's permissions for the file
  5. Number of links to the file.
  6. User who owns the file
  7. Group that the user belongs to
  8. File size in bytes
  9. Date file was last modified
  10. Time file was last modified
  11. Filename

File permissions

In the example of ls -l we saw that a file can have permissions.  The permissions determine who can do what to a file.  There are three groups of people that can have access permissions and there are three types of access that can be set.

Each file can have read access, write access and execute access (abbreviated to r,w and x) , in each of the three user-types - user(you), group, others. (abbreviated to u,g and o)

  • Read access determines if the file can be read, i.e. there contents can be viewed.
  • Write access determines whether the file can be changed.
  • Execute access determines whether a file can be run (if it is a program or a script)
  • User refers to the permissions that the owner of the file has with respect to the file.
  • In Linux you can be a member of a group so that you can share files.  The group section refers to the permissions that everyone in the group that the file belongs to has.
  • Other refers to everyone who is not a member of the group and is not the owner.

The three user-types are shown in the ls -l output as the three groups 2,3 & 4.  Here's a few examples of what the permissions mean.

-rwx------ This is a file which only the user can read, write and execute.
-rwxr-xr-x This is a file that everyone can read and execute, but only the owner can change.  This is the default setting for new files.  Don't change that default until you know what you're doing.
-rwxrw-r-- This file can be read and modified by the owner and anyone in the group, read by everyone, but only executed by the user.
-rwxrwxrwx This file can be read, modified, and executed by everyone with access to the system. (Very dangerous)

The meaning of file permissions for directories are different:
 

r If a directory has read permission then its contents can be listed with ls.
w If a directory has write permission then it means that a file can be created, modified or deleted in that directory
x If a directory has execute permission then files in that directory may be read from, written to and executed (if permissions allow), effectively this determines whether you can go in to a directory.

Changing file permissions

Now that we understand what the file permissions are, how do we change them?.  We use the command chmod (change mode).  There are several different ways to use it.  The simplest way is by using the symbols r,w,x and u,g,o and a, as in the following examples.


 
chmod u+x file1 Gives you execute access to file1
chmod a+r file1 Gives everyone (a = u+g+o) read access to file1
chmod go-rwx dir1 Makes it so that nobody else can access directory dir1

Most people in the school tend to leave all of their directories readable by everyone.  Obviously directories containing coursework should be protected to stop plagiarism.  Sometimes, however, lecturers will ask you to unprotect your directories so that they can look at your work.

More file operations

To practise using these commands and introduce some new ones, follow the instructions below.
 
cp test.txt secret.txt This makes a copy of test.txt called secret.txt
chmod go-rwx secret.txt This changes the permissions of secret.txt so that nobody else can read it.
mkdir secrets This command creates a new directory called secrets
chmod go-rw secrets chmod it so that it is secret
cp test.txt secrets Put a copy of test.txt in secrets
mv test.txt renamed.txt Rename the file test.txt to renamed.txt

If you now get a friend to do

cd ~ug12abc /secrets (cd means change directory)

and then type ls, then they will get a message that says "unreadable" because the he does not have r permission.  However, because we didn't remove the x permission from the secrets directory, and test.txt is not protected, then typing  more test.txt will still display the file.

In order to prevent this you need to do chmod go-x secrets.  Now other people will not even be able to get in to the directory.  After all of this we have got an unprotected file (renamed.txt) in your home directory, a protected file (secret.txt) in your home directory, and an unprotected file (test.txt) in a protected directory (that is, although unprotected itself it is protected by the directory it is in)

The cd command lets you move between directories as we have just seen.  You can move to any named directory or use directories called  .  and .. The first of these is the directory you are in and second refers to the directory that contains your current directory, the parent directory.  So cd .. is a very useful command.  Type cd ~  or just cd to take you back to your home directory.

Deleting

If you want to remove a file then you use the command rm.  For example to remove the file renamed.txt

rm renamed.txt

To delete a directory add -r to the rm command so that it deletes any subdirectories.  You may need to confirm the deletion.

rm -r secrets

Quotas

You have a limited amount of space to store all or your files, this limit is called your quota.  The standard quota is 30 megabytes.  You have a soft limit and a hard limit.  The system will not let you exceed your hard limit.  If you exceed your soft limit a 7 day countdown will start after which you will not be able to login and you will have to see the system administrators.  To check your quota type

quota -v