繁体   English   中英

Linux Shell脚本-正则表达式可过滤带有日期的文件名

[英]Linux Shell Scripting - Regex to Filter Filename with Date in it

我有成千上万个这种命名格式的文件:

cdr_ABSHCECLUSTER_02_201709072214_987392

我正在使用下面的批处理脚本,但是我发现的是,它将根据修改后的日期而不是实际创建文件时重新定位文件。 如何修改此名称以从文件名中提取年份,月份?

由于可以移动文件,因此我发现可以基于“修改日期”而不是创建日期将文件放在错误的目录中。

统计信息显示选项:修改访问已更改

 for dir in /sftphome/*;
 do
    echo "Entering parent directory: " $dir
    cd $dir;
             if [  -d "CDR" ]; then
                    dirpath="$(pwd)/CDR"
                    cd $dirpath

                    echo "Searching CDR directory for files " $dirpath
                    find . -maxdepth 2 -type f |
                            while read file ; do
                                    #Check to see if object is a file or directory. Only copy files.
                                    if [[ ! -d $file ]]; then
                                            year="$(date -d "$(stat -c %y "$file")" +%Y)"
                                            month="$(date -d "$(stat -c %y "$file")" +%b)"

                                            #Create the directories if they don't exist. The -p flag makes 'mkdir' create the parent directories as needed
                                            if [ ! -d "$dirpath/$year/$month" ]; then
                                                    echo "Creating directory structure $dirpath/$year/$month..."
                                                    mkdir -p "$dirpath/$year/$month";
                                                    echo "Directory $dirpath/$year/$month created."
                                            fi

                                            echo "Relocating $dirpath/$file to $dirpath/$year/$month"
                                            cp -p $file "$dirpath/$year/$month"
                                            rm -f $file
                                    fi
                            done
                            echo "Relocation of all files in $dirpath is complete."
             el

我将不胜感激。 谢谢!

这是一种从文件名中的日期戳填充yearmonth变量的方法...

从变量file中的file名开始...

file=cdr_ABSHCECLUSTER_02_201709072214_987392

使用下划线( _ )作为分隔符,将file分成单独的字符串,并放入名为ar的数组中; 我们将遍历数组只是为了显示组件...

IFS='_' read -ra ar <<< "${file}"
for i in "${!ar[@]}"
do
    echo "ar[${i}] = ${ar[${i}]}"
done

# output from for loop:

ar[0] = cdr
ar[1] = ABSHCECLUSTER
ar[2] = 02
ar[3] = 201709072214
ar[4] = 987392

我们将解析ar[3]以获取我们的yearmonth值...

year=${ar[3]:0:4}     # 4-digit year  = substring from position 0 for 4 characters
mo=${ar[3]:4:2}       # 2-digit month = substring from position 4 for 2 characters
echo "year=${year} , mo=${mo}"

# output from echo command:

year=2017, mo=09

但是您的脚本希望以Mmm格式( date +%b )表示month ,因此请Mmm调整...

# convert our 2-character month to a 3-character 'Mon'th

month=$(date -d "${mo}" +%b)

# confirm our variables:

echo "year=${year} ; month=${month}"

# output from echo command:

year=2017 ; month=Sep

至此,我们已经从文件名中的日期戳中填充了yearmonth变量,现在您可以继续执行脚本的其余部分。

放在一起:

# once the 'file' variable is populated:

IFS='_' read -ra ar <<< "${file}"
year=${ar[3]:0:4}
mo=${ar[3]:4:2}
month=$(date -d "${mo}" +%b)

您只需要这个脚本。

find /sftphome/*/CDR -type f -maxdepth 2 | 
    while read file 
    do
        date=`basename "$file" | cut -d_ -f4`
        newdir="$(cut -d/ -f-4 <<< "$file")/${date:0:4}/${date:4:2}"
        mkdir -p "$newdir"
        mv -f "$file" "$newdir"
    done

编辑:

我刚刚注意到%b日期格式。 如果这是必须的(我不建议这样做,因为它很难排序),则将newdir=...行替换为:

newdir="$(cut -d/ -f-4 <<< "$file")/$(date -d${date:0:4}-${date:4:2}-01 +%Y/%b)"

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM