LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   shell script (https://www.linuxquestions.org/questions/linux-newbie-8/shell-script-645596/)

jdeeptir 05-29-2008 10:10 AM

shell script
 
Dear all

how can i create a folder of name "New Folder" using shell script.
I tried "mkdir /mnt/ide4/New\ Folder"


But i get only folder as New created not New Folder

Please help.

With Regards,
deepti

b0uncer 05-29-2008 10:27 AM

Well either of these should work
Code:

mkdir New\ Folder
mkdir "New Folder"

And likewise
Code:

mkdir some/other/dir/New\ Folder
mkdir "some/other/dir/New Folder"

If you don't use (double-)quotation marks, you need to use the escape character \ to escape the whitespace, but it's often easier to enclose the whole filename with (double)quotes.

EDIT: please edit your profile information and add there the operating system/distribution you are using; this helps answering your possibly distributio-specific questions.

seraphim172 05-29-2008 11:01 AM

You can also use the -p option with mkdir to create all interim directories. Using your example /mnt is likely to exist, but if /mnt/ide4 has not been created yet, then it will be created before "New Folder". This is useful in shell scripts, where directory structures can change among sessions.

Linux Archive

chrism01 05-29-2008 07:18 PM

Just FYI, many Linux cmds have problems with spaces in dirnames/filenames, unless you take special precautions, so don't do that (if possible).
It'll save you a lot of grief later.
As an example, <space> is often used as the delimiter between parameters.

i92guboj 05-29-2008 07:46 PM

Quote:

Originally Posted by chrism01 (Post 3168713)
Just FYI, many Linux cmds have problems with spaces in dirnames/filenames, unless you take special precautions, so don't do that (if possible).

False. They handle it as well as it can be done.

So, how do you propose that this should be handled? Spaces are always interpreted as separators for the different arguments of a given command, unless they are quoted. It's not only the correct and easiest way to do it, it's also the way it's been for ages. It works ok as long as you know how to use it.

Just learn to use the escape backslash or quote the thing adequately, windows is the same on that regards, except for the fact that windows users never use the command line so they don't notice (or they use the stupid filena~1.ext DOS-like form, which is one of the dumbest things ever).

chrism01 05-29-2008 08:38 PM

As I said "unless you take special precautions," ie you have to use backslash and or use quotes everywhere.
It just gets messy, esp in one liners/nested cmds etc.
If you look around, you'll find most scripts (not just on LQ) use the default mode ie assume no spaces in filenames.
I never said Unix/Linux can't do it, it can. Its just more work.

i92guboj 05-29-2008 11:38 PM

Quote:

Originally Posted by chrism01 (Post 3168770)
As I said "unless you take special precautions," ie you have to use backslash and or use quotes everywhere.
It just gets messy, esp in one liners/nested cmds etc.
If you look around, you'll find most scripts (not just on LQ) use the default mode ie assume no spaces in filenames.
I never said Unix/Linux can't do it, it can. Its just more work.

If you search a bit, you will find thousands of topics on the net advising you to do so. ALL the variables should be quoted to avoid problems. It's just common sense. You could forbid spaces in file names... It's the only alternative: to go back to the DOS naming scheme.

I don't want to start a discussion, I just wanted to say that this advice:

Quote:

always quote your variables and filenames unless you are certain that they don't have any special character on them
Is much better and more practical than this other one:

Quote:

don't use space on your filenames
In a modern system where we see lots even thousands of files every day with spaces on them.

archtoad6 06-07-2008 09:21 AM

OP,
ALL of the following worked on my MEPIS 6.0 system:
Code:

mkdir -p t/new\ folder1
mkdir -p t/'new folder2'
mkdir -p "t/new folder3"

cd t
mkdir    new\ folder4
mkdir    'new folder5'
mkdir    "new folder6"

cd ..
ls t |less

I wonder if the lack of the "-p" option is the cause of your problem.


Yes, please do ...
Quote:

Originally Posted by b0uncer (Post 3168229)
...edit your profile information and add there the operating system/distribution you are using; this helps answering your possibly distributio-specific questions.


Spaces_in_filenames ;)
I am a latecomer to *nix, so I can't speak w/ the authority I'd like; but I believe the old Unix way is to use underscores instead of spaces.

Yes, there are ways around the problems of handling spaces, but life is so much easier if you don't have to bother. I have even written code to scan such spaces & automatically replace them -- it was especially useful for (directories full of) .mp3's.


All times are GMT -5. The time now is 12:13 AM.