If you want the HTTP response/status and the URL saved to a file in the format you desire. then curl
alone can do that ... It has the option -w
or --write-out
(Which defines what to display on stdout after a completed and successful operation. The format is a string that may contain plain text mixed with any number of variables ... e.g. -w "-----------\nStatus: %{http_code}\nURL: %{url}\n"
) and if you redirect the rest of the output to e.g. /dev/null
then you end up with a clean result of just what you want and you can then redirect that result and append it to a file called e.g. url.txt
like so:
curl -o /dev/null -s -w "-----------\nStatus: %{http_code}\nURL: %{url}\n" https://askubuntu.com >> url.txt
If you have a list of URLs(each on a new line) in a file called e.g. list.txt
like so:
$ cat list.txt
https://askubuntu.com/
https://unix.stackexchange.com/
https://notworkingurlblabla.com/
Then, you can check all the URLs in that file, filter-out non 200
status URLs and append the result to url.txt
all at once e.g. like so:
xargs -n 1 <list.txt curl -o /dev/null -s -w "%{http_code} %{url}\n" | \
awk '{if ($1 == "200") printf "------------\nStatus: "$1"\nURL: "$2"\n"}' >> url.txt
You can also, obviously, use awk
to filter output with a single URL as well like so:
curl -o /dev/null -s -w "%{http_code} %{url}\n" https://askubuntu.com/ | \
awk '{if ($1 == "200") printf "------------\nStatus: HTTP "$1"\nURL: "$2"\n"}' >> url.txt