0

Given base find command:

find . -type d

For each directory returned, I want to spawn an interactive shell that has that directory as the current directory.

5

Solution

This works:

find . -execdir sh \;

Demo

# Create some sample directories
$ for i in {1..4}; do mkdir -p "/tmp/example/$i/foo" ; done

# spawn
$ find -s /tmp/example -name foo -execdir sh \;
sh-3.2$ pwd
/private/tmp/example/1
sh-3.2$ exit
sh-3.2$ pwd
/private/tmp/example/2
sh-3.2$ exit
sh-3.2$ pwd
/private/tmp/example/3
sh-3.2$ exit
sh-3.2$ pwd
/private/tmp/example/4
sh-3.2$ exit
$

How It Works

find

Relevant manpage info and/or other info:

-s: Cause find to traverse the file hierarchies in lexicographical order, i.e., alphabetical order within each directory. This isn't critical to the solution, but it makes the order of the shells be predictable.

/tmp/example: The path argument – The find utility recursively descends the directory tree for each path listed.

-name pattern: Matches files if the last component of the pathname being examined matches pattern. Special shell pattern matching characters (['',]'', *'', and?'') may be used as part of pattern.

-execdir utility \;: The -execdir primary is identical to the -exec primary with the exception that utility will be executed from the directory that holds the current file.

sh

sh: Running sh in this context spawns an interactive shell from the directory that holds the current file.

  • 1
    Brilliant! That simplifies the solution significantly. I'll update my answer. Thank you! – Barry Jones Apr 6 at 2:26
  • Please mark the answer that helped you as 'accepted' so that others with a similar problem can find the solution more easily. :) – Mioriin Apr 6 at 2:55
  • Okay, done. It's just awkward because my answer is the only answer. – Barry Jones Apr 6 at 3:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.