Find Wordpress root directory in bash without WP-CLI












2















How can I find the WordPress root directory in Bash without using WP-CLI?










share|improve this question

























  • Can you provide some context as to why WP CLI is specifically excluded in the question? Also, the WP root directory doesn't make sense as a term once you start doing subdirectory installs such as those composer would install

    – Tom J Nowell
    2 hours ago


















2















How can I find the WordPress root directory in Bash without using WP-CLI?










share|improve this question

























  • Can you provide some context as to why WP CLI is specifically excluded in the question? Also, the WP root directory doesn't make sense as a term once you start doing subdirectory installs such as those composer would install

    – Tom J Nowell
    2 hours ago
















2












2








2








How can I find the WordPress root directory in Bash without using WP-CLI?










share|improve this question
















How can I find the WordPress root directory in Bash without using WP-CLI?







command-line






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited 3 hours ago









Dave Romsey

12.8k83653




12.8k83653










asked 4 hours ago









SlamSlam

227110




227110













  • Can you provide some context as to why WP CLI is specifically excluded in the question? Also, the WP root directory doesn't make sense as a term once you start doing subdirectory installs such as those composer would install

    – Tom J Nowell
    2 hours ago





















  • Can you provide some context as to why WP CLI is specifically excluded in the question? Also, the WP root directory doesn't make sense as a term once you start doing subdirectory installs such as those composer would install

    – Tom J Nowell
    2 hours ago



















Can you provide some context as to why WP CLI is specifically excluded in the question? Also, the WP root directory doesn't make sense as a term once you start doing subdirectory installs such as those composer would install

– Tom J Nowell
2 hours ago







Can you provide some context as to why WP CLI is specifically excluded in the question? Also, the WP root directory doesn't make sense as a term once you start doing subdirectory installs such as those composer would install

– Tom J Nowell
2 hours ago












1 Answer
1






active

oldest

votes


















2














Simple bash script to find your Wordpress root



Ever need to run a script outside of Wordpress and need to know the Wordpress root directory?



while [ ! -e wp-config.php ]; do
if [ $pwd/ = / ]; then
echo "No Wordpress root found" >&2; exit 1
fi
cd ../
done
if [ -e wp-config.php ]; then
wproot=$(pwd)
fi


Use cases:




  • Custom shell scripts to sync local and remote folders and databases.

  • Docker workflows where wp-cli wp commands won't work.

  • Webpack/grunt workflows where you might be issuing commands from the theme folder instead of the Wordpress root.


How to use




  • put this code at the top of any script like myscript.sh.

  • set permissions with chmod u+x myscript.sh.

  • Use ${wproot} as a variable in any path.


    • Example echo "Uploads path is ${wproot}/wp-content/uploads".




Caveats



This is a simple script and may not work under all conditions. It will not work if:




  • your wp-config.php file is not stored in your Wordpress root

  • your wp-config.php file is rename


Admittedly the conditions under which you might need this are pretty rare. I needed it for a visual regression test script that needs to traverse several folders and issue various commands, all without wp-cli or Wordpress functions.



Suggestions for improvement are welcome.






share|improve this answer


























  • Note that the wp-config.php file can be one level up, in which case this will generate the wrong result

    – Tom J Nowell
    2 hours ago











Your Answer








StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "110"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fwordpress.stackexchange.com%2fquestions%2f326720%2ffind-wordpress-root-directory-in-bash-without-wp-cli%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









2














Simple bash script to find your Wordpress root



Ever need to run a script outside of Wordpress and need to know the Wordpress root directory?



while [ ! -e wp-config.php ]; do
if [ $pwd/ = / ]; then
echo "No Wordpress root found" >&2; exit 1
fi
cd ../
done
if [ -e wp-config.php ]; then
wproot=$(pwd)
fi


Use cases:




  • Custom shell scripts to sync local and remote folders and databases.

  • Docker workflows where wp-cli wp commands won't work.

  • Webpack/grunt workflows where you might be issuing commands from the theme folder instead of the Wordpress root.


How to use




  • put this code at the top of any script like myscript.sh.

  • set permissions with chmod u+x myscript.sh.

  • Use ${wproot} as a variable in any path.


    • Example echo "Uploads path is ${wproot}/wp-content/uploads".




Caveats



This is a simple script and may not work under all conditions. It will not work if:




  • your wp-config.php file is not stored in your Wordpress root

  • your wp-config.php file is rename


Admittedly the conditions under which you might need this are pretty rare. I needed it for a visual regression test script that needs to traverse several folders and issue various commands, all without wp-cli or Wordpress functions.



Suggestions for improvement are welcome.






share|improve this answer


























  • Note that the wp-config.php file can be one level up, in which case this will generate the wrong result

    – Tom J Nowell
    2 hours ago
















2














Simple bash script to find your Wordpress root



Ever need to run a script outside of Wordpress and need to know the Wordpress root directory?



while [ ! -e wp-config.php ]; do
if [ $pwd/ = / ]; then
echo "No Wordpress root found" >&2; exit 1
fi
cd ../
done
if [ -e wp-config.php ]; then
wproot=$(pwd)
fi


Use cases:




  • Custom shell scripts to sync local and remote folders and databases.

  • Docker workflows where wp-cli wp commands won't work.

  • Webpack/grunt workflows where you might be issuing commands from the theme folder instead of the Wordpress root.


How to use




  • put this code at the top of any script like myscript.sh.

  • set permissions with chmod u+x myscript.sh.

  • Use ${wproot} as a variable in any path.


    • Example echo "Uploads path is ${wproot}/wp-content/uploads".




Caveats



This is a simple script and may not work under all conditions. It will not work if:




  • your wp-config.php file is not stored in your Wordpress root

  • your wp-config.php file is rename


Admittedly the conditions under which you might need this are pretty rare. I needed it for a visual regression test script that needs to traverse several folders and issue various commands, all without wp-cli or Wordpress functions.



Suggestions for improvement are welcome.






share|improve this answer


























  • Note that the wp-config.php file can be one level up, in which case this will generate the wrong result

    – Tom J Nowell
    2 hours ago














2












2








2







Simple bash script to find your Wordpress root



Ever need to run a script outside of Wordpress and need to know the Wordpress root directory?



while [ ! -e wp-config.php ]; do
if [ $pwd/ = / ]; then
echo "No Wordpress root found" >&2; exit 1
fi
cd ../
done
if [ -e wp-config.php ]; then
wproot=$(pwd)
fi


Use cases:




  • Custom shell scripts to sync local and remote folders and databases.

  • Docker workflows where wp-cli wp commands won't work.

  • Webpack/grunt workflows where you might be issuing commands from the theme folder instead of the Wordpress root.


How to use




  • put this code at the top of any script like myscript.sh.

  • set permissions with chmod u+x myscript.sh.

  • Use ${wproot} as a variable in any path.


    • Example echo "Uploads path is ${wproot}/wp-content/uploads".




Caveats



This is a simple script and may not work under all conditions. It will not work if:




  • your wp-config.php file is not stored in your Wordpress root

  • your wp-config.php file is rename


Admittedly the conditions under which you might need this are pretty rare. I needed it for a visual regression test script that needs to traverse several folders and issue various commands, all without wp-cli or Wordpress functions.



Suggestions for improvement are welcome.






share|improve this answer















Simple bash script to find your Wordpress root



Ever need to run a script outside of Wordpress and need to know the Wordpress root directory?



while [ ! -e wp-config.php ]; do
if [ $pwd/ = / ]; then
echo "No Wordpress root found" >&2; exit 1
fi
cd ../
done
if [ -e wp-config.php ]; then
wproot=$(pwd)
fi


Use cases:




  • Custom shell scripts to sync local and remote folders and databases.

  • Docker workflows where wp-cli wp commands won't work.

  • Webpack/grunt workflows where you might be issuing commands from the theme folder instead of the Wordpress root.


How to use




  • put this code at the top of any script like myscript.sh.

  • set permissions with chmod u+x myscript.sh.

  • Use ${wproot} as a variable in any path.


    • Example echo "Uploads path is ${wproot}/wp-content/uploads".




Caveats



This is a simple script and may not work under all conditions. It will not work if:




  • your wp-config.php file is not stored in your Wordpress root

  • your wp-config.php file is rename


Admittedly the conditions under which you might need this are pretty rare. I needed it for a visual regression test script that needs to traverse several folders and issue various commands, all without wp-cli or Wordpress functions.



Suggestions for improvement are welcome.







share|improve this answer














share|improve this answer



share|improve this answer








edited 4 hours ago

























answered 4 hours ago









SlamSlam

227110




227110













  • Note that the wp-config.php file can be one level up, in which case this will generate the wrong result

    – Tom J Nowell
    2 hours ago



















  • Note that the wp-config.php file can be one level up, in which case this will generate the wrong result

    – Tom J Nowell
    2 hours ago

















Note that the wp-config.php file can be one level up, in which case this will generate the wrong result

– Tom J Nowell
2 hours ago





Note that the wp-config.php file can be one level up, in which case this will generate the wrong result

– Tom J Nowell
2 hours ago


















draft saved

draft discarded




















































Thanks for contributing an answer to WordPress Development Stack Exchange!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fwordpress.stackexchange.com%2fquestions%2f326720%2ffind-wordpress-root-directory-in-bash-without-wp-cli%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Accessing regular linux commands in Huawei's Dopra Linux

Can't connect RFCOMM socket: Host is down

Kernel panic - not syncing: Fatal Exception in Interrupt