Link to home
Start Free TrialLog in
Avatar of oracleapprentice
oracleapprentice

asked on

Is piping the answer

I am trying to create a directory within a folder that is retrieved by a find command.  I came up with something like this:

find . -regex '.*\/foo' | mkdir -v foo2

But it doesn't work.

Help please.
Avatar of griessh
griessh
Flag of United States of America image

Hi oracleapprentice,

find . -type d -name '*foo' | read; cd $REPLY; mkdir foo2;  cd -

This won't work if you are expecting more than one '*foo' dirs

======
Werner
Avatar of Gns
Gns

.... In which case you'd need do it like a for loop, and create the subdir in every found result...
(also a slight amendment Werner, I think s/he's looking for the fixed name "foo", not any dir ending in foo)
for dir in `find . -type d -name "foo"`; do cd $dir; mkdir foo2; cd $OLDPWD; done

(Also take care with regexs' and globs including ".*"... At least when recursing ... not so bad here though:-)

-- Glenn
Avatar of oracleapprentice

ASKER

I found this to work for me.

find . -regex '.*\/foo' -exec mkdir -v '{}/foo2' \;
Hm, ok. Just another little warning, that solution relies on GNU find (which is not generally available on Unix systems) while the (slightly simplified) for loop solution works on most any unix/linux (note also that most mkdir versions don't have the -v flag either):
for dir in `find . -type d -name "foo"`; do  mkdir $dir/foo2; done

-- Glenn
Avatar of Tintin
Easy enough for a portable find solution (maybe a few rare exceptions)

find . -type d -name "*foo" -exec mkdir '{}/foo2' \;
The glob is still wrong Tintin, but otherwise true:-)

-- Glenn
ASKER CERTIFIED SOLUTION
Avatar of Computer101
Computer101
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial