root@maelcum:~ # pkg which /usr/local/etc/nginx/nginx.conf-dist
/usr/local/etc/nginx/nginx.conf-dist was installed by package nginx-1.22.1_5,3
pkg-which(8) might be useful.
Note that the config file itself isn't installed by a package. But there's usually a *.sample or some other copy of the config that is installed with the package.Code:root@maelcum:~ # pkg which /usr/local/etc/nginx/nginx.conf-dist /usr/local/etc/nginx/nginx.conf-dist was installed by package nginx-1.22.1_5,3
But don't automate that. Things like /etc/rc.conf are not from a package but will be surely missed when gone.
Yeah, you can automate the scanning itself, but I wouldn't automate the removal.
#!/bin/sh
# Define the directory to search
dir_to_search="/usr/local"
# Define the output file
output_file="config_files.txt"
# Loop through all files under the directory recursively
find "$dir_to_search" -type f | while read file_path; do
# Run the "pkg which" command on each file and write the output to the file
pkg which "$file_path" >> "$output_file"
done