LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - General (https://www.linuxquestions.org/questions/linux-general-1/)
-   -   File called "*" in home directory. (https://www.linuxquestions.org/questions/linux-general-1/file-called-%2A-in-home-directory-593639/)

krnlg 10-22-2007 06:23 AM

File called "*" in home directory.
 
Ok so, a few minutes ago I looked in my home directory and noticed there was an empty file called '*' in it. I assumed I'd somehow created it with an incorrect use of the 'touch' command or something, and so like an idiot I opened a terminal and typed 'rm * <enter>'.

Lovely, some deleted files there (but nothing important, pretty much everything is in subdirs). This made me think, well how could I ever have created a file called just asterisk anyway - if I try to 'touch' it, I need to enclose it in quotes for it to work.

Also I've looked through my bash histories both for my normal user and for root, and there is nothing at all that could've made this file.

So my question is: does anyone know of a software bug that could do this, or is it possible, given that I'm hardly inexperienced when it comes to the command line and I still managed to erase a directory because of a wildcard filename (I know, you laugh but I cry :/), could it be some kind of subtle "virus" - probably the wrong term really but you know what I mean; after all, it was a file called *, nicely placed in ~/
All I've been doing recently is attempting to transcode some video files, I mean I've installed software, etc. but nothing weird and probably nothing outside of the debian testing tree..

if i'm being paranoid, let me know ;)

Nick_Battle 10-22-2007 07:08 AM

I've not tried it, but I suspect most programs that prompt for a filename (eg. to save something) would create a file called "*" if you typed that into the dialog and clicked OK. Is it possible you were struggling to see "all files" at some point, entered a "*" into a dialog (or failed to replace one already there?), clicked OK and then had the program create a file called "*"?

Was the file empty?

At least it wasn't called "-rf /" :-)

farslayer 10-22-2007 08:26 AM

you can touch * without quotes if the directory has no other files/folders in it.. it will create the file you describe.
touch *

if there are other files in the directory the asterisk would need to be in quotes.

you can also remove the asterisk file specifically without deleting the other files in the directory by enclosing the asterisk in quotes

rm '*'

No clue how you ended up with that file, but I had to create a directory and play around with it after you mentioned it :)

and because it was just to hard to pass up I had to try Nicks twisted little suggestion as well... Glad to see that it failed.. heh

Nick_Battle 10-23-2007 06:30 AM

Quote:

Originally Posted by farslayer (Post 2932520)
and because it was just to hard to pass up I had to try Nicks twisted little suggestion as well... Glad to see that it failed.. heh

:-)

I tried typing "*" into the Save As dialog entry for a handful of editors last night, and they all created a file called "*". This is what I was thinking of, though perhaps it's unlikely to be the explanation.

What did you try that failed to create a file?

Cheers,
-nick

indiancosmonaut 10-23-2007 01:01 PM

Hi,

Could you help me with this similar problem :-)

How do i remove -.bak file!

I even tried rm command with the -rf option rm -rf '\-.bak', but its not working...

The output is:

$ rm -rf '-.bak'
rm: illegal option -- .
rm: illegal option -- b
rm: illegal option -- a
rm: illegal option -- k
usage: rm [-fiRr] file ...

Kind regards,

indiancosmonaut

kromberg 10-23-2007 03:03 PM

run this script:

Code:

#!/usr/bin/python
import os
os.remove( "-.bak" )

Problem solved. You can use python to remove all kinds of files with funky names.

Keith

indiancosmonaut 10-24-2007 01:46 AM

Hi kromberg,

Thanks for the reply. I have never used python but will look into it.
Currently python is not present on my solaris training box.

Can shell do it?

Kind regards,

indiancosmonaut

jay73 10-24-2007 01:55 AM

rm -rf -- -.bak should do it too.

Disillusionist 10-24-2007 01:58 AM

you could try using the -i option for rm:

rm -i ?.bak

or

rm -i *.bak

The first would try to remove a file containing a single character followed by .bak

The second would try to remove all files ending in .bak

But the commands would ask you before making the attempt.

Therefore just say no to the files you don't want to delete.

indiancosmonaut 10-24-2007 11:30 AM

Hi Jay

It worked!

Code:

$ rm -rf -- -.bak
$ ls -ltr | grep -- "-.bak"
$

And I have another doubt!

1. Kindly explain the command a little (the use of -- )
2. When i tried to grep it like this...

Code:

$ ls -ltr | grep "-.bak"
grep: illegal option -- .
grep: illegal option -- a
grep: illegal option -- k
Usage: grep -hblcnsviw pattern file . . .

It didn't work :-(

But this worked...

Code:

$ ls -ltr | grep -- "-.bak"
$

Please let me know of the reason.

Kind regards,

indiancosmonaut

jay73 10-24-2007 02:17 PM

That is because bash is only so smart. When it sees a hyphen, it assumes that what follows is an option to the command (think of ls -l). Of course, the hyphen in your file name is not so you need to find a way to tell it that isn't. This is where the double hyphens come in handy.
The double hyphens normally introduce a long option. You can write ls -l but also ls --list, which is just the full name of the option. Now, long options are by definition the last thing that precedes a filename. Once bash discovers a double hyphen it stops interpreting any following single hyphens as options and it takes them literally, as parts of a filename. It doesn't matter whether your double hyphens are actually accompanied by a long option or whether they are just that, double hyphens without an option. You can write ls -a --list but it would be incorrect to write ls --list -a. Or rather, because -- signals the end of the list of options, bash would start looking in your directory for a file named -a, which most likely doesn't exist at all.
As an alternative, you could also use ./. This again signals that what follows should be taken literally. rm -rf ./-.bak should work just as well as the trick with the double hyphens because ./ by definition introduces a filename, not an option.
I hope this makes sense. Just let me know if it doesn't.

farslayer 10-24-2007 02:49 PM

Glad I've kept following this thread.. Learned something new today, I would not have thought to use -- by itself. :)

thanks for the tip jay73 !!

indiancosmonaut 10-25-2007 01:08 AM

Thanks for this very useful tip Jay.
And Yes, It did make sense :-)

rm -rf ./-.bak worked fine as well.

Just to add, this also works in ksh.
Probably this is the case with all the shells?

Code:

$ ksh
$ echo UNIX > -.bak
$ ls -l -.bak
ls: illegal option -- .
ls: illegal option -- k
usage: ls -1RaAdCxmnlhogrtucpFbqisfL@ [files]
$ ls -l -- -.bak
-rw-r--r--  1 training other          5 Oct 25 11:37 -.bak
$ ls -l ./-.bak
-rw-r--r--  1 training other          5 Oct 25 11:37 ./-.bak
$ rm -rf ./-.bak
$ ls -l ./-.bak
./-.bak: No such file or directory
$

Thanks again!

Kind regards,

indiancosmonaut

checkmate3001 10-25-2007 01:17 AM

Wow...

You guys are crafty... I can see I have a lot of stuff to learn yet (don't we always).

Thanks!

Disillusionist 10-25-2007 01:34 AM

I'd also like to thank Jay73 for the useful explanation of the -- option.

I would however advise against using -rf as a default option with rm when all I want to do is remove 1 file

-rf after all means recursive delete (delete any file/folder matching criteria and all files/folders below it) and force the delete (dont raise any issues just do it)

The following are safer commands:
rm -- -.bak
rm ./-.bak

I would only use -r when I need to remove folders that are not empty.

If -.bak is an empty folder instead of a file:
rmdir -- -.bak
rmdir ./-.bak


All times are GMT -5. The time now is 04:16 AM.