Solved

Is piping the answer

Posted on 2004-08-23
8
180 Views
Last Modified: 2010-04-21
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.
0
Comment
Question by:oracleapprentice
8 Comments
 
LVL 11

Expert Comment

by:griessh
ID: 11875427
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
0
 
LVL 20

Expert Comment

by:Gns
ID: 11880311
.... 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
0
 

Author Comment

by:oracleapprentice
ID: 11882344
I found this to work for me.

find . -regex '.*\/foo' -exec mkdir -v '{}/foo2' \;
0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 20

Expert Comment

by:Gns
ID: 11889957
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
0
 
LVL 48

Expert Comment

by:Tintin
ID: 11899014
Easy enough for a portable find solution (maybe a few rare exceptions)

find . -type d -name "*foo" -exec mkdir '{}/foo2' \;
0
 
LVL 20

Expert Comment

by:Gns
ID: 11900644
The glob is still wrong Tintin, but otherwise true:-)

-- Glenn
0
 
LVL 1

Accepted Solution

by:
Computer101 earned 0 total points
ID: 12185397
PAQed, with points refunded (50)

Computer101
E-E Admin
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Let's say you need to move the data of a file system from one partition to another. This generally involves dismounting the file system, backing it up to tapes, and restoring it to a new partition. You may also copy the file system from one place to…
My previous tech tip, Installing the Solaris OS From the Flash Archive On a Tape (http://www.experts-exchange.com/articles/OS/Unix/Solaris/Installing-the-Solaris-OS-From-the-Flash-Archive-on-a-Tape.html), discussed installing the Solaris Operating S…
Learn how to navigate the file tree with the shell. Use pwd to print the current working directory: Use ls to list a directory's contents: Use cd to change to a new directory: Use wildcards instead of typing out long directory names: Use ../ to move…
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now