LinuxQuestions.org
Latest LQ Deal: Complete CCNA, CCNP & Red Hat Certification Training Bundle
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, 11: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, 11: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 11:30 AM.
 
Old 05-29-2008, 12:01 PM   #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 05:39 AM.
 
Old 05-29-2008, 08:18 PM   #4
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.8, Centos 5.10
Posts: 17,240

Rep: Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324
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, 08:46 PM   #5
i92guboj
Gentoo support team
 
Registered: May 2008
Location: Lucena, Córdoba (Spain)
Distribution: Gentoo
Posts: 4,063

Rep: Reputation: 381Reputation: 381Reputation: 381Reputation: 381
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, 09:38 PM   #6
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.8, Centos 5.10
Posts: 17,240

Rep: Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324
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-30-2008, 12:38 AM   #7
i92guboj
Gentoo support team
 
Registered: May 2008
Location: Lucena, Córdoba (Spain)
Distribution: Gentoo
Posts: 4,063

Rep: Reputation: 381Reputation: 381Reputation: 381Reputation: 381
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-30-2008 at 12:41 AM.
 
Old 06-07-2008, 10: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: 233Reputation: 233Reputation: 233
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


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 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 07:24 AM
Shell Scripting: Getting a pid and killing it via a shell script topcat Programming 15 10-28-2007 03:14 AM
I made a shortcut to a shell script and it is using default shell icon... shlinux Linux - Software 2 04-20-2006 07:29 AM
Alias or shell script to confirm 'exit' commands from a shell rose_bud4201 Programming 2 03-08-2006 03:34 PM
shell script problem, want to use shell script auto update IP~! singying304 Programming 4 11-29-2005 06:32 PM


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

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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration