【问题标题】:Linux Shell Scripting - Regex to Filter Filename with Date in itLinux Shell 脚本 - 正则表达式过滤包含日期的文件名
【发布时间】:2018-02-16 18:51:18
【问题描述】:

我有数千个采用这种命名格式的文件:

cdr_ABSHCECLUSTER_02_201709072214_987392

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

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

stat 显示选项: 使用权 修改的 改变了

 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

如果有任何见解,我将不胜感激。谢谢!

【问题讨论】:

  • 看看shell Parameter Expansion 操作符,它可用于使用模式提取变量的一部分。

标签: regex linux bash shell scripting


【解决方案1】:

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

从变量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

但是您的脚本需要month 格式为Mmm (date +%b),所以稍作调整...

# 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)

【讨论】:

    【解决方案2】:

    这个脚本就是你所需要的。

    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)"
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-07-15
      • 1970-01-01
      • 2015-02-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-03-27
      相关资源
      最近更新 更多