LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 11-21-2011, 05:16 PM   #1
fm13
LQ Newbie
 
Registered: Nov 2011
Posts: 1

Rep: Reputation: Disabled
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?
 
Old 11-21-2011, 05:43 PM   #2
frieza
Senior Member
 
Registered: Feb 2002
Location: harvard, il
Distribution: Ubuntu 11.4,DD-WRT micro plus ssh,lfs-6.6,Fedora 15,Fedora 16
Posts: 3,233

Rep: Reputation: 406Reputation: 406Reputation: 406Reputation: 406Reputation: 406
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)
 
0 members found this post helpful.
Old 11-21-2011, 06:58 PM   #3
dive
Senior Member
 
Registered: Aug 2003
Location: UK
Distribution: Slackware
Posts: 3,467

Rep: Reputation: Disabled
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/
 
Old 11-22-2011, 08:21 AM   #4
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443
Blog Entries: 3

Rep: Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723
Quote:
Originally Posted by fm13 View Post
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.
 
Old 11-23-2011, 08:19 AM   #5
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
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

Last edited by David the H.; 11-23-2011 at 08:20 AM.
 
Old 11-23-2011, 12:12 PM   #6
DavidMcCann
LQ Veteran
 
Registered: Jul 2006
Location: London
Distribution: PCLinuxOS, Debian
Posts: 6,131

Rep: Reputation: 2302Reputation: 2302Reputation: 2302Reputation: 2302Reputation: 2302Reputation: 2302Reputation: 2302Reputation: 2302Reputation: 2302Reputation: 2302Reputation: 2302
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
 
1 members found this post helpful.
Old 11-23-2011, 10:30 PM   #7
RedNeck-LQ
Member
 
Registered: Jan 2011
Posts: 83

Rep: Reputation: 11
Quote:
Originally Posted by fm13 View Post
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/

Last edited by RedNeck-LQ; 11-23-2011 at 10:31 PM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
can not create directory brstek Linux - Desktop 13 09-26-2011 12:56 AM
Create a directory named like his Parent Directory sina_saeedi82 Linux - Newbie 8 05-26-2011 11:36 AM
cp: cannot create directory mghorbani Linux - General 14 07-21-2009 09:25 AM
mkdir : cannot create directory : no such file or directory patcheezy Linux - Newbie 6 05-13-2009 11:26 AM
can't create new directory dummoi Linux - Newbie 4 06-24-2004 02:50 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 06:51 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration