【问题标题】:Is there a way to organise video files by month in new directories?有没有办法在新目录中按月组织视频文件?
【发布时间】:2026-01-22 01:10:01
【问题描述】:

我对 bash 比较陌生。我想知道 Mac OS 终端上是否有一个现有命令可以在新目录中按月分隔视频文件。从本质上讲,这意味着如果我在 12 个月内有视频,我将有 12 个新目录,其中包含视频。

否则,我想知道是否有另一种方法可以通过 python 来解决此类问题。

我希望用它来处理 500 多个视频文件。如果我可以为我编写一个脚本而不是一个一个地浏览它们,那将极大地帮助我。

脚本之前

脚本后(期望的输出)

更新(找到解决方案)

我最终找到了解决方案,感谢您引导我找到正确的答案。现在我今天学到了一件新东西

import os, shutil 
from datetime import datetime

filepath = "/Users/alfietorres/Downloads/"      #base file path 
for filename in os.listdir(filepath):           #iterate through each file in the directory

    r = os.stat(filepath+filename)              #look for the file path and the file name
    d = r.st_birthtime                          #look for the time created 
    date=datetime.fromtimestamp(d)              #assign the details to a date variable

    month_directory = filepath+str(date.month)+"-"+str(date.year)      #use the date variable to create a UNIQUE new directory 
    file_being_moved = filepath+filename        #file the path of the file being moved    

    if os.path.isdir(month_directory) :         #check if the directory is created
        print("directory found ... ")
        shutil.move(file_being_moved,month_directory)   #move the file we are iterating on to the directory
    else: 
        print("creating directory ... ")        
        os.mkdir(month_directory)                       #create new directory 
        shutil.move(file_being_moved,month_directory)   #move items in new directory

【问题讨论】:

  • 是的,这很简单。你怎么知道哪个视频属于哪个月份?它在文件名中吗?元数据?修改时间?
  • 对不起,我又检查了一遍。我再次修复了OP。我发现有一种方法可以按 创建日期 分隔文件。有没有办法自动做到这一点?
  • 创建日期和你从GetFileInfo -d yourfile.mov得到的日期一样吗?
  • 是的,它是输入:GetFileInfo -d IMG_2997\ 2.HEIC 输出:02/15/2020 17:36:59。与获取文件信息 GUI 相同
  • 所以您对.MOV.mov 感兴趣?

标签: python bash macos terminal file-management


【解决方案1】:

你可以编写一个 python 脚本来为你做这件事。

使用os 模块获取有关您的文件的信息。特别是创作时间:

import os
r = os.stat("path/to/file")
print(r.st_ctime_ns)   # it prints the number of nano seconds since creation (Windows OS)

为了获取目录中的文件/目录列表,你也可以使用os模块:

os.listdir("path/to/directory/")  # you get a list of all files/directories

为了将时间戳转换为日期时间,可以使用datetime模块:

from datetime import datetime
d = r.st_ctime_ns // 1000000   # convert to seconds
date = datetime.fromtimestamp(d)
print(date)   ##  datetime.datetime(2019, 3, 19, 5, 37, 22)
print(date.year)  ## get the year
print(date.month)  ## get the month
print(date.day)   ## get the day

现在您只需结合这些信息来组织您的文件。

其他有用信息:

  • 要创建目录,请使用os.mkdir("directory name")
  • 要移动文件,请使用os.rename("old path", "new path")

【讨论】:

    【解决方案2】:

    在新目录中按月分隔视频文件

    来自info date

    ‘-r FILE’
    ‘--reference=FILE’
         Display the date and time of the last modification of FILE, instead
         of the current date and time.
    

    所以从文件的最后一次修改时间获取月份。

    date -r file '+%m'
    

    使用bashfind

    #!/usr/bin/env bash
    
    dirs=("$@")
    
    find "${dirs[@]}" -type f -print0 | {
      while IFS= read -rd '' file; do
        file_name=${file##*/}
        path_name=${file%"$file_name"}
        directory=$(date -r "$file" "+%m")
        if ! [[ -e "${path_name}month$directory" && -d "${path_name}month$directory" ]]; then
          echo mkdir -p "${path_name}month$directory" && echo mv -v "$file" "${path_name}month$directory"
        else
          echo mv -v "$file" "${path_name}month$directory"
        fi
      done
    }
    

    要使用脚本,假设脚本的名称是myscriptfolder_name 是相关目录的名称。

    ./myscript folder_name
    

    或者有多个文件夹/目录

    ./myscript folder1 folder2 folder3 folder4 another_folder /path/to/folder
    
    • -type f 查找文件而不是目录。

    • 如果您对输出满意,请删除所有echo,并将所有mv 更改为cp,只是为了复制文件而不是完全移动它们。

    【讨论】:

      最近更新 更多