BASH loop on a file list or directory
One thing I find myself doing a lot at work and in CTFs is looping some command (or string of commands) on a list of items. There are plenty of ways to do this and here is what I find works best for me. Create a file with the list of items each on a new line. Typically I’ll just open vim in my Linux term and paste it in or scp it real quick.
for i in $(cat list.txt)
do
cmd $i >> output.txt
done
This loop combined with the “dig +short” command will take a list of FQDNs or domains and output their respective IPs to a file. Alternatively “dig -x +short” will take a list of IPs to get the reverse IP on a list of IP addresses, which is often used for red teaming during the recon phase.
I do not use this as often as generally the command will have enough support to process multiple files but every now and then a niche case such as a script or some other command requires a FOR loop to process all files in a directory. In those cases this is how to run a command on every file in a directory using a FOR loop.
for file in /dir/*
do
cmd [option] "$file" >> results.out
done