MA10126

Essential Unix

 

Introduction | Logging in | Directories | Files | Text Editors | Miscellany | File access | Links

 

4. Files

 

To display on screen the contents of a text file, use the command

cat filename

 

cat stands for concatenate; the command

cat file1 file2 > file3

will take the contents of the file file1, append to it the contents of the file file2, and output the result into a new file file3.

 

If the textfile is large, the contents displayed using cat will zoom off the top of the Unix window. To view the contents one page at a time, use the command

more filename

instead of cat. Press the space bar to get the next screenful, or press Ctrl-c (press the c key while holding down the control Ctrl key) to halt execution.

 

To delete (remove) a file, use the command

rm filename

 

To make a copy of file file1 as a new file file4, use the command

cp file1 file4

 

Note that rm, cat, cp and mv will perform their tasks without any warnings or the “Do you really want to delete this file?” queries that you get in Windows. With the cp command above, if the file file4 already exists, it will be overwritten with the new one without warning. Remember that we said that Unix does not treat you like an idiot? If you want to be treated like an idiot, you can use the command with the option –i

cp –i file1 file4

You will then get a warning if the destination file already exists. (Some Unix installations make this a default option. Respond to a query such as “rm: remove file.dat (yes/no)?” by typing y or n for yes or no.)

 

An important feature of Unix commands, is that they can be modified by options; options are indicated by a hyphen followed by a letter, e.g. -i. As Unix is case-sensitive, all commands and options should be typed lowercase – except that there are a few options which are denoted by uppercase letters.

 

You can get information about a command and the options it can take, by consulting the online manual. Type

man ls

to see the manual page about the ls command and the many options available. As manual pages are often very long, they get displayed using more. 

 

The most important options for ls are:

-a to list all files and directories, including those whose names start with a “.”, which are otherwise hidden;

-l to list full details of each file;

-s to list the file sizes (in blocks – on our system each block is 0.5kb);

-t to list the files in the order in which they were created, rather than alphabetically.

Options can be combined, for example

ls –tal calendar

will list files in the calendar directory using the –t, -a and –l options.

 

To delete a directory, use the command

rmdir dirname

Note that a directory can only be deleted if it is empty, i.e. if all the files in it have first been deleted. It is however possible to use the recursion option –r :

rm -r dirname

This will delete the directory, all the files in it, and all subdirectories of it and their files, and so on. Be very careful about using this powerful option!

 

You can use * as a wildcard in filenames, standing for any string of characters. Thus,

rm data*

will delete all files whose names start with data. Another useful wildcard is ?, standing for a single character. The command

rm data?

would delete files data1 and data2, but not data11.

 

You should by now realise that Unix filenames don’t have to have filetype extensions as in Windows (where Word documents have the .doc extension, for example). But it is convenient to use these for particular filetypes, and there are some conventions (Unix webpages have .html extension). Then you can use

rm *.dat

to delete all files with the .dat extension.

 

 

Practise these commands by performing the following exercise:

 

Start in your home directory. Try to delete the calendar directory you created in the previous exercise (not using the –r option); this won’t work.

You will have to delete the files (today and users0) in the subdirectory week0, then the week0 directory itself, before being able to delete calendar.

Now create files ls.hlp, cp.hlp and cat.hlp, containing the online manual pages for the ls, cp and cat commands respectively. Check they exist by listing the files in your home directory which end in .hlp.

Display the contents of each file, and read through the options available for each command. Try out some of the options for ls, alone and in combination.

Concatenate the three files into a new file called helpfile. Display its contents one screenful at a time.

Delete the original three files using a wildcard, leaving just helpfile.

 

 

To see the solution to this exercise, click here.

 

An important and surprising feature of Unix, is that as well as accessing your own files, you are able to copy files from other people’s filespace (unless they have changed file and directory permissions to deny access). Try this by getting a copy of a file called tutorial2c.m from Dr Tony Robinson’s filespace into you own. His username is masar, and the file is in his directory called ma10126. So you should type:

cp ~masar/ma10126/tutorial2c.m tutorial2c.m

Check it is in your own filespace, and list its contents. This is a MATLAB script file, containing a sequence of MATLAB instructions.

 

You can also change working directory to another person’s one, and list the files in it. Have a look at the contents of Dr Robinson’s directory by typing

cd ~masar/ma10126

ls

 

The one task we are as yet unable to do, is to produce our own textfiles by inputting text into a text editor. This is covered in the next page. Click on “Text Editors” in the linkbar below…

 

Introduction | Logging in | Directories | Files | Text Editors | Miscellany | File access | Links