INTRODUCTION

Files that are downloaded from other systems often have names that are
difficult to deal with, either because they are so long or because they
contain characters that have special meaning to the Unix operating
system.  This document addresses some of the problems encountered in
dealing with these files.

AIX permits filenames to contain as many as 256 characters.  While a
name that long permits great flexibility, generally filenames can be
descriptive without using that many characters!  Unlike some systems,
filenames can contain more than one dot; e.g.; letter.dec.94 is a valid
filename.  Naming files according to their subject and placing them into
appropriate directories can help keep one's file space organized, which
helps in locating information.

Q: How can I delete a file beginning with a hyphen?

A: Because a hyphen at the beginning of a filename will be taken to be
an option of the rm (remove/delete) command, this causes a special
problem when you try to delete (or copy or rename) it.  You will
probably get an error message telling you that there is no such option.
One way to delete the file would be to specify the current directory
along with the filename so that the filename no longer begins with a
hyphen.  Do this by putting the characters ./ (which specifies the
current directory) before the filename, as in

    rm ./-badname

Questions concerning deleting, copying, or renaming files whose names
contain "funny" characters are among the most frequently asked.  Often
the only way to operate on such files is to match the filenames using
wildcard characters, a pattern-matching technique that is described
below.

The problem can be avoided by NOT using the following characters in your
filenames, since each has a special meaning in the Unix system:

      /  "  '  `  *  ;  #  ?  -  !  &  <  >  $  space  [  ]

A space in a filename should be avoided since many Unix commands would
interpret the name as two filenames rather than one.  You could use a
period (.) or underscore (_) where you might have wanted a space; for
example, read.me or read_me rather than read me.

If you are transferring a file from another system to a computer running
the Unix operating system and the name contains any of the above
characters, the best solution is to rename the file before the
transfer.  If you somehow get a file whose name begins with a hyphen or
contains any of the special characters, you can try pattern-matching or
using single quotes (') around the name of the file.  The character *
will match zero or more characters and ? will match a single character.
Characters enclosed in square brackets ([ ... ]) will match any single
character listed withing the brackets.

Several examples are shown below which work for the remove (rm)
command.  Other solutions may be needed for copy (cp) or move (mv),
since the file to be copied or moved must match a unique filename.
Since the default for all three commands is to perform the action
without giving you a chance to change your mind, be sure to either alias
each command to contain the -i option which will query you for
confirmation (this is the default at UVa), or use the -i option when you
issue the command.

Consider a directory containing these files:

.hidden
Quarks
co.5
co.10
cow
coward
fi le
fi_le
quark1
quark2
quark3

The following commands could be used to delete various combinations of
those files:

    Command                  File(s) Deleted
  rm co*                  coward cow co.5 co.10
  rm ?uark?               Quarks quark1 quark2 quark3
  rm quark[12]            quark1 quark2
  rm quark[1-3]           quark1 quark2 quark3
  rm 'fi le'              fi le
  rm f*                   fi_le fi le
  rm *.*                  co.5 co.10
  rm .*                   .hidden
  rm *                    all but .hidden 

The simplest way to match almost any filename is to use either * or .*
because * will match all filenames except those beginning with a leading
period (.) and .* will match all filenames beginning with a period.
Because most filenames will be matched, this can be dangerous; you could
very quickly and easily delete all your files!

© 2008 by the Rector and Visitors of the University of Virginia.

The information contained on the University of Virginia’s Department of Information Technology and Communication (ITC) website is provided as a public service with the understanding that ITC makes no representations or warranties, either expressed or implied, concerning the accuracy, completeness, reliability or suitability of the information, including warrantees of title, non-infringement of copyright or patent rights of others. These pages are expected to represent the University of Virginia community and the State of Virginia in a professional manner in accordance with the University of Virginia’s Computing Policies.