Skip to main content
Brief explanation
Source Link
Chris Davies
  • 128.5k
  • 16
  • 179
  • 325

Assuming your find supports it, use the -execdir option instead of -exec

find . -type f -name 'foo' -execdir pwd \; 

This will change into each directory in turn, and if there's a foo file present there it will run pwd.

NB You cannot repurpose this solution for running commands inside directories named foo. For that you'd need to revert to a simple -exec sh -c '...' approach, iterating across the args supplied to sh and using cd for each in turn. This example runs pwd inside each directory:

find . -type d -name 'foodir' -exec sh -c ' for d in "$@"; do ( cd "$d" || exit; pwd ); done ' _ {} + 

Assuming your find supports it, use the -execdir option instead of -exec

find . -type f -name 'foo' -execdir pwd \; 

This will change into each directory in turn, and if there's a foo file present there it will run pwd.

NB You cannot repurpose this solution for running commands inside directories named foo. For that you'd need to revert to a simple -exec sh -c '...' approach, iterating across the args supplied to sh and using cd for each in turn:

find . -type d -name 'foodir' -exec sh -c ' for d in "$@"; do ( cd "$d" || exit; pwd ); done ' _ {} + 

Assuming your find supports it, use the -execdir option instead of -exec

find . -type f -name 'foo' -execdir pwd \; 

This will change into each directory in turn, and if there's a foo file present there it will run pwd.

NB You cannot repurpose this solution for running commands inside directories named foo. For that you'd need to revert to a simple -exec sh -c '...' approach, iterating across the args supplied to sh and using cd for each in turn. This example runs pwd inside each directory:

find . -type d -name 'foodir' -exec sh -c ' for d in "$@"; do ( cd "$d" || exit; pwd ); done ' _ {} + 
Respond to criticism
Source Link
Chris Davies
  • 128.5k
  • 16
  • 179
  • 325

Assuming your find supports it, use the -execdir option instead of -exec

find . -type f -name 'foo' -execdir pwd \; 

This will change into each directory in turn, and if there's a foo file present there it will run pwd.

NB You cannot repurpose this solution for running commands inside directories named foo. For that you'd need to revert to a simple -exec sh -c '...' approach, iterating across the args supplied to sh and using cd for each in turn:

find . -type d -name 'foodir' -exec sh -c ' for d in "$@"; do ( cd "$d" || exit; pwd ); done ' _ {} + 

Assuming your find supports it, use the -execdir option instead of -exec

find . -name 'foo' -execdir pwd \; 

Assuming your find supports it, use the -execdir option instead of -exec

find . -type f -name 'foo' -execdir pwd \; 

This will change into each directory in turn, and if there's a foo file present there it will run pwd.

NB You cannot repurpose this solution for running commands inside directories named foo. For that you'd need to revert to a simple -exec sh -c '...' approach, iterating across the args supplied to sh and using cd for each in turn:

find . -type d -name 'foodir' -exec sh -c ' for d in "$@"; do ( cd "$d" || exit; pwd ); done ' _ {} + 

Assuming your find supports it, use the -execdir option instead of -exec

find . -name 'foo' -execdir pwd \; 

If it doesn't, please provide details of your platform and/or distribution (as appropriate).

Assuming your find supports it, use the -execdir option instead of -exec

find . -name 'foo' -execdir pwd \; 

If it doesn't, please provide details of your platform and/or distribution (as appropriate).

Assuming your find supports it, use the -execdir option instead of -exec

find . -name 'foo' -execdir pwd \; 
edited body
Source Link
Stéphane Chazelas
  • 587.8k
  • 96
  • 1.1k
  • 1.7k
Loading
Source Link
Chris Davies
  • 128.5k
  • 16
  • 179
  • 325
Loading