Create directories with regular expression


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Create directories with regular expression
# 1  
Old 08-31-2008
Create directories with regular expression

Hi guys,
can any one tell me how to create directories using regular expression?
Let's say that I need to create directories test01, test02, test03.... test10.

Can it be done using any regular expression?

thanks.
# 2  
Old 08-31-2008
Why regular expression? Some shells (ksh93, some bash versions and zsh) support brace expansion.

With bash and ksh93:

Code:
mkdir $(printf "test%02d " {1..10})

With zsh:

Code:
mkdir test{01..10}

If your shell doesn't support such expansion, you could use a shell loop or use some external tool.
Something like this for example:

Code:
perl -e'mkdir sprintf "test%02d", $_ or warn "Oops: $!\n" for 1..10'


Last edited by radoulov; 08-31-2008 at 12:42 PM..
# 3  
Old 08-31-2008
thanku radoulov, for prompt reply.
3rd code given by you is working perfectly....Smilie

May be my shell doesn't support such expansions, it has given following errors with rest of the commands.

1st one has given the error:
printf: 3016-001 Specify a number instead of {1..10}.

and second one has created a directory with name test{01..10}.
# 4  
Old 08-31-2008
HI ,radoulov ,

can it be done by any command other than perl ??
# 5  
Old 08-31-2008
[assuming no spaces or other pathological characters in the pattern]

Yes,
with AWK (you should use nawk or /usr/xpg4/bin/awk on Solaris):
Code:
awk 'BEGIN {
  while (++i <= 10)
    args = args ? args FS sprintf("test%02d", i) : sprintf("test%02d", i)
  print "mkdir", args | "sh" 
}'

Or using a shell loop (assuming word splitting, so it won't work with default zsh configuration):

Code:
i=1
unset args
while [ $i -le 10 ]; do
  args="$args $(printf "test%02d" $i)"
  i=$(($i + 1))
done
mkdir $args


Last edited by radoulov; 08-31-2008 at 03:57 PM..
# 6  
Old 08-31-2008
In the shell, anything which generates the file names you want can be passed to mkdir, but it's not (usually) a regular expression then.

Code:
for f in $(seq 1 10); do printf "test%02i\n" $f"; done | xargs mkdir

Code:
mkdir `awk '{ for (i=1; i<=10; ++i) printf "%02i", i }' /dev/null`

Code:
tr '\000' '\012' </dev/zero | head -10 | nl | sed 's/.*/mkdir test0/;s/010/10/' | sh

# 7  
Old 08-31-2008
thanks for the reply era,

None of the commands u have given r working,
outputs of the commands are as follows:
1st: ksh: seq: not found
2nd: Usage: mkdir [-p] [-m mode] Directory ...
3rd:mkdir: 0653-358 Cannot create test0.
test0: Do not specify an existing file.


I dont exactly know what are the reasons. I use ksh, and some of these commands may not work on ksh?
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Grep with Regular expression now working on file directories

Hello Everyone, I have a file sam1 with the below content SYSYSID;MANDT;/SIE/AD_Z0M_INDX;/SIE/AD_Z0M_KEY1 echo $Regex \bSYSYSID\b|\bMANDT\b|\b/SIE/AD_Z0M_INDX\b|\b/SIE/AD_Z0M_KEY1\b cat sam1 | grep -Eo $Regex I expect the result as SYSYSID MANDT /SIE/AD_Z0M_INDX /SIE/AD_Z0M_KEY1... (4 Replies)
Discussion started by: sam99
4 Replies

2. UNIX for Advanced & Expert Users

sed: -e expression #1, char 0: no previous regular expression

Hello All, I'm trying to extract the lines between two consecutive elements of an array from a file. My array looks like: problem_arr=(PRS111 PRS213 PRS234) j=0 while } ] do k=`expr $j + 1` sed -n "/${problem_arr}/,/${problem_arr}/p" problemid.txt ---some operation goes... (11 Replies)
Discussion started by: InduInduIndu
11 Replies

3. Programming

How to use regular expression in C/C++ ?

how to code with regexp.h some one can give me instance? thx (4 Replies)
Discussion started by: AKB48
4 Replies

4. Programming

Perl: How to read from a file, do regular expression and then replace the found regular expression

Hi all, How am I read a file, find the match regular expression and overwrite to the same files. open DESTINATION_FILE, "<tmptravl.dat" or die "tmptravl.dat"; open NEW_DESTINATION_FILE, ">new_tmptravl.dat" or die "new_tmptravl.dat"; while (<DESTINATION_FILE>) { # print... (1 Reply)
Discussion started by: jessy83
1 Replies

5. Shell Programming and Scripting

Integer expression expected: with regular expression

CA_RELEASE has a value of 6. I need to check if that this is a numeric value. if not error. source $CA_VERSION_DATA if * ] then echo "CA_RELESE $CA_RELEASE is invalid" exit -1 fi + source /etc/ncgl/ca_version_data ++ CA_PRODUCT_ID=samxts ++ CA_RELEASE=6 ++ CA_WEEK_NO=7 ++... (3 Replies)
Discussion started by: ketkee1985
3 Replies

6. Shell Programming and Scripting

Regular Expression

Hi, In Perl What should be the regular expression for 1-23. I tried with |1|2. But it is not working. I have a code snippet like below $state = 0; while( $state != 1 ) { $hour=<STDIN>; if ( $hour =~ /|1|2/) { print "Integer within range.\n"; $state = 1;... (3 Replies)
Discussion started by: siba.s.nayak
3 Replies

7. UNIX for Dummies Questions & Answers

using regular expression for directories in find command

Hi, I want to find the files available in a directory /var/user/*/*/data/. I tried using the command "find /var/user/ -path '*/*/data/ -name '*' -type f" it says find: 0652-017 -path is not a valid option and then i tried using "find /var/user/ -name '*/*/data/*' -type f" but its not... (3 Replies)
Discussion started by: vinothbabu12
3 Replies

8. Shell Programming and Scripting

check if multiple directories exist else create missing directories

Hi , I 'm trying to check if multiple directories exist on a server, if not create the missing ones and print " creating missing directory. how to write this in a simple script, I have made my code complex if ; then taskStatus="Schema extract directory exists, checking if SQL,Count and... (7 Replies)
Discussion started by: ramky79
7 Replies

9. Linux

Regular expression to extract "y" from "abc/x.y.z" .... i need regular expression

Regular expression to extract "y" from "abc/x.y.z" (2 Replies)
Discussion started by: rag84dec
2 Replies

10. Shell Programming and Scripting

Regular Expression + Aritmetical Expression

Is it possible to combine a regular expression with a aritmetical expression? For example, taking a 8-numbers caracter sequece and casting each output of a grep, comparing to a constant. THX! (2 Replies)
Discussion started by: Z0mby
2 Replies
Login or Register to Ask a Question