How to Deal With a Filename That Starts With the Hyphen (-) Character

How do I deal with a filename that starts with the hyphen (-) character?

You can refer to it either using ./-filename or some command will allow you to put it after double dash:

rm -- -filename

How to remove a file beginning with a dash (in Unix like mode)

Use a double hyphen (--) to stop flag parsing:

rm -- -\ -l

Alternatively, you can even use tab completion:

rm -- \- # Then press TAB immediately after typing the final -

This assumes that's the only file with a leading hyphen in the directory. Bash is smart enough to automatically escape the space in the middle of the file name.

if single-character filename exists within a string enclosed in [ ] entire string gets replaced with the character

It is not a bug.

Characters in square brackets is the part of glob matching. Try:

cd /
echo [abcd]*

And if there is no files matched to the given mask then the mask printed as is.

So you need to quote your values:

mkdir /tmp/temp123
cd /tmp/temp123
echo "[123abc]"
touch a
echo "[123abc]"

How to open a - dashed filename using terminal?

This type of approach has a lot of misunderstanding because using - as an argument refers to STDIN/STDOUT i.e dev/stdin or dev/stdout .So if you want to open this type of file you have to specify the full location of the file such as ./- .For eg. , if you want to see what is in that file use cat ./-

Using dash character in main source file name

In short - if you do not give the module a name, the D compiler will use the file-name (without extension) as module-name.

So, if you name your D source file hello-world.d, and it does not have something like module hello_world; at the top of the source-file, the compiler will actually try to insert and compile module hello-world; and that will fail because hello-world is not a valid identifier.

If the file name was hworld.d for an example, then the compiler will actually compile a module with auto-generated name hworld.

Including a hyphen in a regex character bracket?

Escaping using \- should be fine, but you can also try putting it at the beginning or the end of the character class. This should work for you:

/^[a-zA-Z0-9._-]+$/

How can I grep for a string that begins with a dash/hyphen?

Use:

grep -- -X

Documentation

Related: What does a bare double dash mean? (thanks to nutty about natty).

Delete dir with hyphen in name

You could do:

rm ./-p

And depending on the rm used:

rm -- -p


Related Topics



Leave a reply



Submit