Shell编程:批量获取文件权限信息

2023-10-22  本文已影响0人  野藤_

SAS程序完成后,一般会用脚本进行版本控制,文件权限信息会被设置为只读、不可写。为方便判断版本控制是否成功完成,简单写了个Shell脚本用于查看SAS程序权限信息。若SAS程序依旧可写,则版本控制需重新设置。

脚本逻辑不复杂,如果读者想要引用参考,可直接替换路径地址。查询结果输出到csv文件中,可使用EXCEL打开。通过简单设置,就可以筛选出还处于可写状态下的SAS程序。

程序备份如下:

#!/bin/bash
#07_chkfile_sccs.sh

#Set results output;
res_path="/xx/xx/tfl_sas_files_info.csv"

#Set the folder paths to scan
ana_path="/xxx/xxx"
folder_paths=("$ana_path/tables" "$ana_path/listings" "$ana_path/figures")

#Create an empty csv file
echo "File Name,Permission,Size,Owner,Modified Time" > $res_path

#Traverse each folder
for folder_path in "${folder_paths[@]}"
do
    #Traverse each SAS file in the folder
    for file in "$folder_path"/*.sas
    do
        if [[ -f "$file" ]]; then
            #Get the file name
            file_name=$(basename "$file")
            #Get the file permission
            file_permission=$(stat -c "%A" "$file")
            #Get the file size
            file_size=$(stat -c "%s" "$file")
            #Get the file owner
            file_owner=$(stat -c "%U" "$file")
            #Get the file modified time
            file_mtime=$(stat -c "%y" "$file")
            #Format the date and time
            file_datetime=$(date -d "$file_mtime" "+%Y-%m-%d %H:%M:%S")

            #Append the file information to the CSV file
            echo "$file_name,$file_permission,$file_size,$file_owner,$file_datetime" >> $res_path
        fi
    done
done

感谢阅读, 欢迎关注:SAS茶谈!
若有疑问,欢迎评论交流!

梳理不易,转载请注明出处 (by Jihai / SAS茶谈)

上一篇 下一篇

猜你喜欢

热点阅读