Difference between revisions of "Removing files with weird names"

From Noah.org
Jump to navigationJump to search
m
m
Line 1: Line 1:
 
[[Category:Engineering]]
 
[[Category:Engineering]]
 
[[Category:Drives_and_Filesystems]]
 
[[Category:Drives_and_Filesystems]]
In a shell run this command:
+
If you have a filename that looks like the options to a command or if the filename has weird ASCII characters then you may have trouble deleting the file because you cannot easily type the filename.
  
 +
The codes that people embed can be insidious because they can contain normal printable characters that will cause problems if you try to type the filename directly. Be careful with the following example. If you don't pay attention you could delete a lot of files. '''IMPORTANT: Don't try to remove the resulting file without reading the rest of this!''' The following is a trick to play on someone:
 
<pre>
 
<pre>
 
touch -- -rf
 
touch -- -rf
 
</pre>
 
</pre>
  
First thing, don't try to remove this file! Now ask yourself, "How do I delete it?".
+
Now list the file:
 +
<pre>
 +
$ ls -l -- -rf
 +
-rw-r--r-- 1 noah noah 0 Jun 14 14:13 -rf
 +
</pre>
  
Most GNU tools will accept '--' as an option. This tells the option parser to stop interpreting the arguments list as options. For example, to remove a file named "-rf" do this:
+
How do you delete the '''-rf''' file? If you run `rm -rf` then this would do nothing (fortunately). It's possible to make the filename even more destructive. The solution to delete the '''-rf''' file is to give `rm` a hint that this string is no an option. Most GNU tools will accept '''--''' as a final option. This tells the option parser to stop interpreting the arguments following '''--''' as more options. It will only treat them as strings. For example, to remove a file named "-rf" do this:
  
 
<pre>
 
<pre>
 
rm -- -rf
 
rm -- -rf
 
</pre>
 
</pre>

Revision as of 14:38, 14 June 2011

If you have a filename that looks like the options to a command or if the filename has weird ASCII characters then you may have trouble deleting the file because you cannot easily type the filename.

The codes that people embed can be insidious because they can contain normal printable characters that will cause problems if you try to type the filename directly. Be careful with the following example. If you don't pay attention you could delete a lot of files. IMPORTANT: Don't try to remove the resulting file without reading the rest of this! The following is a trick to play on someone:

touch -- -rf

Now list the file:

$ ls -l -- -rf
-rw-r--r-- 1 noah noah 0 Jun 14 14:13 -rf

How do you delete the -rf file? If you run `rm -rf` then this would do nothing (fortunately). It's possible to make the filename even more destructive. The solution to delete the -rf file is to give `rm` a hint that this string is no an option. Most GNU tools will accept -- as a final option. This tells the option parser to stop interpreting the arguments following -- as more options. It will only treat them as strings. For example, to remove a file named "-rf" do this:

rm -- -rf