How do I manipulate files with special characters in their name?
There are five problems that occur when dealing with strange filenames:
- The characters in the name are specially handled by the shell.
- The file name contains an exclamation point or a single quote.
- The file starts with a "-".
- The file name contains non-printing characters.
- The file name contains a "/".
Note: In the following examples, solutions are only given for removing the files. The situations and solutions can also be applied to the mv (move) and cp (copy) commands. Use the same syntax for manipulating the characters, but apply them using the traditional conventions for the respective commands.
For example, ";" means "that's the end of that command, start a new command. So if you try to delete a file named "a;b", the shell will say, "there's no file named a, and there's no command named b". Tab, &, #, *, ", ?, and space also have special meanings.
The fix for this is to put the file name in single quotes, which tells the shell that it's all one name, and not to interpret special characters. In the example above, the command would be
% rm 'a;b'
Shells which have !-style history will try to interpret the "!" character even inside quotes. It's difficult to quote a quote character.
The solution is to put a "\" before the exclamation point or single quote. The "\" character tells the shell to ignore the special meaning of the next single character. For example:
!bang 'quote 'huh?'
% rm !\bang
% rm \'quote
% rm \'huh\?\'
The rm command, like most unix commands, interprets a "-" as an option flag. So, a file name that starts with a "-" is interpreted as a list of options, not as a file name.
The solution is to make sure that the "-" is not the first character.
% rm ./-xyz
In the working directory, ./-foo and -foo both refer to the file named -foo because "." means "the working directory".
4) The file name contains non-printing characters (such as ^Q), or 8 bit characters. Ls will often display these as "?".
Some programs strips the eighth bit from all characters, some don't. In order to delete such a file name, you have to use a shell, such as T-shell, or a program that handles 8 bit characters to remove them.
This can only happen when the file was created by a non-unix machine that was mounting the file system over the network.
The solution is to remove or rename the file from whatever machine created it.
For any other instances that don't apply to the above cases, please contact CETS.