In Linux, at times certain files are created with some special characters like hypen(-), single-quotes, blank character etc. It can happen either by accident or by some applications. Deleting those files would be difficult using regular 'rm' command options.
Hereby am describing certain methods to get rid of those files. For illustration purpose, I have taken 3 files with above mentioned special characters as example:
$ ls -ltr | tail -3
-rw-r--r-- 1 root root 0 Dec 4 05:30 file a
-rw-r--r-- 1 root root 0 Dec 4 05:31 '-fileb'
-rw-r--r-- 1 root root 1070 Dec 4 05:32 -filec
$ rm file a
rm: cannot lstat `file': No such file or directory
rm: cannot lstat `a': No such file or directory
$ rm '-filec'
rm: invalid option -- l
Try `rm ./-filec' to remove the file `-filec'.
Try `rm --help' for more information.
$ rm -filec
rm: invalid option -- l
Try `rm ./-filec' to remove the file `-filec'.
Try `rm --help' for more information.
Method 1 : Delete using inodes
$ ls -litr | tail -3
1511929 -rw-r--r-- 1 root root 0 Dec 4 05:30 file a
1511931 -rw-r--r-- 1 root root 0 Dec 4 05:31 '-fileb'
1511932 -rw-r--r-- 1 root root 1070 Dec 4 05:32 -filec
1511929 -rw-r--r-- 1 root root 0 Dec 4 05:30 file a
1511931 -rw-r--r-- 1 root root 0 Dec 4 05:31 '-fileb'
1511932 -rw-r--r-- 1 root root 1070 Dec 4 05:32 -filec
$ pwd
/opt
$ find /opt -inum 1511929 -exec rm -i {} \;
$ find /opt -inum 1511931 -exec rm -i {} \;
$ find /opt -inum 1511932 -exec rm -i {} \;
$ find /opt -inum 1511931 -exec rm -i {} \;
$ find /opt -inum 1511932 -exec rm -i {} \;
Others Methods: Using double-hypen "--" & double-quotes (depends on filenames)
$ rm -- -filec
rm: remove regular file `-filec'? y
$ rm -- file\ a
rm: remove regular empty file `file a'? y
$ file ./'-fileb'
./-fileb: ERROR: cannot open `./-fileb' (No such file or directory)
$ rm "'-fileb'"
rm: remove regular empty file `\'-fileb\''? y
$
No comments:
Post a Comment