Reputation: 89
Pretty new to Bash, I'm working on a bash script to automate storing/retrieving certificates from a certain directory. I figured out how to get one IP, but if there are multiple files with IP addresses in a directory, I am unsure how to store a list of the IPs.
For example, I can look in a directory whose path is saved in ${PATH}
PATH=$(ls path/to/directory)
to get ONLY the IP from a file named 0.0.0.0Cert.pem
SINGLE_IP=${echo "PATH%%C%"}
SINGLE_IP == 0.0.0.0
But if there is a 0.0.0.0Cert.pem and a 1.1.1.1Cert.pem in ${PATH}
SINGLE_IP=${echo "PATH%%C%"}
will only get the first IP found in the directory, however, I need to get all the IPs so I can grab all the relevant files later in the script.
Thanks!
EDIT:
For clarity I ONLY need the IP from the file names. I don't need the Cert.pem or anything other than the actual IP.
Upvotes: 1
Views: 354
Reputation: 1
Convert Below Binary to IP, And save it to /tmp/IP.txt file.
01111111.00000000.00000000.00000001
Upvotes: -1
Reputation: 92894
With find
command:
To find the "crucial" files:
find path/to/dir -type f -name *[0-9]Cert.pem
To extract only IP addresses from filenames:
find path/to/dir -type f -name "*[0-9]Cert.pem" -exec sh -c \
'f=$(basename $1); echo "${f%Cert*}"' _ {} \;
The output would be in the following format:
0.0.0.0
1.1.1.1
...
Upvotes: 3