Wednesday, July 1, 2009

Removing Files With Unusual Characters

Suppose, some one (miscreant) got access to your computer and
creates files, which you may not be able to delete or you will end
up deleting all the files/subdirectories of the directory on which
these files are created.

Some examples of such dangerous file names are:
$ ls -l
total 0K
-rw-r--r-- 1 mitesh users 0 2009-07-01 19:19 *
-rw-r--r-- 1 mitesh users 0 2009-07-01 19:20 -rf *
-rw-r--r-- 1 mitesh users 0 2009-07-01 19:27 -rw-r--r--
-rw-r--r-- 1 mitesh users 0 2009-07-01 19:19 test

Now, how are you going to delete above files?

(1) * : If one tries
$ rm *
or
$ rm -r *
This will delete all the files(& all subdirectories) in present working
directory.
Solution:
$ rm -f ./'*'
We can verify this.
$ ls -l
total 0K
-rw-r--r-- 1 mitesh users 0 2009-07-01 19:20 -rf *
-rw-r--r-- 1 mitesh users 0 2009-07-01 19:27 -rw-r--r--
-rw-r--r-- 1 mitesh users 0 2009-07-01 19:19 test

(2) -rf *: If one tries
$ rm -rf *
This will delete all the files & all subdirectories in present working
directory.
Solution:
$ rm -f ./'*'
We can verify this.
$ ls -l
total 0K
-rw-r--r-- 1 mitesh users 0 2009-07-01 19:27 -rw-r--r--
-rw-r--r-- 1 mitesh users 0 2009-07-01 19:19 test

(3) -rw-r--r--: If one tries
$ rm -rw-r--r--
rm: invalid option -- w
Try `rm ./-rw-r--r--' to remove the file `-rw-r--r--'.
Try `rm --help' for more information.
Solution:
$ rm ./-rw-r--r--
Other Solution
$ rm -- -rw-r--r--

Note: So we need to be vigilant, while using 'rm' command.

No comments: