Return to Manual Contents Astronomy Computer Manual

Files and the Filesystem

Listing your files: ls
Moving Around: pwd, cd
Directories: mkdir, rmdir
Ownership and Protection: chmod
Creating files: cat
Looking at Files: more and less
Destroying Files:rm,mv,cp
Copying Files from other Computers:rcp,ftp
Wildcard Characters: ? and *
Pipelines and Redirects: |,>,>>
Searching a File: grep

Listing your files: ls

Unix keeps your files in directories. When you log in, your directory is set to your 'home' directory. On lepus, this is /home/username. To see what files you have type ls. This will list all of your files.

There are some special files that begin with a '.' ie. .tcshrc, .login etc. To list these files, you must use special options with ls, namely ls -a. To get a listing with ownership and access times try ls -l. You can also combine options: ls -la
Do a man ls to read about all the possible options.

Top of Page

Moving Around: pwd, cd

To find out your current directory use the pwd command:

lepus% pwd

/home/smith/data1/data2

lepus%

To move from one directory to another you type cd directory_name where directory name is the whole path. For example:

lepus% cd /home/smith/data1

This would move you from your current directory to the directory specified. A short cut to move up one directory is: cd .. and to move to your home directory all that you have to type is cd without specifying the directory. To change into subdirectories, you don't have to specify the whole path. For example, if you were currently in /home/smith and wanted to go to /home/smith/data1, you would only need to type cd data1 .

Top of Page

Directories: mkdir, rmdir

You can create as many subdirectories as you wish, ie /home/smith/data1/data2. To create a directory you type mkdir directory_name

To remove a directory it must be empty, ie. contain no files. The command to remove an empty directory is rmdir directory_name To remove a directory and all of its files you can (with caution) use the command rm -r directory_name See information regarding permissions in the section on ownership.

Top of Page

Ownership and Protection: chmod

If you were to do an ls -l you would get something like this:

lepus% ls -l

drwxr-xr-x 2 smith 512 Nov 13 10:34 data1
-rw------- 1 smith 1912 Sep 20 1991 junk1
-rw-r--r-- 1 smith 72 Nov 17 15:47 junk2

The first group of letters indicates the file protection for various groups of users. The first letter indicates the file type: a 'd' indicates that data1 is a 'directory', a '-' indicates a normal file.

The next 9 letters show the file protections. The protections are for read (r), write (w) and execution (x). A '-' means that the certain function is not allowed. They are in groups of three representing protections for the owner (u), the group (g), and the final three for others(o). The owner is you, groups are shared by your supervisor and all of his students, and others means all other lepus users.

Example: In the first example, for the directory data1, the owner has all read, write and execution privileges, however, group and others have only read and execution, meaning that they cannot modify anything in the directory.

To change file protections, use the command chmod, for example, chmod o-r filename would remove read privileges for others on the specified file, whereas chmod g+w filename would give permission for all group members to write to the filename.

The third column, 'smith', indicates the owner of the file. Each user owns the files in their own directory.

Top of Page

Creating files: cat

To create a file you have several options: you may either use the command cat or use one of the editors (vi, emacs or xemacs). cat stands for concatenate and type. cat reads each file in a sequence and displays them in a specified output. If no files are given then cat reads from the standard input (your keyboard), until you type a ^D (control D) at the start of a new line. Similarly, if no output file is given cat directs the data to the standard output (your terminal screen).

To create a file from standard input:

lepus% cat > advice
Always borrow money from a pessimist
He doesn't expect to be paid back.
^D

The file 'advice' now contains the two typed lines above. If you wished to view this file, cat the file 'advice' to standard output:

lepus% cat advice

Always borrow money from a pessimist
He doesn't expect to be paid back.

You can create an output file from an already existing file(s), using the format cat file1 file2 file3 ... > output_file . ^D

You can create an output file from an already existing file(s), using the format cat file1 file2 file3 ... > output_file .

Top of Page

Looking at Files: more and less

If your file is longer than the height of the screen then, the contents of the file will fly by faster than you can read it. To prevent this dilemma from occurring use the command 'more' instead of cat to look at files. The 'more' command will stop at the end of each page and will only continue if you hit the space bar. You use more in the following way:

lepus% more filename

A similar, but much more versatile command is 'less'. This command is used in the same way as 'more', but allows you to go backwards as well as forwards, search for strings, etc. You may find this much more useful than 'more'.

Top of Page

Destroying Files:rm,mv,cp

If there is a file you wish to get rid of then use the command 'rm'. Make sure you are certain you want to remove the file because once it has been removed, it's gone forever.

lepus% rm filename

You can move a file into another file by using the command 'mv' (this is really just renaming the file).

lepus% mv filename new_filename

A word of caution: the 'mv' command wipes out whatever was in the target file.

At the end of this procedure, new_filename would contain the contents of what was in filename, and filename would no longer exist.

To copy one file to another use the command 'cp'. Unlike the command 'mv', 'cp' will save the first file, but like 'mv', 'cp' will remove the contents of the target file. There are many options that go along with 'cp' - see the man pages for more details.

lepus% cp file1 file2

Top of Page

Copying Files from other Computers:rcp,ftp

'rcp' copies files between computers. The syntax is rcp hostname:path filename . rcp does not prompt you for a password. On local Astronomy machines, permissions to rcp will normally already be set up, but if permission is denied, then you will have to set up a '.rhosts' file to grant 'rsh' permissions between the two computers for your account. (You also can use scp , which is more secure).

lepus% rcp radio:/u/smith/data.file junk1

would copy the contents of data.file from the computer radio to a file called junk1 on lepus.

'ftp' - file transfer program - transfers files to and from a remote network. You use the commands 'get' and 'put' to copy the files from one computer to another. Here is a typical session:

lepus% ftp radio.astro
Connected to radio.astro.
220 radio.astro FTP server (SunOS 4.1) ready.
Name (radio:scott): scott
331 Password required for scott.
Password: <passwd not echoed>
230 User scott logged in.
ftp> bin
200 Type set to I.
ftp> get data.file
200 PORT command successful.
150 Binary data connection for data.file (128.100.89.33,3616) (636 bytes).
226 Binary Transfer complete.
local: data.file remote: data.file
636 bytes received in 0.06 seconds (10 Kbytes/s)
ftp> bye
lepus%

Top of Page

Wildcard Characters: ? and *

As in VMS and MS-DOS, Unix has wild card characters. The ones used most often are ? and *. ? is a wild card character representing one character and * represents an arbitrary number of characters. For instance, you may wish to remove all the files that begin with 'ab'. To do this, you would type rm ab* Another example would be: ls [a-m]* which would list all the files beginning with the letters 'a' through 'm'. Be very cautious in using wildcards, especially with the 'rm' command.

Top of Page

Pipelines and Redirects: |,>,>>

The output of one command can be fed directly as input to another. A set of commands strung together in this way is called a pipeline, and the symbol for the input/output connection is a vertical bar (|). Example:

lepus% ls -l | grep junk

would list any file in the current directory matching junk (see more on grep in the section grep).

Redirects (>) are used to redirect the output from the screen (called the standard output). You can redirect the output to a file.

lepus% ls -la > junk

The above command would create a file 'junk' containing a listing of the current directory. Be careful: '>' will overwrite the file 'junk' if it already exists. Using '>>' will append the information to the pre-existing file 'junk'.

Top of Page

Searching a File: grep

Often you will want to find a certain string in a file, but you do not want to spend your time searching through the whole file (or files). The command 'grep' will search through a files(s) for the string you want and then print the line(s) that the string happens to be in. 'grep' is very particular: you must make the distinction between upper and lower case letters. 'grep' is also not crazy about strings that are more that one word long. To look for multi-word strings, enclose the string in quotes. The format is:

lepus% grep 'string' filename

Of course you can include wildcards in the filename! grep -i string filename will disregard the letter case and print all matching results of 'string' regardless of the case of the letters.