UC San Diego SearchMenu

Files: Unix commands for creating, renaming, copying, removing, redirection and pipes

Files are basic collections of information in the UNIX system.

Most files are text files created with an editor. You can create a new file with the "vi" editor (see the online Beginner's Guide to the VI editor):

vi newname

Every file must have a unique name. As a rule of thumb, stick to the following characters when inventing file names:

alphabetic characters 
numerals 
underscore 
period 

Most other characters (e.g. 

/!@#$\%^&*(){}[]-~|;"'?<>

) have special meanings and can be troublesome.

Getting information about existing files

Use the "ls" command to inquire about files.

        % ls            (list the names of files)
        % ls -l         (list name, size, modification time, etc. of files)

Copying and moving (renaming) files:

    % cp  orig   backup            (Make a duplicate of "orig",
                                        entitled "backup".  CAUTION:
                                        if "backup" is a file that
                                        already exists, then using
                                        cp will replace its contents
                                        with those of "orig".

         % mv  oldname newname          (Change the name from "oldname"
                                        to "newname".  CAUTION: prior
                                        content of "newname" is discarded.)

       

If the destination file already exists, the previous contents will be overwritten (destroyed) by a mv or cp. Also, avoid using the wildcard character (*) due to the potential for destroying files. Once files are destroyed, they cannot be recovered by the user. The cp command is not generally useful for copying directories and directory hierarchies; the tar command provides that capability. See "man tar".

Removing files

<pre>       % rm  filename                (Remove file.)
       

Commands to remove files are irreversible; once a file is deleted, it cannot be recovered by the user.

Output Redirection

The output of most commands and programs goes to standard output (the screen). It is often desirable to save such output in a file. Use the following operators to redirect output into a file:

Operator           Usage                    Description

        >       % command > filename    Send command output, except
                                                                   error messages, into file
                                                                   filename

        >&      % command >& filename   Send command output, including
                                                                   any error messages, to file
                                                                   filename.

        >>      % command >> filename   Append output of command to
                                                                   the end of file filename

       

WARNING: The destination filename must not be not used on both the left and right sides of the redirection operator, otherwise the contents of files will be destroyed. For example, this command will destroy the previous contents of file file2:

   % cat  file1  file2 > file2

Pipes

The UNIX pipe mechanism provides a means of combining the functionality of various UNIX utilities. UNIX utilites are designed to be general purpose tools. Each one performs a particular kind of task, e.g. the "grep" command retrieves lines which contain some pattern, "sort" sorts, and "more" displays files a screenful at a time. Suppose that you wanted to do some process that required the functions of grep, sort and more. Let's say you wanted to find all the lines that contained "Smith" in a file called attendees, you wanted to sort those lines, and display the result a screenful at a time. You could do the job as separate steps:

   % grep Smith attendees > temp1
        % sort temp1 > temp2
        % more temp2

       

But there's a better way to do this using "pipes". A pipe connects two utilities so that the output from one becomes the input to the next. The vertical bar (|) signifies a pipe connection. The same three steps shown in the example above can be performed in one fell swoop using pipes:

   % grep Smith attendees | sort | more
       

The modularity of UNIX utilities and the convenience of connecting them with pipes makes it easy to quickly construct very powerful mechanisms to perform a variety of tasks.

Using symbolic links to access Samba shares

A symbolic link is a directory entry that points to a file or directory elsewhere. This can be useful when you have multiple class storage directories and want to move about between them. It's essential when you want to access these class directories using Samba shares on ACMS lab PCs.

Example: User abrayn has an OCE account on the ACMS sgva-serv1.ucsd.edu server. Her OCE account home directory there is /u/disk19/abrayn, as seen by the following command she enters at the UNIX prompt in a command window:

  % pwd
        /u/disk19/abrayn
       

She also has a class allocation for va160w, including a storage directory under the class umbrella. To access this directory from the UNIX command window, she could use 'prep va160w':

  % prep va160w

        You have a directory: /u/disk24/va160w/abrayn
        Do you want to change to that directory now? (y/n) [y]:
       

However, from a PC in an ACMS lab, she can't Log-in using 'abrayn' and see this same directory unless it appears as a folder in her home (H:) share. To make this happen, she'd need a symbolic link in her home directory.

To create this link (from a UNIX command window), she could use:

  % ln -s /u/disk24/va160w/abrayn  va160w
       

This would create 'va160w' in her home directory which points to this directory. Following that link (going into the folder 'va160w' in the Samba share) would take her there, just as 'cd va160w' would do in the UNIX command window.

Any files she creates there would use the space she has available on /u/disk24 for the va160w class. Since that space is available in addition to space she normally has for her OCE account, she'll be less likely to exceed her disk quota than if she just used her OCE allocation.

Further information

For more information on creating symbolic links, type 'man ln' at the UNIX prompt in a command window.

For more information on OCE accounts, search for keyword 'OCE' in the ACMS Online Help System.

To view your current allocations, use the ACMS Account Lookup Tool, or type 'allocations' at the UNIX prompt.