Need help in creating directory


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Need help in creating directory
# 1  
Old 07-19-2011
Need help in creating directory

Hello Experts

let say we got a line a file named

Code:
/user/Oracle/my_catalog/default/root/webcat+backup+testing+07192011.atr

now i have to create a directory with name

Code:
webcat backup testing 07192011

by removing + and removing .atr at the given path...can it be possible

Please let me know your advice.

Thanks in advance

Smilie

Last edited by Yogesh Sawant; 07-22-2011 at 06:27 AM.. Reason: added code tags
# 2  
Old 07-19-2011
Code:
mkdir "$(echo "/user/Oracle/my_catalog/default/root/webcat+backup+testing+07192011.atr" |\
 awk -F\| '{gsub(/(+)/," ",$NF);gsub(/\.atr$/,""); print}')"

This User Gave Thanks to zaxxon For This Post:
# 3  
Old 07-19-2011
Code:
file_path=" /user/Oracle/my_catalog/default/root/webcat+backup+testing+07192011.atr"
dir_name=`echo $file_path | awk -F"[.+]" '{print $NF-1}`
mkdir $dir_name

This User Gave Thanks to itkamaraj For This Post:
# 4  
Old 07-19-2011
In order to prevent the spaces in the new directory being seen as token separators when making the directory you'll have to either quote the new directory name or escape the spaces.
eg:
Code:
file="/user/Oracle/my_catalog/default/root/webcat+backup+testing+07192011.atr"
# escaping
basename $file|cut -d\. -f1|sed ' s/\([a-z0-9]\+\)+\+/\1\\ /g'
#quoting
echo \"$(basename $file|cut -d\. -f1|sed ' s/\([a-z0-9]\+\)+\+/\1 /g')\"

Though spaces in file paths is a shooting offence @ this site Smilie
# 5  
Old 07-19-2011
Quote:
Originally Posted by Skrynesaver
In order to prevent the spaces in the new directory being seen as token separators when making the directory you'll have to either quote the new directory name or escape the spaces.
eg:
Code:
file="/user/Oracle/my_catalog/default/root/webcat+backup+testing+07192011.atr"
# escaping
basename $file|cut -d\. -f1|sed ' s/\([a-z0-9]\+\)+\+/\1\\ /g'
#quoting
echo \"$(basename $file|cut -d\. -f1|sed ' s/\([a-z0-9]\+\)+\+/\1 /g')\"

Though spaces in file paths is a shooting offence @ this site Smilie
A minor nit: It doesn't hurt anything, but the -d option-argument in cut does not need to be backslash-quoted. The dot is not a shell metacharacter.

A major nit: Your echo suggestion will not protect anything. Since the double quotes are escaped, the results of the command substitution will undergo field splitting. You're just getting lucky because the sample data never yields more than one consecutive space character and that just happens to be what echo uses to delimit its arguments in its output. If the filename contained consecutive + characters, the field splitting would effectively squeeze the resulting consecutive spaces into one.

If your intention with that suggestion is to output a quoted string with the correct number of spaces, then you need to use a set of unescaped quotes outside the backslash-escaped quotes. This additional set of quotes is needed for another reason as well: the shell performs pathname expansion (file globbing) after command substitution and field splitting. Should the filename contain filename pattern metacharacters (*, ?, [...]), and should that pattern match something, the result would be something unintended. A long shot, perhaps, but still, a window for error.

A less error-prone approach would be to simply stuff the command substitution's output into a variable (variable assignment does not undergo field splitting nor filename expansion).

Your sed invocation is needlessly complex. You could just use tr + ' ' or sed 'y/+/ /' to do that job.

To further simplify, basename and cut could be eliminated as well, with their functionality folded into a single sed:
Code:
dir=$(echo "$file" | sed 's/.*\///; s/\.atr$//; y/+/ /')
mkdir "$dir"

If posix-compliancy/portability is not a necessity, and if a modern shell is available, then the most efficient solution would be to leverage that shell's built-in features. The following should work with contemporary korn and bash shells (and perhaps others):

Code:
dir=${file##*/}       # delete directories (i.e. basename)
dir=${dir%.*}         # delete file extension
dir=${dir//+/ }       # replace all pluses with spaces
mkdir "$dir"

With regard to shooting someone over spaces in filenames, I reserve my bullets for tabs and newlines in filenames. Since UNIX and its userland is built around the text format which uses \t as the field/column delimiter and \n as the record/line delimiter, any file naming convention which uses them is truly and profoundly braindamaged. Such filenames make it impossible to use the plethora of text-oriented utilities to process filenames/pathnames. Spaces? Not so much. In my opinion, they're no big deal.

Regards,
Alister

---------- Post updated at 11:07 AM ---------- Previous update was at 10:57 AM ----------

Quote:
Originally Posted by Skrynesaver
Code:
sed ' s/\([a-z0-9]\+\)+\+/\1 /g'

Hmmm. I probably should have taken a closer look at the sed before I wrote the above. If I'm understanding it correctly (this seems to be GNU sed and I can't test it since none of my sed's support the \+ sequence), this already squeezes multiple pluses into a single space (nothing in the original post indicates that this is desirable, but perhaps it is). If that's the case, then field splitting won't affect the spaces inserted by sed. However, I'll leave my post intact as it does contain some useful information even if most of it mistakenly assumes that your sed could generate multiple spaces from multiple pluses.

Woops and regards,
Alister Smilie

Last edited by alister; 07-19-2011 at 12:16 PM..
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

creating file and it directory

hello, is there any command that i can use to create a file and also it directory (if not exist)? example: <command> /home/admin/testdir/test1.txt desc: this will create test1.txt under testdir directory. but if testdir is not exist it will be created automatically. thank you... (3 Replies)
Discussion started by: makan
3 Replies

2. Shell Programming and Scripting

creating directory from scripts

Dear All, I have a shell scripts which create a directory and perform moving some files, when the script is kept where it is creating directory then it runs fine , but when the scripts is run where it is supposed to be which is different location then where i am creating directory , scripts... (2 Replies)
Discussion started by: guddu_12
2 Replies

3. UNIX for Dummies Questions & Answers

Creating directory with specific size?

Hello world, I just learnt we can create a directory with custom size in a Linux server (say Redhat). Is it true? I'm asking because the only data (I can think of) a directory's inode holds is the files and 'sub-dir's. How can a new empty directory be of some required size? :wall: PS : In... (2 Replies)
Discussion started by: satish51392111
2 Replies

4. UNIX for Dummies Questions & Answers

creating directory in unix

Hi, If i create a directory like with the command ... ~/.ssh so where this folder will be get created in side my home directory ...? what (~) represents here..?:confused: (4 Replies)
Discussion started by: rahul125
4 Replies

5. UNIX for Dummies Questions & Answers

Error while creating directory

Hello Experts while read dirlist do mkdir "$(echo "$dirlist" |\ awk -F\| '{gsub(/(+)/," ",$NF);gsub(/\.atr$/,""); print}')" done < /user/Oracle/my_catalog/default/root/bkup/dirnames.txt where dirname.txt consist of file path details as mentioned below ... (4 Replies)
Discussion started by: aks_1902
4 Replies

6. Homework & Coursework Questions

Creating directories within a directory?

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted! 1. The problem statement, all variables and given/known data: Ok i need to create a directory within another directory in one command. I'm already in a directory to. I need to... (6 Replies)
Discussion started by: gangsta
6 Replies

7. Shell Programming and Scripting

Creating Directory

Hi All, As I m very new for Unix, I need to check for a directory and move a file. If Directory is not found, The script should create a directory and move the file. Can any one help here. (7 Replies)
Discussion started by: vikramtk
7 Replies

8. Shell Programming and Scripting

creating a directory tree

Hi all, I'd like to create a directory tree, and define from stdin how many levels deep and how many directories in each level should be created. What I wrote does not work properly:#!/bin/bash #set -x read -p " What root directory? " rootDir && { /bin/rm -R $rootDir; mkdir $rootDir; } ||... (2 Replies)
Discussion started by: NBaH
2 Replies

9. Shell Programming and Scripting

Creating date directory and moving files into that directory

I have list of files named file_username_051208_025233.log. Here 051208 is the date and 025233 is the time.I have to run thousands of files daily.I want to put all the files depending on the date of running into a date directory.Suppose if we run files today they should put into 05:Dec:08... (3 Replies)
Discussion started by: ravi030
3 Replies

10. UNIX for Advanced & Expert Users

creating directory error

i have create a Directory with "$@#$%" . After creating a Directory, put ls command display "#$%" . Why? (2 Replies)
Discussion started by: lakshmananindia
2 Replies
Login or Register to Ask a Question