Unable to delete a file using cygwin shell

1

I'm trying to delete the file ~$bgka.mod from my current directory in Cygwin shell. When I list all my files in that directory it shows up:

$ ls
~$bgka.mod   CaBK.mod     hcdist.hoc    mcdist.hoc      pbc.hoc
500net       ccanl.mod    hcell.bcell   mcell.bcell     pgc.hoc
50knet.hoc   gcdist.hoc   hcell.gcell   mcell.gcell     phc.hoc
bcdist.hoc   gcell.bcell  hcell.hcell   mcell.hcell     pmc.hoc
bcell.bcell  gcell.gcell  hcell.mcell   mcell.mcell     ppsyn.mod
bcell.gcell  gcell.hcell  hyperde3.mod  mod_func.c      README.html
bcell.hcell  gcell.mcell  ichan2.mod    mosinit.hoc     run50knet.bash
bcell.mcell  Gfluct2.mod  inhsyn.mod    nca.mod         screenshot.jpg
bgka.mod     gskch.mod    LcaMig.mod    parameters.dat  tca.mod

when I try rm ~$bgka.mod I get the following error:

$ rm ~$bgka.mod
rm: cannot remove ‘~.mod’: No such file or directory

Also, I have tried deleting the file from Windows Explorer and Windows cmd.exe but it doesn't show up in either of these windows.

How can I delete it?

kfolw

Posted 2015-02-18T21:58:41.743

Reputation: 13

Do you see any of your Cygwin files in Windows (Explorer or cmd)? Try looking in C:\cygwin\home\kfolw (if that's your actual login name). The top-level directory might be something like C:\cygwin64 instead ofC:\cygwin. – G-Man Says 'Reinstate Monica' – 2015-02-19T22:36:08.193

Answers

2

I'm trying to delete the file ~$bgka.mod from my current directory

~ and $ are special characters in bash.

You can either escape them using \ or put the argument in single quotes '.

Double quotes cannot be used as "Enclosing characters in double quotes " preserves the literal value of all characters within the quotes, with the exception of $, `, and \"

Use:

rm \~\$bgka.mod

Or:

rm '~$bgka.mod'

Tilde Expansion

If a word begins with an unquoted tilde character ~, all of the characters up to the first unquoted slash (or all characters, if there is no unquoted slash) are considered a tilde-prefix. If none of the characters in the tilde-prefix are quoted, the characters in the tilde-prefix following the tilde are treated as a possible login name. If this login name is the null string, the tilde is replaced with the value of the HOME shell variable. If HOME is unset, the home directory of the user executing the shell is substituted instead. Otherwise, the tilde-prefix is replaced with the home directory associated with the specified login name.

Source Shell Expansion


Shell Parameter Expansion

The $ character introduces parameter expansion, command substitution, or arithmetic expansion. The parameter name or symbol to be expanded may be enclosed in braces, which are optional but serve to protect the variable to be expanded from characters immediately following it which could be interpreted as part of the name.

Source Shell Expansion


Quoting

Quoting is used to remove the special meaning of certain characters or words to the shell. Quoting can be used to disable special treatment for special characters, to prevent reserved words from being recognized as such, and to prevent parameter expansion.

Each of the shell metacharacters has special meaning to the shell and must be quoted if it is to represent itself.

Escape Character

A non-quoted backslash \ is the Bash escape character. It preserves the literal value of the next character that follows, with the exception of newline. If a \newline pair appears, and the backslash itself is not quoted, the \newline is treated as a line continuation (that is, it is removed from the input stream and effectively ignored).

Single Quotes

Enclosing characters in single quotes ' preserves the literal value of each character within the quotes. A single quote may not occur between single quotes, even when preceded by a backslash.

Double Quotes

Enclosing characters in double quotes " preserves the literal value of all characters within the quotes, with the exception of $, `, and \. The characters $ and ` retain their special meaning within double quotes. The backslash retains its special meaning only when followed by one of the following characters: $, `, ", \, or newline. Within double quotes, backslashes that are followed by one of these characters are removed. Backslashes preceding characters without a special meaning are left unmodified. A double quote may be quoted within double quotes by preceding it with a backslash.

Source Quoting:


Further Reading

DavidPostill

Posted 2015-02-18T21:58:41.743

Reputation: 118 938

2

You need to quote the file name:

rm '~$bgka.mod'

Discussion

Without quotes, the shell thinks that $bgka is a shell variable and it substitutes in its current value. Since bgka hasn't been assigned to anything, it substitutes in the empty string. As as a result, the shell tries to delete the file named ~.mod. That file doesn't exist. That is why you receive the error:

rm: cannot remove ‘~.mod’: No such file or directory

Enclosing the file name in single quotes solves this because it tells the shell not to do any substitutions.

You can see the difference between single quotes and no quotes by using a simple echo statement:

$ echo ~$bgka.mod '~$bgka.mod'
~.mod ~$bgka.mod

John1024

Posted 2015-02-18T21:58:41.743

Reputation: 13 893

1

The above answers are very detailed and correct. A useful trick in dealing with file names containing special characters is the 'find' command.

find -name "*bgka*" -exec rm {} \;

If you can find a pattern to match in the name portion, this is the easiest way to handle files containing special characters.

Daniel

Posted 2015-02-18T21:58:41.743

Reputation: 932