Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have some webspace on which I can't edit the main php.ini file, and I can't use an .htaccess command to make it recursively applied.

If we call my web directory D, I'd like to recursively create a softlink in every subdirectory of D (and every subdirectory under those, etc) to a php.ini file in D.

Is there a single command line statement that could be used for this? If so, what is it?

I'm using a Linux-based host.

share|improve this question
up vote 3 down vote accepted

Bash 4+ solution (with globstar enabled, ** is a recursive glob and **/ will expand to every directory, recursively):

shopt -s globstar
for f in ./**/; do ln -s php.ini "$f"php.ini; done

POSIX solution with find -- this will work on any *nix:

find . -type d -exec ln -s php.ini '{}'/php.ini \;

Either of those should be run in the directory D. ln -s is the command to create a symbolic (soft) link on *nix, you can check out further information about it with man ln.

To remove the links afterwards, you can again use either bash (assuming version 4+) or find:

shopt -s globstar
for f in ./**/php.ini; do [[ -L "$f" ]] && rm "$f"; done

The [[ -L "$f" ]] tests to see if $f is a soft link, and && means 'only do the next thing if the previous evaluates to true' -- so this won't get rid of your original 'php.ini' file.

find . -type l -name 'php.ini' -delete

-type l (that's a lower-case L, not a one) tells find to look for symbolic (soft) links only, which means that it won't find & delete your original 'php.ini' file.

share|improve this answer
Thanks! That does create the softlinks... (unfortunately, I've got to go back to the drawing board on how I'm going to approach the original problem) If I want to remove them later, I could make the globstar look like: for f in ./**/; do rm php.ini; done Right? – apnorton Jul 5 '13 at 21:05
for f in ./**/; do rm "$f"php.ini; done, actually (your guess would only remove the original php.ini file, not any of the soft links). Or, even better: rm ./**/php.ini. Both of those will remove the original php.ini too, though; the easiest way around that is to rename the original first with something like mv php.ini php.ini.backup, and then name it back when you're done getting rid of the links. It's possible to avoid the renaming workaround using find . -mindepth 2 -type f -name 'php.ini' -delete -- see man find for more information. – evilsoup Jul 5 '13 at 21:12
D'oh, brainfart -- that would find regular files, and not symbolic links; instead you should use find . -type l -name 'php.ini' -delete (that's a lowercase l, not a 1). – evilsoup Jul 5 '13 at 21:23

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .