简体   繁体   中英

How can I save multiple IP addresses from file names in a directory?

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.

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
...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM