How to Traverse All Files in Subdirectories Using a Shell Script
Recently, the blog was migrated and the CDN source changed. I wanted to refresh and preheat all images on the CDN. The problem arose: to refresh and preheat, I needed all image addresses. So, I decided to use a Shell script to recursively traverse the wp-content/uploads directory to get the image file paths. Let's get started.

Create a Shell Script
Use the vi editor to create a file named traveDir.sh and copy the following script code:
#! /bin/bash
function read_dir(){
for file in `ls $1` # Note: these are backticks, indicating running a system command
do
if [ -d $1"/"$file ] # Note: there must be spaces between these, otherwise an error will occur
then
read_dir $1"/"$file
else
echo $1"/"$file # Process the file here
fi
done
}
# Read the first argument
read_dir $1
Don't forget to add execution permissions to the script: chmod +x traveDir.sh.
Usage
The script has been created in the previous step. Next, simply execute ./traveDir.sh /xxx/wp-content/uploads, replacing xxx with the absolute path of your site. If you want to export the results to a .txt file, you can use the >> stream operator.
# Export to .txt
./traveDir.sh /xxx/wp-content/uploads >> 1.txt
Subsequent Operations
The exported file paths might look like /data/wwwroot/xiaoz.me/wp-content/uploads//2019/11/snipaste_20191110_102750.png. Use a text editor to batch replace /data/wwwroot/xiaoz.me/ with your own domain name, then submit the URLs to the CDN provider for refresh and preheating.
Summary
- Batch export file paths using the script.
- Use a text tool to batch replace the site root directory with your own domain name.
- Submit the URLs for refresh.
The script is quite simple; you can find many similar examples online. This article references: How to Traverse All Files in a Directory and Its Subdirectories Using Shell.