Extract all the server names from nginx configs
Let's say you have a bunch of sites behind one nginx, and you want to extract all the domains from all the .conf
files, one per line. #shell
cd /etc/nginx/sites-enabled
cat * | grep server_name | sed "s/server_name//" | tr -d ";" | cut -f1 -d"#" | xargs | tr " " "\n" | sort | uniq
Explanation
cat * | grep server_name*
→ List content from all files in a directory and output the lines containingserver_name
.sed "s/server_name//"
→ Remove the word "server_name" from those lines.tr -d ";"
→ Delete the semicolon.cut -f1 -d"#"
→ Ignore everything after a#
(inline comments).xargs | tr " " "\n"
→ Replace spaces with new lines.sort | uniq
→ Sort lines, get rid of duplicates.
Voila!
Unless specified otherwise, this work is licensed under a Creative Commons BY-NC-SA 4.0.