Previous Up Next

4  File Management

One of the more important topics for the use of your Unix account is file management, the understanding of how to manipulate the various files you have in your account. Since you will largely be connecting to cent1 via a text only console you will need to understand how all the typed commands available affect your files.

First off its worth noting a couple of points about files under Unix. Most importantly they are all stored in a case sensitive way. This means the file “foo” is not the same as “FOO” or the file “fOo”. For this reason clear naming is important. The second is that file extensions do NOT have to relate to what the file is for. Whereas on a windows box you will find for example MS word documents ending in .doc, MS Excel spreadsheets ending in .xls and executable programs ending in .exe on a Unix system this is not the case. Any file may be called any thing, and any program may read any file happily. While most programs will use a file extension they are just part of the file name and the Operating System doesn’t treat them as instructions on how to treat that file (this is a good thing for security).

Files are generally assumed to contain one of two things, either binary data (e.g. a program designed to be executed) or textual data (often formatted in some specific way). If you’re not sure what a file contains then its best to use a very simple command that can tell you:

% file filename

The “file” program will tell you what type of file the file called “filename” is. It does this by examining the start of it, checking for a file extension (they’re optional, but often used) and a few other tricks. file will tell you if something is a file or directory, and if its a file will probably tell you what type, and thus what program, should be used to open it.

4.1  Configuration files

Another convention which it helps to know about for files is the dot-convention. Any file or directory on a Unix system that starts with a dot/period/fullstop e.g. “.vimrc” is not shown during normal browsing. These files are generally assumed to be configuration files for your programs, and so not something you want to see cluttering up your view all the time. However these files can be accessed just like normal, they aren’t considered “hidden” for security reasons, just out of your view for convenience.

4.2  Basic Commands

There are a handful of basic commands that can be used to manipulate your files on the system, and I’ll now go through them, with examples.

4.2.1  Your current directory

The first thing to know how to do is to see where you “are” in the directory structure. Your shell will always be essentially “looking” at one directory in the structure at a time, this is known as the “current directory”. So the first thing to do is type:

% pwd

“pwd” (Print Working Directory) will simply print out the current directory you are in, usually if you’ve just logged onto cent1 this will be your home directory.

4.2.2  Finding out what files there are

Now that you know where you are you probably want to know what files are there. This is fairly easy to achieve:

% ls

ls (LiSt files) is used to get a listing of all the files that are currently in the directory you’re in. Well almost all the files, all the dot-files (see above) will not be shown by a regular ls. To see all the files, including these dot-files you need the following command:

% ls -a

Other useful features of ls include the -F flag. This is used to help differentiate many common types of file. So:

% ls -F

Shows all files a normal ls would. Except directories have “/” appended to the end, executable files have a “*” at the end and symbolic links (see section 12.3) have a “@” at the end.

These two flags can be combined together to show this behaviour for all the dot-files, simply by saying either “ls -a -F” or “ls -aF”, both are valid.

4.2.3  Viewing a file

Now that you can see what files are in your current directory you might well what to know what they contain. All files can be viewed by any program, but probably the easiest and safest program for viewing them is “less”. Less is a type of program called a “pager” and is designed to view files which have more lines than your terminal is tall. Its use is fairly simple:

% less filename

Here less will open the file called “filename” for viewing. less is probably what was used when you were viewing man pages (see section 3.1) and so its use should be the same. Simply press the space bar for the next page, and “b” to go back up a page, once you’re done pressing “q” will quit. less can do a lot more than many people think, including searching for text, which can be achieved by pressing “/” and typing the word you want to search for, then pressing return. To see a full description of how to use less see “man less” or “less --help” (dash dash help).

4.2.4  Copying a file

You can make a copy of any file you have permission to read (see section 4.8). This simply creates a whole separate filename with exactly the same contents as the previous one and is often used to make backups of files. For this you will use the cp command:

% cp original target

cp (CoPy) will simply copy “original” to “target”, if there is already another file called “target” it will overwrite it, so be careful. If you want to try and prevent that from happening use:

% cp -i original target

With the addition of the -i flag cp will prompt you with a yes/no prompt if “target” already exists, so you can be sure you’re not overwriting a file you might want.

More details with how this relates to directories is shown in section 4.3.4. Both the “original” and “target” names conform to all the rules laid out in section 4.5, so you can read that if you want more information on clever things that can be done with them beyond basic copying.

4.2.5  Moving or Renaming a file

Where copying is to make a duplicate of a file moving is much simpler. The move file command simply moves the file from one location and name to another. This means that the move command can also be used to rename a file, as you simply move it from one name to another. It can be used as follows:

% mv original target

mv (MoVe) will move the file called “original” to the file called “target”, simple as that, all its permissions will be the same, and even the date it was created will be the same, only the name (and possibly location) will change. mv also supports the -i flag just like cp does, which again helps if you want to make sure you won’t be overwriting a file that already exists. So for example you could type:

% mv foo bar

This would move the file “foo” to become a file called “bar”. More on using move in relation to directories is listed in section 4.3.4, and more details about how paths work are in section 4.5.

4.2.6  Deleting a file

Sooner or later you’ll find a file that you don’t need anymore, and in the interests of saving disk space you’ll want to delete it. The following command does just that:

% rm filename

rm (ReMove) will simply delete the file (in the example called “filename”) from the system. After this command has been run you will not be able to get the file back, there is no “recycle bin” for your shell on cent1, so always be sure before you use rm.

rm also accepts a -i flag, but it uses it slightly differently. If you type:

% rm -i filename

You will be asked if you are sure you want to delete the file “filename” from the system, this is often a good idea to ensure you haven’t typed the wrong filename, but if you’re deleting a lot of files can get annoying.

4.2.7  Finding out how big a file is

The files in your directories will grow and shrink depending on how much information you fill them with, however the easiest way to find out how large a file is is to use the du command:

% du -k filename

du (Disk Usage) will display how much space the file “filename” consumes on the disk, normally it uses a unit of 512 bytes, but since nobody else does then the use of the -k flag will print out the result in terms of KiloBytes (Kb) which is a fairly normal amount used elsewhere.

If you want to know how large a directory is including all the files inside it it’s best to use the following flags:

% du -ks directoryname

This will display the size of the directory “directoryname”, and all the files in it, and in any subdirectory of it to you in Kb, this is quite helpful for determining how much space you’re really using.

4.2.8  Concatenating files

Sometimes you will have two or three files full of text that are all related, and perhaps would be better all in one file. Instead of a lot of tedious editing (see section 5) you can simply use the concatenate command. In its most basic form you simply type:

% cat foo

This will just output the file “foo” to your terminal in a rush, which isn’t very helpful at the minute, however if you type:

% cat foo bar

Then it will output the contents of file “foo” followed by the contents of file “bar”. After this you just need a way of doing something with the output.

% cat foo bar > baz

Now cat will output all of “foo” then all of “bar”, but instead of sending the contents of both files to your terminal it will send them to the file “baz” overwriting it with the contents of foo then bar. Although this example only shows two files you can add as many as you want to the cat command, as long as you end it with “> target”. For example “cat foo bar baz gzork > quux” will output all of the files which are its first four arguments (foo, bar, baz and gzork) to the file quux, one after another. It is also worth noting that the original files are left intact.

4.3  Directories

Up to this point a lot of discussion has taken place which has mentioned directories, however no indication of how to use them has been given. There are really only a couple of things you need to know about directories to start using them.

4.3.1  Making new directories

To make a new directory inside the current one, you can simply use the following command:

% mkdir foo

This will make a new directory called “foo” in the current directory. Your directories can be called anything you want, and common directories to find in home directories include “bin” (for personal programs), “work” (for documents you’ve written), “src” (for source code you’ve written), “Mail” (for all your stored email). As stated above you don’t have to obey a structure in your home directory, and can do what you want.

4.3.2  Removing Directories

If you want to remove a directory you can simply use the “rm” command from earlier. However it will need an argument in order to work:

% rm -r dn

This will use the rm command recursively, that is it will enter the directory (called “dn” in the example, but whatever your directory is called should go here) and delete all the files in it, then move to every subdirectory and do the same before finally removing the directories and last removing the directory in question. Use of “rm -r” will remove the entire directory and all its contents, so be careful with it. As before you can do “rm -ri” to be asked about each file in turn before it’s deleted from the system.

4.3.3  Changing directory

Changing directory is done by use of the “cd” command (Change Directory). If you give this a target directory you will go to that directory. For example, if you are in your home directory, and it contains a directory called “new” then:

% cd new

Will move you to the new directory (and pwd will prove that point). Moving back up the directory structure uses a neat feature of the directory structure.

If you move to a directory, even a totally freshly created one, and type ls you will see that it contains two files ‘..’ and ‘.’ - these two files have interesting properties.

The ‘.’ file is the same as the current directory, it literally points to it, so “./filename” points to the file called “filename” in the current directory (but then again, so does just “filename”). This is useful for some things to do with your path, which will be shown later (see section 4.5).

However the ‘..’ file is a pointer to the previous directory, so if you change to the directory ‘..’ then you move back up the directory structure by one directory, for example:

% cd ..

This will take you back to the previous directory, however the previous directory also contains a ‘..’ entry, so you can do:

% cd ../../

You will jump backwards two directories.

One final and important note about the use of cd is that if you just type cd on its own with no arguments at all then you will be returned to your home directory. This means if you are deep in a directory tree you can just type cd, jump back home and move off somewhere else.

4.3.4  Copying and moving with relation to directories

One of the most used copying commands is to copy a file from where it is currently into a subdirectory. This may be to make a backup of it, or to organise your files better, or even to copy it to another users space so they can edit that file. A simple example of this would be:

% cp output backup/

This example presumes you have a directory called “backup” and a file called “output”. it will copy the file output into the directory backup, but keep the name the same. If you wanted to copy it into the subdirectory and change the name you could use:

% cp output backup/old-output

This would copy the file “output” into the directory “backup” and rename it, so a copy of the file would exist called “old-output” in the subdirectory “backup”.

If you attempt to copy a file into a directory that has a subdirectory of that name then cp will print the error “cp: cannot overwrite directory ‘name’ with non-directory” (where “name” is the name of the file). However if you copy a file into a directory with the same name it will overwrite it.

Another likely task is to copy a file from a subdirectory into the current directory, often this is done so you can process the file without risking changing the original. An example of this would be:

% cp data/test1 ./

This will use the cp command to copy the file “test1” from the subdirectory called “data” into the current directory (here called “./” for reasons that will become clear in section 4.5). Assuming the directory and file existed after doing this a copy of the file “test1” would be in your current directory, and changing it would not affect the original.

Copying a file from the parent directory into the current directory is another thing which at first might look like it has a confusing syntax but actually makes sense. An example would be:

% cp ../new-foo ./foo

This would copy the file “new-foo” from the parent of your current directory to the current directory you are in (which would be a subdirectory of the parent) and rename it “foo”. The reasons for this syntax are explained in sections 4.3.3 and 4.5. But to reiterate, each directory has a file called “..” (dot dot) which is the parent directory, and a file called “.” (dot) which is the current directory, so in essence the cp program just follows the two paths logically, starting by hopping up to the previous directory “../” and getting the file “new-foo”, then going back to the current directory “./” and copying that file to the new name, “foo”.

You could just write that command like:

% cp ../new-foo foo

But as you can see its clearer to type “./foo” instead of “foo” which tends to almost run into the origin path visually and thus makes spotting typos harder. Largely this is a style issue, you may prefer not typing “./” or you may find it adds clarity, its up to you.

Lastly you’ll sometimes want to copy from one subdirectory to another, without actually going into one of those directories. This could be achieved with a command like:

% cp data/test1 backup/

This copies the file “test1” from the subdirectory “data” into the subdirectory “backup”, where since a name is not specified it simply calls it the same thing. So this will enter the data subdirectory, copy the file “test1”, then enter the subdirectory “backup” and write out a copy also called “test1”. Of course this relies on there being those two directories and that file in existence, if this isn’t the situation then cp will spit out an error.

Sometimes you will want to make a copy of a whole directory, for backup purposes or to later change a few things about that directory. If you want to copy an entire directory then you need to know about the ‘-r’ recursive flag for cp, as shown in this example:

% cp -r Mail Mail-backup

What this does is to recursively copy all the files and subdirectories of the “Mail” directory (one of the more common places to store old and important email) to a directory called “Mail-backup”. The new Mail-backup directory will be an exact duplicate of the Mail directory, at the point that the command was run, all subdirectories and all files within will be copied.

Similarly to the above examples some short examples for moving files in relation to directories will now be given, you will see that mv and cp work with their origin and target arguments in a very similar way.

Similarly one of the most common things you’ll want to do is move a file into a subdirectory. This is commonly used as a trick for organising your files. An example of this is:

% mv rant writing/

This will move the file “rant” into the subdirectory “writing”, thus filing it away neatly. After running this command the file will be gone from your current directory and will instead exist as a file of the same name in the subdirectory “writing”.

Another useful task is to move a file from a subdirectory into the current directory, this can be done with a command like the following example:

% mv data/report ./

This will move the file “report” from the directory “data” and place it into the current directory (as shown by the “./” notation, again please see 4.5 for an explanation of how this works).

For similar reasons as to why you’d want to copy a file from one subdirectory to another you’ll sometimes want to move a file from one subdirectory to another. This can be done with similar syntax:

% mv data/test1 backup/

This will move the file “test1” from the subdirectory “data” to the subdirectory “backup”, after this command is run (assuming all the files and directories exist) you’ll find the file gone from the “data” directory and that it now appears in the “backup” directory.

You might want to move an entire directory, either to make space in the file-system for something else, or to rename it. This can be done by the following command:

% mv data old-data

This will move the directory called “data” to a directory called “old-data” essentially renaming it, all its contents will still be intact, including any files or subdirectories that it contained. Of course this command would also work on files called “data” and “old-data”, if you used a command like this it wouldn’t however:

% mv data/ old-data/

Although it would still work for directories, again it is a personal style issue.

Lastly you might want to move a directory into another directory, often for organising your home directory. This can be achieved by a command like:

% mv data backup/

The difference between this command and the one above is that the backup directory exists, and so instead of printing an error message it places the “data” directory inside the “backup” directory. So now “data” is gone from the current directory but is a subdirectory of “backup”.

4.4  Finding files

Before too long your home directory will end up full of several directories, all with several subdirectories, and all packed with files. At this point remember exactly where you put each file gets to be a bit of a problem. This is why the find command was created, it’s more mundane uses aren’t so complex, so for example:

% find . -name 'foo'

Will start at the current directory (‘.’ remember), and search for all files in that directory, or a subdirectory called “foo”, and will print out the path to each of them.

However this isn’t very useful if you can’t remember the exact filename, so to do this you can use various characters to affect the search. For example:

% find . -name 'foo*'

This will find all files called “foo”, or that start with “foo”, the * matches any number of any characters, so this would find files called “foo”, “foobar”, “foo-the-file-you-want”, “foooooooOOO”, etc. Similarly if you use:

% find . -name '*foo'

It will find all the files that end in “foo”. This is especially useful with commands such as find . -name '*.doc' which will find all files that end in .doc, i.e. all the word documents in your directory.

The full documentation for find is stored in man find and is worth reading carefully. Find can be used to find files in a number of interesting ways, and if you want you can run a command against all the files you find, but that kind of thing is beyond this document.

4.5  Paths, relative and absolute

Most the above examples have dealt only with files in the current working directory. However files in other directories can be accessed and affected if you know their locations. There are two ways of doing this, absolute paths, and relative paths, and either one can be used to point any command at the files you want it to affect, this includes ls, cp, mv, rm, grep, whatever you have your $EDITOR set too or indeed anything else you can think of or write.

4.5.1  Absolute paths

Absolute paths are the easiest to describe, you simply start from the root of the file system (called ‘/’ which is always the left most symbol of any full path) and give all the directories between that and where the file is stored. An example of this would include where the ls program is stored, the full path to this is: “/usr/bin/ls” i.e. the directory called “usr” in the root directory, the subdirectory “bin” and finally the file “ls”.

If you wanted to use an absolute path you could for example copy a file from one user’s home-directory to yours (assuming you had permission). This example presumes the other user’s username was “foo” and their home directory was in the “/home/cent1/20/” directory.

However to use full paths you also need to know the full path to your own home directory, assuming your username was “bar” and your home directory was in the “/home/cent1/15” directory then the command would be as follows:

% cp /home/cent1/20/foo/the-file /home/cent1/15/bar/

Which goes right to the root directory / and goes forwards until it hits the file you want “the-file”, then for the target directory goes back to the root and forwards into your home-directory where it places a copy of “the-file”.

Whenever you’re trying to interpret full paths simply start at the left-hand side and read across to the right, that will tell you every directory and subdirectory, divided by the ‘/’ (forward slash) symbol. The last element will either be a file or a directory, but regardless of what it is it will be affected by the command.

4.5.2  Relative paths

Remembering and typing all those long paths tends however to lead to mistakes and cursing, which is why relative paths are much easier to use for most things. These can be thought of as simply all the steps needed to get to the directory you want, from the current one. A classic example is to copy something from a subdirectory to the current one:

% cp sub/stuff ./

This will copy the file “stuff” from the directory called “sub” into the current directory, which can be shown by ./ (see section 4.3.3 for reasons).

Another common task is to copy something from the directory above the current one in the directory tree into the current one, to do this you would use something like:

% cp ../stuff ./

Which tracks back to the previous directory (also known as ‘..’) and reads the file called “stuff”, depositing a copy into the current directory (again shown by ‘./’).

You can also nest these lookups, so that the command:

% cp ../../../../stuff ./

Will interpret the path “../../../../stuff” from left to right, starting off by going up a directory (“../”) then going to the “../” entry of that directory, which is of course the parent. Overall this command tracks up four directory levels, then copies the file “stuff” to the current directory (“./”).

So the basic principles of relative paths is that you have to know your current working directory then start reading them from the left to right. Remember that each directory includes two entries automatically, “../” is a link to the parent and “./” is a link to that directory itself. This means for each “../” in the directory you see mentally think of that as going up one directory in the tree, also remember you can go up several directories before starting to go into subdirectories.

For example if you had one directory whose full path was:
/home/cent1/20/frank/stuff/data/results/set1/

And another directory who’s full path was:
/home/cent1/20/frank/stuff/backup/experiments/october/

If your shell had the second directory as its current working directory, and you wanted to copy a file from the first directory you could use a command like:

% cp ../../../data/results/set1/* ./

This command looks less frightening once you start to break it up carefully. Remembering that the current directory is:
/home/cent1/20/frank/stuff/backup/experiments/october/

Then just read it from left to right, it starts with “../” so up a directory, that puts us in:
/home/cent1/20/frank/stuff/backup/experiments/

Then another “../”, which would put our current directory to:
/home/cent1/20/frank/stuff/backup/

And a third “../” so now our directory is:
/home/cent1/20/frank/stuff/

At this point we see the command has the directory “data”, so we enter that directory, cp is now looking at:
/home/cent1/20/frank/stuff/data/

Then the “results” directory is listed, so cp moves itself to:
/home/cent1/20/frank/stuff/data/results/

Finally we see the “set1” directory, so cp focuses on:
/home/cent1/20/frank/stuff/data/results/set1/

The path then includes the ‘*’ (star) character. This character is a “wildcard” in that it matches anything. So the cp command copies all the files in this directory, and that’s the origin finished.

cp then moves onto interpreting the destination for this copy, but first it resets where its focusing on to the current working directory:
/home/cent1/20/frank/stuff/backup/experiments/october/

The only symbol in the destination is the “./”. This means that cp knows the destination is the current directory, and so copies all the files identified by the origin (above) to the current working directory.

4.5.3  What can be done with ∼

In connection with the use of relative paths there is the ~ (tilde) operator. This can be used as a substitute for the home directory in a number of ways. For example:

% less ~/foobar

Will use less to view the file called “foobar” in your home directory, whatever your current directory is. Similarly useful is:

% less ~bob/interesting/stuff 

Will use less to view the file called “stuff” in the subdirectory “interesting” of the home directory of user “bob”. This use of ~ makes viewing, copying and generally working with files in your and other users home directories much easier.

4.6  tar and tarfiles

There is a tool for Unix systems (and Windows systems too, but its less common to find it installed there) for dealing with bundling up a large number of files together for storage purposes. It’s called “tar” (originally it stood for “Tape ARchive” but you don’t have to be backing up onto tape to make use of tar). The archives it produces are known as “tarfiles” (and sometimes colloquially known as “tarballs”) and are a single file containing many files (a bit like a large bucket with a number of items in). Below is a quick guide to using the most common features of tar. For more complete documentation, as ever, see “man tar”.

The use of tarfiles can make transferring a large number of files, or backing up a large number of files much easier, you can for example create snap-shots of your home directory and then copy them elsewhere for personal backup, or just bundle up a large number of files before sending them via email. In its simplest form you can use:

% tar -cf newarchive.tar foo bar baz

This will use the tar command to create (the ‘c’ option) a new archive, the ‘f’ option means that the word after it will be the name of the new archive, in this case “newarchive.tar” In classic Unix tradition this doesn’t have to end in .tar (you can call the file anything you want) but by convention it does. Finally it will add the files “foo”, “bar” and “baz” to the archive.

If you want to see the contents of a tar file then the following command will do just that:

% tar -tf newarchive.tar

This will simply list all the files (the ‘t’ option) in the selected file (the ‘f’ option again), in this case “newarchive.tar”.

The final thing you’re likely to want to do routinely to tarfiles is extract the files from one so you can use them. The easiest way to do this is the following command:

% tar -xf archive.tar

This will extract (the ‘x’ option) all the files from the tarfile called “archive.tar” to the current directory, subdirectories and all.

A very useful variant of this is the following:

% tar -xpf archive.tar

This will extract (the ‘x’ flag) the files (‘f’ flag) from the tarfile called “archive.tar”. However it also has the ‘p’ flag. This causes the permissions of all the files in archive.tar to be preserved during output, meaning whatever permissions were set on those files when they were put into the tarfile they will have when extracted. See section 4.8 for more on permissions.

The p flag can be used in most other tar operations, so if you want to ensure the permissions are preserved when creating an archive you can use:

% tar -cpf archive.tar foo bar baz

Which will add “foo”, “bar” and “baz” to a tarfile called “archive.tar” and preserve their permissions.

4.7  gzip/gunzip

If you have exceptionally large files that you don’t need to access that often you can compress them using the “gzip” program. This reduces their size on disk but means they need to be “gunzipped” before they can be used again. To compress a file you simply use:

% gzip filename

Where “filename” is the name of the file you want to compress. After the program has run successfully it will rename the file to “filename.gz”, and examining that file with “file” will show its “gzip compressed data”.

Before you can access the data in a gzip compressed file you need to unzip it, this can be achieved in much the same way as it was compressed, with the command:

% gunzip filename.gz

After that command has run you should find that the file called “filename.gz” is now just called “filename” and it will be uncompressed and fully usable by any program.

Because gzip and gunzip are so commonly used on tar archives there is a flag that can be used with tar in order to make it automatically compress tarballs. If you use:

% tar -czf archive.tar.gz foo bar baz

It will create a tarfile called “archive.tar.gz” and put foo, bar and baz in it, then compress the archive with gzip.

To extract a .tar.gz archive you need to use the z flag again, for example:

% gunzip -xzf archive.tar.gz

That will act like a normal “tar -xzf” but will also gunzip the archive first.

On a related note you may find sometimes that .tar.gz files can have the alternative extension (such as .tgz). This indicates that they may be either packages for the Slackware Linux distribution, or that the person who made them available simply named them that as they liked to use 3 letter file extensions.

The best way to work out if something is a tarfile is to use the file command on it. See section 4.

4.8  Permissions

A lot of this document has so far mentioned permissions in one way or another, but nothing has been discussed in earnest how these work. The Unix permissions model is actually quite simple and is based on a few concepts. Primarily that all objects on the System (both files and directories) are owned by some user and some group.

Secondly that all these objects have three sets of “permission bits” one set for the owner, one set for the group and one set for every other user on the system. Each of these permission bits has a “read bit” shown by ‘r’, a “write bit” shown by ‘w’ and an “execute bit” shown by ‘x’. These permission bits are always listed rwx and are repeated three times, the first for the owner of the file, the second for the group that the file belongs too and thirdly for everyone else.

For files the meaning of these three bits are fairly obvious, a read-bit means those users can read (and therefore make copies) of that file. The write-bit means that those users can write data to the file, changing its contents, even to the point of emptying it, and the execute bit means that the user can run the file as a program (of course if it’s not really a program then this doesn’t do them much good).

For directories however the meanings change a little, the read-bit on a directory means that that group of people can “read” the directory i.e. ls it and see what files and directories it contains. The write-bit on a directory means that they can write changes to the directory itself, meaning they can create or delete files inside that directory, even if they don’t have permissions to do so to that file itself. It’s worth being very careful with assigning write permissions to directories. Finally the execute-bit governs if that group of users can change to that directory as their current directory.

To find out what the permissions are on something it’s best to use the ls command again, but this time with the -l (long output) flag:

% ls -l

This will give you a list of all the files and directories in your current directory. You should see that files have a single dash ‘-’ followed by 9 permission bits, 3 for user, 3 for group and 3 for other, followed by other information (and a plus symbol (‘+’) at the end to signal the end of the permissions. Directories should have a ‘d’ at the start to indicate they’re directories, this will be followed by their 9 permission bits.

You may notice that some entries in the directory have ‘l’ or indeed other characters for their first bit. ‘l’ indicates a symbolic link, for other symbols consult man ls.

To change the permissions on a file or directory you need to use the chmod command (CHange the permissions MODe of a file). It’s use isn’t that complex and centres around the use of three flags, ‘u’ for the user, ‘g’ for the group, ‘o’ for other. It also includes an ‘a’ flag that affects all three groups.

These group flags are then followed by a ‘+’ or ‘-’ symbol, depending on if you are giving them that permission or taking it away (obviously if that mode is already set it does nothing).

These symbols are finally followed by some (or all) the letters ‘r’, ‘w’ and ‘x’ which obviously represent the read, write and execute bits. This isn’t actually as complex as it sounds, as I’ll illustrate with a few examples:

% chmod o-rwx foo

This command removes the read, write and execute privileges for the “other” group from the file or directory (here called “foo”).

% chmod u+x foo

This gives the user execute permissions on the file, meaning if it’s a program it can then be executed by typing “./foo”, since its in the current directory.

One of the more useful commands is to remove all of the permissions from the “groups” and “other” category. To do this the following command works:

% chmod go-rwx foo

Which should ensure that no one but you can access the file, or directory, called “foo” in any way. It’s best to check with “ls -l” that this worked.

chmod can also use the ‘=’ (equals) symbol to set permissions exactly, as opposed to adding or removing them, so something like:

% chmod u=rwx foo

Would ensure that your user had read, write and execute permissions for the file called “foo”.

It should be noted you can add multiple strings to chmod, separating them with a comma, this means instead of doing multiple commands a single line like this one:

% chmod u=rw,go-rwx foo

Would ensure that only you could read the file “foo” and that no user from your group, or the other category could.

4.9  Quota

The amount of space you have available for use on the Unix systems is restricted by a quota, well in fact 3 quotas. One for your home directory, one for your email inbox and one for your space on the University webserver. These can be checked with:

% quota -v

This command will list each of the file-systems you have a quota on that are available at that point. It will list the “usage” of that file system, its “quota”, which is what you’re supposed to stay below and finally a “limit” after which point the System will start to refuse your ability to add more data to that file system (e.g. by creating new files). It will also tell you a few more interesting things.

Generally for the Lancaster Unix systems the three file systems you will see are “/export/home” which is your home directory, “/home/mail” your inbox and “/home/www” which is your webspace.

4.10  H: Drive

While at the University you have access to your “H: Drive” (Home Drive) system. This is a large block of disk space available to you from any windows machine you are logged into. However this space is also accessible via the Unix server with the use of the “lan” command (Which is part of homegrown, see section 13.1). Its use is fairly simple:

% lan

It will then prompt you for your password, and then connect to the system that hosts H:. Once there you can use get filename to get a filename from Central Files to your Unix current directory and put filename to send a file from Unix to your Central Files space.

Full documentation for “lan” is available at:
http://hedgehog.lancs.ac.uk:8080/mail/lan.html from any machine on campus. You can also type “help” when running lan to get a print out of all the commands it accepts. An example or two follows for clarity.

lan is most important for getting files from the Unix server to central files, or from central files to the Unix server. Since all the lab machines on campus have the so called “H: drive” bound to your space on central files this means the following is an example of how to move a file from your desktop lab machine to the Unix systems.

  1. First copy or move the file (called “filename” for this example) onto your H:\ on the Windows Lab machine.
  2. Then switch to your PuTTY login to cent1.
  3. type: lan at your prompt, it will ask you for your password.
  4. type: get filename to retrieve the file called “filename” from central files, it will be copied to your current working directory.
  5. type: quit to exit lan. You will be dumped back into your current directory, where you will see the file.

However the other main use of lan is to copy files from cent1 to your lab machine, this is the best way to view .pdf files or images like .jpg’s as the shell accounts on cent1 obviously have no graphical capabilities (although if you’re part of computing you can view them in A1a on the Unix terminals). This can be done by the following:

  1. Make sure your current working directory is the one containing the file you want to move to central files (which again will be called “filename” for this example).
  2. type: lan and give it your password.
  3. type: put filename this will copy the file to central files.
  4. type: quit to exit lan.
  5. Use explorer to look at your H:\ drive, you should fine there is now a file called “filename” in the base of your H: drive and you can now use Windows programs on it.

4.11  Moving files to and from the Unix systems from off campus

Quite often you will want to move files to and from the Unix machines but will not be sat at a lab machine, the following will work from either an off campus windows machine, or with some adjustment a Unix machine located anywhere.

There are two ways of doing this, either by using the “scp” program, which uses the Windows command line, or by using the GUI program called “winscp”.

The easiest way if you’re used to FTP style programs is to obtain a copy of winscp. This can be downloaded free of charge from:
http://winscp.sourceforge.net/eng/

Its fairly simple to use and with a minimal amount of testing you should get it working.

The alternative is to use a program called “scp” (SSH Secure Copy, see section 2.1) this can be obtained from:
http://www.chiark.greenend.org.uk/ sgtatham/putty/download.html
And runs using the Windows command line.

This program will then be used to either move files from your off-site machine, or drag files down from cent1 to your off-site machine. Unless you are running an SSH server you will never push files from cent1 to the machine.

So to copy a file from cent1 back to your off-site machine you need to roughly follow the following steps:

  1. Open a command prompt on your Windows machine, generally by hitting the Start button and selecting “run” then typing cmd and hitting return (although for those of you with Windows 95/98/ME machines you need to type command).
  2. Use the DOS cd command to move to the directory that scp.exe has been downloaded to. If scp.exe was copied to somewhere in the path (such as the C:\Windows\ directory) then you can skip this step.
  3. type the following:
    scp "C:\My Documents\filename" username@unix.lancs.ac.uk:~/
    This will copy the file “filename” from the directory “C:\My Documents\” to your home directory on the University Unix systems.
  4. The command will prompt you for your password, type it when prompted.
  5. When its finished transferring you can close the command prompt.

To copy a file from the Unix systems to your off-site machine use the following example as a base:

  1. Open a command prompt on your Windows machine, generally by hitting the Start button and selecting “run” then typing cmd and hitting return (although for those of you with Windows 95/98/ME machines you need to type command).
  2. Use the DOS cd command to move to the directory that scp.exe has been downloaded to. If scp.exe was copied to somewhere in the path such as “Windows” directory in C:\ then you can skip this step.
  3. type the following:
    scp username@unix.lancs.ac.uk:~/filename "C:\My Documents\"
    This will copy the file “filename” from your home directory on cent1 to the directory “C:\My Documents\” on the machine you’re sat in front of.
  4. The command will prompt you for your password, type it when prompted.

You should note that the syntax for scp is virtually identical to that of the cp command (see section 4.2.4) except that before giving the path of the file you have to say “username@machinename:” , “username” being your remote username, “machinename” being the machine you want to copy the file off, and both of those followed by a colon.

4.12  scratch ($scratch)

On the central Unix systems sometimes you will need more space than is ever going to be given to you on your quota, for this reason the scratch file space exists. The location of it is stored in an environment variable (see section 6.3) called $scratch. What this means is that if you type:

% cd $scratch

You should find yourself in a directory somewhere under /scratch/ in the hierarchy, this is your “scratch space”. You can put files here of fairly big sizes (multiple hundred megs) and do things like program compiles too big for your normal home directory. However files in $scratch will be deleted after a couple of days, or if the space is needed by newer files, so be careful what you store in there. As long as you don’t put anything permanent in it you’ll generally be ok.

4.13  webspace ($WWWHOME)

Assuming that you’ve applied for webspace (if not see
http://www.lancs.ac.uk/iss/registration/getapage.htm) then you can access the directory that holds your HTML files from the Unix system. The location for your directory should be stored in the variable $WWWHOME, however this will just be your home directory on the webserver, the directory that web-content is served out of is called “public_html” and is stored in that directory. So to get to the directory that should hold all your files simply type:

% cd $WWWHOME/public_html/

You can also use this method to copy files, and the following command will copy a file called “index.html” from your current directory to your webspace.

% cp index.html $WWWHOME/public_html/ 

4.14  mailspace ($MAILHOME)

The final place in the file system stored as an environment variable is $MAILHOME, which allows you to go to the quotad space the mail server can see, this contains your INBOX and sometimes a mail-box containing sent-mail as well. Really there is very little point going to this space (it is useful space however, as it stores all your incoming mail), but it can be helpful to know its there, for example to backup your inbox.

4.15  Dealing with awkward files

There are a number of files which are awkward to deal with via your shell account. These include graphics files, PDF files, MS office documents, Windows executables... that kind of thing.

Some of these can be dealt with on the Unix machine, this includes PDF files and word documents, although to use more graphical tools on these follow the instructions in section 4.10 to move them to another machine.

To view a PDF file you’ll need to extract the text from it to read (you will sadly loose all the graphics from it) to do this you can use a command like the following:

% pdftotext foo.pdf 

This program requires using the homegrown software (see section 13.1). What it does it it extracts all the text from a pdf and writes it to a file called “filename.txt” so in this case it would extract the text from “foo.pdf” to a file called “foo.txt”. If you want to change the name of the file to extract too you have to provide another argument, such as:

% pdftotext foo.pdf text-of-foo

This will extract all the text from “foo.pdf” and will write it all to the file “text-of-foo”.

Word documents are a slightly different story, they require a program called “antiword” to read. This is stored in the ∼clinch link-farm (for more information see section 13.8). An example would be:

% ~clinch/bin/antiword report.doc | less

This will extract all the text from the file “report.doc” (assuming its really a word document, try using file on it), and then present the text for viewing in the pager less, so space bar for down a page, ‘b’ for up a page, ‘q’ to quit.

For other files (such as graphics) its often best just to use lan or scp (see sections 4.10 and 4.11 for more information) and view them using your lab machine.


Previous Up Next