Publish: 2020-02-18 | Modify: 2020-02-18
Recently, my blog has moved, and the CDN source has changed. I want to refresh and warm up all the images on the CDN. So, the problem is, I need to get all the image URLs for the refresh and warm-up process. I decided to use a shell script to recursively traverse the wp-content/uploads
directory to get the image file paths.
Using the vi editor, create a file named traveDir.sh
and copy the following script code:
#! /bin/bash
function read_dir(){
for file in `ls $1`
do
if [ -d $1"/"$file ]
then
read_dir $1"/"$file
else
echo $1"/"$file
fi
done
}
read_dir $1
Don't forget to give the script execute permission with chmod +x traveDir.sh
.
After creating the script in the previous step, you just need to execute ./traveDir.sh /xxx/wp-content/uploads
, where xxx
should be replaced with the absolute path of your site. If you want to export it as a .txt
file, you can use the >>
stream operation.
# Export as .txt
./traveDir.sh /xxx/wp-content >> 1.txt
The exported file paths may look like /data/wwwroot/xiaoz.me/wp-content/uploads//2019/11/snipaste_20191110_102750.png
. You can use a text tool to batch replace /data/wwwroot/xiaoz.me/
with your own domain, and then submit the URLs to your CDN service provider for refresh and warm-up.
The script is quite simple and can be found easily online. This article refers to: Method for Traversing All Files in a Directory and Its Subdirectories Using Shell
I come from China and I am a freelancer. I specialize in Linux operations, PHP, Golang, and front-end development. I have developed open-source projects such as Zdir, ImgURL, CCAA, and OneNav.