How to iterate through all files in a folder using shell?

In a shell, you can use a for loop and wildcards to iterate through all files in a folder. Here is an example code:

#!/bin/bash

# 设置要遍历的文件夹路径
folder="/path/to/folder"

# 遍历文件夹中的所有文件
for file in "$folder"/*
do
    # 判断文件是否存在
    if [ -f "$file" ]; then
        echo "处理文件:$file"
        # 在这里可以进行文件的操作,例如拷贝、移动、重命名等
    fi
done

In the above example, replace /path/to/folder with the actual path of the folder you want to iterate through. Then run the script, which will go through all the files in that folder and perform the corresponding operation on each file.

广告
Closing in 10 seconds
bannerAds