Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

script to create subfolders 1

Status
Not open for further replies.

KatherineF

Technical User
Mar 3, 2003
124
US
Hi,

I have a bash script as follows:

-------------------------------------------
#!/bin/sh
for dir in `find /foo/stats/* -type d`
do
mkdir `find $dir/newfolder/`
Done
-------------------------------------------

However, there`s a detail that I`d be grateful for help with:

As is, the script creates "newfolder" subfolder for ALL LEVELS of subfolders under "/foo/stats".

However, I`d like to apply it to the FIRST LEVEL OF SUBFOLDERS ONLY, e.g. "foo/stats/folder1", "foo/stats/folder2" BUT NOT TO "foo/stats/folder1/subfolder/", "foo/stats/folder2/subfolder/" and so forth.

Could you please help me modify this script appropriately?

Thanks in advance
 
Hi !

I noticed you are using bash... so you probably are in linux and if I remember correctly, there is an option with Linux`s find that specifies the depth and the option is :

-depth

so your command would be :

for dir in `find /foo/stat/* -type d -depth 1`

Hope this helps !
Olivier Martin
--------------
Read that somewhere and I think its cool ;) : If you want to accelerate a Windoze Box, -9.8 m/s^2 might be your best bet !
 
depending on your flavor of "find"

`find /foo/stats -type d -level 0`
`find /foo/stats -type d -maxdepth 1 -print`
 
Or find /foo/stats/* -type d -prune on Solaris.

Why the second find in your script? Shouldn't it read:

[tt]#!/bin/sh
for dir in `find /foo/stats/* -type d -prune`
do
mkdir $dir/newfolder/
done[/tt]

Olivier, the -depth switch has a different effect; it makes find do a "depth first" search, i.e.

[tt]/dir/file
/dir[/tt]

...instead of:

[tt]/dir
/dir/file[/tt]

... which is useful for some utilities, like cpio I think. Annihilannic.
 
my version:
the: /* in find
and: '/' at end of mkdir statement
are superfluos and confusing.
NOTA: if you need a subdir in newfolder, use:
mkdir -p $dir/newfolder/subdir

#!/bin/sh
for dir in `find /foo/stats -type d`
do
mkdir $dir/newfolder
done
 
I remembered when I saw the -maxdepth option that this is what is used in linux.. any idea what option I can use in AIX 4.3 ? I don't find it in the man page... and you are right, the -depth is to go through the directories in a different order. Thanx for the correction ;)
Olivier Martin
--------------
Read that somewhere and I think its cool :
If you want to accelerate a Windoze Box, there is an easy way to have it accelerate at -9.8 m/s^2 :)
 
You can always use:

mkdir -p to create all the directories and their subdirectories in one line

For example

suppose you want to create a directory called "a" and the subdirectory "b"

Then type mkdir -p /directory_name/a/b
 

Why the 'for' statement??

# find /foo/stats -type d -depth 1 -exec mkdir {}/newdir \;

should do it.

Cheers

Henrik Morsing
Certified AIX 4.3 Systems Administration
& p690 Technical Support
 
And why do you use a
Code:
find
after all ?
If you just want first level subdirectories:
Code:
for dir in /foo/stats/*/.; do
    mkdir -p $dir/subdir/subsubdir/andsoon
done
This way you do not fork any subshell.

Some comments about the responses I have seen above:

1) morsing: the
Code:
-depth 1
option does not stop the search at first level, it just do a "depth first" search as said Annihilannic. Using the
Code:
-exec
of the
Code:
find
command is a good practice. We often forget that
Code:
find
not only "find" but can also "do" something on entries found (it just echoes them by default).

2) iribash: the
Code:
/*
at the end of the find is not superfluous if you use the
Code:
-type d -prune
options with Solaris find. Without it, the only directory found would be
Code:
/foo/stats
itself, since it is a directory and you ask to not descend in directories.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top