Access a Filename Containing Unprintable/Special Characters for UNIX

What to do when you create a filename which has special characters in it which make it difficult to manipulate?

When you create files (on purpose or accidentally) with special characters in them, it causes unique problems. This can be caused by saving a file from an application, transferring it from another host, etc. In some cases you have simple alternatives, in others you have to get a bit more fancy.

The first thing to try is to (UNIX) "quote" the character - what this means is to prefix the character with the backslash ("\") symbol. This causes the character to be interpreted as that character, not specially interpreted by the UNIX shell. (Some characters have special meaning to the shell beyond the simple ASCII value.) For example, if you have a filename like:


you can try to access it with:

foo\!file              (Example: more foo\!file  )

You can either continue to access it like that (a pain if it's frequent) or rename it to another name. For more information on quoting in the shell see 'man csh' and search for "metacharacters".

Another thing which might work is to enclose the filename in quotes when you reference it. This works well for files created with spaces in the name, like:,

my paper

To access this file, you can use:

"my paper"             (Example:  more "my paper"   )

You can also use the UNIX quoting method:

my\ paper              (Example:  more my\ paper )

If the above two ideas fail you can deal with the file not by name, but by the numeric value of the pointer to the file, or the inode number. This is particularly useful if the special character is a non-printable character, like a control character (for example, Ctrl-g). To list the inode numbers of files in your directory, you can use the "-i" option on the ls command:

ls -ali  pattern

where pattern is a pattern to match (or leave blank for all files). For example, you might see something like:

rac2> ls -ali *test
 208978 -rw-r--r--  1 USERID       2430 May  3  1993 appletalk.test
 209299 -rw-r--r--  1 USERID       3287 Oct 25  1993 creative.test
 208946 -rw-r--r--  1 USERID        447 Jun 22  1993 prob!test

The number first on the line is the inode number, or inum of the file. You can then use this number with the 'find' command to modify the file or filename. The general format for 'find' is:

find  path  expression [ expression ]

where path is the directory in which the search is started (and traverses all subdirectories from there down) and expression is the command(s) executed if a file is matched by the find command. For example, to simply remove the file "prob!test" file above (assumed in or under the home directory), type:

find ~/ -inum 208946 -exec rm -i {} \;

Note: the "\;" at the end of the command is vital to include when using the "-exec" expression of 'find', or the command won't know it's been terminated and will just hang there. What this does is match all files which fit the search criteria (in this case inum = 208946), and all files matched are inserted into the "{}" area in the command executed by "-exec". Of course in this case, only one file will be matched.

If you wish to rename the file to a different name, try something like:

find . -inum 208946 -exec mv {}  new.filename \;

This will rename the file from "prob!test" to "new.filename". (Again, don't forget the last two characters on the line!) This second example assumes the file exists in (or under) the directory you're currently in.

For the path the home directory can be indicated as "~/", the current directory can be indicated as simply "." (period), and you can always code the absolute path as well.

Advanced Note: If you happen to have remote file systems mounted under the directory structure in which you're searching, use the "-mount" option. This will prevent the following of the link to the remote file system, possibly changing or removing remotely-mounted filenames you don't intend to. For example:

find ~/ -mount -inum 208 946 -exec rm -i {} \;

will only remove a file matched on the local filesystem.