Method for traversing all files in subdirectories using Shell script

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.

Creating the Shell Script

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.

Usage

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

Further Steps

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.

Summary

  1. The script exports file paths in batches.
  2. Use a text tool to batch replace the site's root directory with your own domain.
  3. Submit the URLs for refresh.

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


Comments