LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - General (https://www.linuxquestions.org/questions/linux-general-1/)
-   -   Is it possible to create a directory with the name -1 in Debian (https://www.linuxquestions.org/questions/linux-general-1/is-it-possible-to-create-a-directory-with-the-name-1-in-debian-914788/)

fm13 11-21-2011 05:16 PM

Is it possible to create a directory with the name -1 in Debian
 
I've tried:
mkdir '-1'
mkdir \-1

to no avail. Is it possible to create a directory beginning with the negative sign followed by a digit?

frieza 11-21-2011 05:43 PM

i would say probably not, (at least not with the command line) with good reason, the - symbol is parsed to mean that the character following is a flag or argument, thus creating a file name that begins with (or contains a space followed by a -, such as '1 -2') would be mis-parsed by the shell, creating all sorts of problems, that being said you probably could do so in a graphical file manager, but i wouldn't recommend doing so,

imho this seems a bit like asking why you can't use a '/' in a file name (in case you are wondering, / is a directory separator and again would be mis-parsed)

dive 11-21-2011 06:58 PM

Yes it is possible - you need to add ./ in front of it:

Code:

[00:57:56](david@BlackSun)(~/xxxx)% mkdir ./-1
[00:58:00](david@BlackSun)(~/xxxx)% ls
-1/


MTK358 11-22-2011 08:21 AM

Quote:

Originally Posted by fm13 (Post 4530129)
I've tried:
mkdir '-1'
mkdir \-1

Quoting won't work because it's the mkdir command that parses "-" options, not the shell.

But anyway, dive's solution is correct. "." means the current directory, so prepending it to absolute paths doesn't change them. It's actually a good idea to use "./" inside scripts, in case filenames start with a character that a command might interpret as an option.

David the H. 11-23-2011 08:19 AM

As mentioned, the problem is that "-" indicates an option to most commands. Quoting doesn't any good because they are removed by the shell before the command is executed, and the raw value is what's passed to it.

What you need is some way to tell the command that it's not an option, but an argument. Prefixing it with an explicit pathname is one way. Another is that most commands accept a "--" option, which tells it that there are no more options to process. Everything after that is to be treated as an argument. Some commands like grep and sed also have other options that can be used as well (-e in this case).

http://mywiki.wooledge.org/BashPitfa...leading_dashes

DavidMcCann 11-23-2011 12:12 PM

Why do it? You may force Linux to create a directory or file with an odd name, but there will always be the risk of things behaving badly later on. The best practice is
- start names with a letter or number
- only include letters, numbers, underscore, hyphen, and dot

RedNeck-LQ 11-23-2011 10:30 PM

Quote:

Originally Posted by fm13 (Post 4530129)
I've tried:
mkdir '-1'
mkdir \-1

to no avail. Is it possible to create a directory beginning with the negative sign followed by a digit?

You can create a -1 directory with this

Code:

mkdir -- -1
A double dash (--) is used in bash built-in commands and many other commands to signify the end of command options, after which only positional parameters are accepted.

Example: lets say you want to grep a file for the string "-v" normally "-v" will be considered the option to reverse the matching pattern, but with -- you can grep for "-v" string like this:

grep -- -v file

Example 2

to remove -1 direcory

rmdir -- -1/


All times are GMT -5. The time now is 02:46 PM.