LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 05-29-2008, 10:10 AM   #1
jdeeptir
LQ Newbie
 
Registered: Mar 2008
Posts: 7

Rep: Reputation: 0
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
 
Old 05-29-2008, 10:27 AM   #2
b0uncer
LQ Guru
 
Registered: Aug 2003
Distribution: CentOS, OS X
Posts: 5,131

Rep: Reputation: Disabled
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.

Last edited by b0uncer; 05-29-2008 at 10:30 AM.
 
Old 05-29-2008, 11:01 AM   #3
seraphim172
Member
 
Registered: May 2008
Posts: 101

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

Last edited by seraphim172; 06-25-2008 at 04:39 AM.
 
Old 05-29-2008, 07:18 PM   #4
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,359

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
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.
 
Old 05-29-2008, 07:46 PM   #5
i92guboj
Gentoo support team
 
Registered: May 2008
Location: Lucena, Córdoba (Spain)
Distribution: Gentoo
Posts: 4,083

Rep: Reputation: 405Reputation: 405Reputation: 405Reputation: 405Reputation: 405
Quote:
Originally Posted by chrism01 View Post
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).
 
Old 05-29-2008, 08:38 PM   #6
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,359

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
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.
 
Old 05-29-2008, 11:38 PM   #7
i92guboj
Gentoo support team
 
Registered: May 2008
Location: Lucena, Córdoba (Spain)
Distribution: Gentoo
Posts: 4,083

Rep: Reputation: 405Reputation: 405Reputation: 405Reputation: 405Reputation: 405
Quote:
Originally Posted by chrism01 View Post
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.

Last edited by i92guboj; 05-29-2008 at 11:41 PM.
 
Old 06-07-2008, 09:21 AM   #8
archtoad6
Senior Member
 
Registered: Oct 2004
Location: Houston, TX (usa)
Distribution: MEPIS, Debian, Knoppix,
Posts: 4,727
Blog Entries: 15

Rep: Reputation: 234Reputation: 234Reputation: 234
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 View Post
...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.
 
  


Reply



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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
How to ssh from a shell script ? For ppl who can write shell scripts. thefountainhead100 Programming 14 10-22-2008 06:24 AM
Shell Scripting: Getting a pid and killing it via a shell script topcat Programming 15 10-28-2007 02:14 AM
I made a shortcut to a shell script and it is using default shell icon... shlinux Linux - Software 2 04-20-2006 06:29 AM
Alias or shell script to confirm 'exit' commands from a shell rose_bud4201 Programming 2 03-08-2006 02:34 PM
shell script problem, want to use shell script auto update IP~! singying304 Programming 4 11-29-2005 05:32 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 05:45 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