Technical article 104762

How do I remove files that have special characters in the name?

OpenServer 5.0.0

Written · Last revised

Problem

Users have created files on a UNIX filesystem (which could be one of the following types of filesystems):

DTFS -- Desktop Filesystem
S51K -- UNIX filesystem
HTFS -- High Throughput Filesystem
EAFS -- Enhanced ACER Fast Filesystem
AFS  -- ACER Fast Filesystem
XENIX - XENIX Filesystem)
that have special DOS-type characters or escape sequences
embedded in the filenames.  (Some of the characters are not
reproducible by the keyboard - others are the backslash ("\")
character.)

How do I remove files that have special characters embedded in the name?

Solution

Here are five possible solutions to this problem:

1. If you can use wildcards to list the filename that
contains unprintable characters, and only that filename
appears in the listing, then you can also use wildcards
to remove the file.  First, display the files in the
directory with:
# ls -b | more
This will cause unprintable characters to be displayed
with "?"'s as placeholders.   For example, if the file
you want to remove was displayed as "f?oo?bar", then
do the following:
# ls -lb f?oo?bar
The "?"'s in the above command are interpreted by the
shell to match one character each.  One line should
display, with the name of the file you wish to remove.
Finally, to remove, do:
# rm f?oo?bar
Note that you can also use the "*" wildcard to match
zero or more characters in the filename, trying to
match just the file you want to remove.  Then use the
same pattern in the rm statement.  For example, if:
# ls -lb *bar

matches just the "f?oo?bar" file, then you can remove with:

# rm *bar

2. By inode - if the files to be deleted are few in number, then locate the file by the inode number. Issue the command:

(NOTE: The "#" represents the prompt and should not be typed.)

# ls -li
  total 630
  10985 drwxr-xr-x   2 root  other   32 Feb 17 03:00 680053
  10510 -rw-r--r--   1 root  other    0 Jun 15 13:52 \foo
  10938 drwxr-xr-x   2 root  other   64 Jun 02 03:00 dev2695
  10899 drwxr-xr-x   3 sys   sys     48 Apr 08  1994 init
  11014 drwxr-xr-x   2 root  other   32 Feb 10 15:20 junk1
  10792 drw-r--r--   2 bin   bin     32 Mar 24  1993 messages
  10900 drwxr-xr-x   2 root  other   32 May 23 07:51 sme0

The number that appears in the first column is the inode number bound to the filename.

Next, remove only the file which has the appropriate inode
number.  In the previous example, the target file is "\foo"
and the inode number for this file is 10510.
                                      ^^^^^

To remove the file, use a compound find(S) command:

# find . -inum 10510 -exec rm {} \;
               ^^^^^
It is important to realize that you must be in the same
directory as the target file.  This find command will delete
the "\foo" file as it has inode number 10510 assigned to it.
3. By using escape sequences  - If the special character in
the filename is a printable character, then you can escape the
special character by preceding it with a backslash (\).  For
example, to remove the file "\foo", preceed the special character,
in this case a "\", with a backslash, as:
# rm \\foo

This will remove the file that appears as "\foo" in the previous example.

4. By selective removal -- ***CAUTION*** This can erase every
file in the current working directory!  Please read the
next section carefully before proceeding:

Sometimes, especially from a networked DOS environment, files can be created which contain unprintable characters. Since these unprintable characters cannot be duplicated from the keyboard, you need to specify all files in the target directory as files to be removed,

There is an argument to the rm(C) command that will place
rm in interactive mode.  The "-i" argument requires
confirmation for each and every file to be deleted.
By combining this option with a wildcard to specify all
files in the current working directory, you can target
only those files which have unprintable characters for
deletion:
# cd {target-directory}
# rm -i *

Once you have selected the files to be deleted, by responding with a 'y' at the prompt, you can break out of the rm command by pressing the <Del> key.

5. You can change the current working directory to the
directory that contains the improper file names.  Once
there, you can use the hd(C) command.  This will tell
you what non-printable characters exist within a file
name by displaying the octal values of those codes:
# cd {target-directory}
# ls | hd | more
    -- or --
# ls | od -c | more

You can then remove the file by typing in the literal code string which was displayed in the hex dump:

# rm "f\0157oo\0142bar"

See also

man pages for rm(C) and find(C)

From the Xinuos technical article database, record 104762. Reproduced with permission. The original is at www.sco.com/ta/104762, which is reachable but blocked from search engines. Xinuos marks this article against a retired product line. That refers to the product’s lifecycle, not to whether the advice still works — plenty of it still applies to running systems.

Next step

Need this fixed rather than explained?

Tell us the release, what the system runs and what it is doing. We will reply by email with what we would do next.

  • You deal with the people who do the work
  • No obligation, and no upgrade required before we will help
  • We tell you plainly if something is not worth doing

Send an enquiry

We reply by email. If a server is down, say so in the first line. You can also write to help@scoexpert.com.