Search: This Site | People | Departments | Penn State  
Penn State Home Page Population Research Institute Home Page
  contact PRI news directory sitemap home

SSRI Home Page PRI Home Page

Removing 'funny' files


Every once in a while, you'll do something wrong and create a file that for one reason or another can't seem to delete. UNIX allows you to have virtually any character (the only exception is a slash '/') be part of a filename. This includes spaces, punctuation, and even characters not in the English alphabet. Unfortunately not all programs handle these 'strange' filenames equally well, and you may want to remove or rename them. Here are some examples on deleting these files, but just substitute the rm command for the mv command (with the name to rename it to) if you want to to rename it. (helpful if you want to look at the file first before deleting it)

Files with spaces in them

% ll
-rw-r--r--  1 barr           87 May 17 14:02 hey look at me
-rw-r--r--  1 barr          232 May 17 14:09 normal.wp
-rw-r--r--  1 barr          670 May 17 14:01 normal1.wp
% rm hey look at me
rm: hey: No such file or directory
rm: look: No such file or directory
rm: at: No such file or directory
rm: me: No such file or directory
%
At the command line, the space character is used to separate arguments to a command. Unfortunately if a file has spaces, this is a conflict! To have UNIX treat what you type as all one file, put single quotes around it:
% rm 'hey look at me'
% ll
-rw-r--r--  1 barr          232 May 17 14:09 normal.wp
-rw-r--r--  1 barr          670 May 17 14:01 normal1.wp
%

Files with quotes in them

This is a bit harder, since it depends a bit on the quotes. You can put double quotes around single quotes, or single quotes around double quotes. Here's a few examples:
% ll
-rw-r--r--  1 barr            0 May 17 14:20 "oops"
-rw-r--r--  1 barr            0 May 17 14:16 here's a file
-rw-r--r--  1 barr          232 May 17 14:09 normal.wp
-rw-r--r--  1 barr          670 May 17 14:01 normal1.wp
% rm "here's a file"
% ll
-rw-r--r--  1 barr            0 May 17 14:20 "oops"
-rw-r--r--  1 barr          232 May 17 14:09 normal.wp
-rw-r--r--  1 barr          670 May 17 14:01 normal1.wp
% rm '"oops"'
% ll
-rw-r--r--  1 barr          232 May 17 14:09 normal.wp
-rw-r--r--  1 barr          670 May 17 14:01 normal1.wp
%

Other shell metacharaters

The shell (the program that reads the commands you type and executes them) has certain special characters that are treated specially. Here's a list of them:
' " ! $ ^ & * ? < > ( ) { } [ ] ; \ | ~ `
(The last character is a backquote, which may appear here or on paper as a forward single quote.)

If any of these characters appear in your files, you will need to "escape" them to tell the shell to treat them as their literal value, instead of treating it specially. This is done by putting a backslash (\) before the character. You can also put single quotes (') around the whole thing if there are multiple metacharacters in the filename. (though this won't work right if there is also a single quote in the filename)

Here are a few examples:

% ll
-rw-r--r--  1 barr           53 May 17 14:31 *.log
-rw-r--r--  1 barr           45 May 17 14:33 ;asldfjasd;lkasj;dflaj
-rw-r--r--  1 barr        23413 May 17 14:41 a:\docs\thesis.wp
-rw-r--r--  1 barr        45390 May 17 14:31 save.$
-rw-r--r--  1 barr        42304 May 17 14:31 ~thesis.txt
% rm \*.log
% rm ';asldfjasd;lkasj;dflaj'
% rm 'a:\docs\thesis.wp'
% rm save.\$
% rm \~thesis.txt
%

Non-printable characters

Sometimes you'll get some non-printble characters appear in your filename. (they are often backspace or delete characters) When you view the directory listing the non-printable characters will be replaced by question mark (?) characters. You can tell that the character really isn't a question mark when you go and try to delete it:
% ll
-rw-r--r--  1 barr       687534 May 17 14:59 g?thesis
% rm g\?thesis
rm: g?thesis: No such file or directory
%
In this case, the question mark is a convenient reminder, since the question mark is also a wildcard which matches any single single character. But watch out, because the wildcard may match another file in the directory, say gothesis. To make sure you're not deleting files that you don't want to, use the -i option to rm. This will remove files interactively, asking you whether to delete each file in turn before it actually deletes them.
% ll
-rw-r--r--  1 barr       683534 May 17 14:56 gothesis
-rw-r--r--  1 barr       687534 May 17 14:59 g?thesis
% rm -i g?thesis
rm: remove gothesis? n
rm: remove gthesis? y
%

When all else fails...

you can use this last trick to remove files interactively. Just remember to use -i!!.
% rm -i *
rm: bigfile.log? n
rm: bigfile.lst? n
rm: bigfile.sas? n
rm: remove gob&ld$e@go???k? y
rm: thesis.wp? ^C
%
After you get prompted for the file you wanted to delete, you can type control-C to abort the rest of the command.


Click Here To Return To Main File Management Page

Last modified: 10/26/01 | Contact Webmaster
Privacy and Legal Statements | Copyright Information | ©Copyright 2006, The Pennsylvania State University |