Unixbhaskar's Blog

Friday, November 5, 2010

Let's deal with oddity : Remove filename with special char in them

Let's face it,everyone face some oddity sooner then later to deal with open system.I have had come across quite a lot over the decade and am going to share few in this article.

Sometime people forget the basic thing and make things complicated. Like when they create a simple file they put arcane character in the name and it will create havoc in later stage.For example if someone create a file with "--" or "#" or some other character in the filename.Say one has a filename like below:

bhaskar@bhaskar-laptop_18:35:12_Fri Nov 05:~> touch -- -hey

Now you cannot list it like below:

bhaskar@bhaskar-laptop_18:38:52_Fri Nov 05:~> ls -l -hey
ls: invalid option -- 'e'
Try `ls --help' for more information.


delete it like below;

bhaskar@bhaskar-laptop_18:39:00_Fri Nov 05:~> rm -f -hey
rm: invalid option -- 'h'
Try `rm ./-hey' to remove the file `-hey'.
Try `rm --help' for more information.

Now this is hindering you to get rid of it or even worse to take backup from the system if that file included in the backup list.So how to get rid of it we all know that file store all the information in get it..

bhaskar@bhaskar-laptop_18:42:19_Fri Nov 05:~> ls -i | grep hey
655109 -hey

Now you can get rid of that file by inode like below:

bhaskar@bhaskar-laptop_18:50:14_Fri Nov 05:~> find . -inum 655109 -exec rm -i {} \;
rm: remove regular empty file `./-hey'? Y

Hope this will help .


1 comment:

  1. Thanks Bhaskar

    Other ways we can think of is

    1)rm ./-hey (going to that dir where the file resists)

    2)rm -- -hey