在目录结构 YYYY-MM-DD 中对文件进行分类,使用 Automator 中的脚本,从文件名中获取它

问题描述 投票:0回答:1

拥有没有元数据日期的媒体(图片和视频),但以这种日期结构命名(YYYYMMDD_HHMMSS.jpg/.mov/.png...),我试图将它们分类在名为日期的文件夹中,年-月-日。

我在 Mac 中的 bash 版本: GNU bash,版本 3.2.57(1)-release (x86_64-apple-darwin22) 版权所有 (C) 2007 Free Software Foundation, Inc.

我的类型 -ap bash:bash 是 /bin/bash

我正在尝试这个:

#!/usr/bin/env bash
dest_dir=/Users/myuser/photo-archive
for file in /Users/myuser/photo-archive/Blackhole/*.jpg; do
  [[ -f $file ]] || continue  # look at regular files only
  if [[ $file =~ ^([[:digit:]]{4})([[:digit:]]{2})([[:digit:]]{2}) ]]; then
    year=${BASH_REMATCH[1]}
    month=${BASH_REMATCH[2]}
    day=${BASH_REMATCH[3]}
    destination_dir=$dest_dir/$year-$month-$day
    mkdir -p "$destination_dir"
    echo "Moving $file to $destination_dir"
    mv "$file" "$destination_dir"
  fi
done

但我什么也没得到。

知道错误可能出在哪里吗?我超级迷失。

谢谢

bash macos shell automator
1个回答
0
投票

您忘记了

file
包含完整路径。改变

if [[ $file =~ ^([[:digit:]]{4})([[:digit:]]{2})([[:digit:]]{2}) ]]; then

if [[ ${file##*/} =~ ^([[:digit:]]{4})([[:digit:]]{2})([[:digit:]]{2}) ]]; then

它会起作用的。

© www.soinside.com 2019 - 2024. All rights reserved.