删除自动生成的时间戳文件夹并保留内容

Remove auto generated timestamp-folders and keep content

我有 10 年的文件夹,包含 12 个月和 30 天。但是,在日期文件夹之后会出现一个自动生成的时间戳文件夹。

/2016/11/25/08151949/image.jpg

我需要删除“08151949”文件夹但保留内容。

/2016/11/25/image.jpg

因为我有大约。 3600 timestamp-folder 我需要使该任务自动化。一个额外的问题是我如何构建一个 301 重定向来保持 url 的存活。

假设您的时间戳始终具有相同的结构(在本例中为 8 位数字)...您可以尝试创建一个 restructure.bash 脚本,如下所示:

#!/usr/bin/env bash

# Config variables
CONTENT_TO_KEEP="image.jpg"
START_PATH="/"

is_folder_timestamp() {
  local FOLDER_NAME=""
  # The following with match any 8-digit string, feel free to edit the regex though
  # Reference: 'man grep'; Search for 'ERE'
  local TIMESTAMP_FORMAT_REGEX='[:digit:]{8}'
  
}

# Go to start path
pushd $START_PATH


for year in *; do
  if [ -d $year ]; then
    pushd $year

    for month in *; do
      if [ -d $month ]; then
        pushd $month

        for day in *; do
          if [ -d $day ]; then
            pushd $day

              for element in *; do 
                if [ -d $element ] && `is_folder_timestamp "$element"`; then
                  # At first, I'd suggest you comment the two lines following
                  # the echo to dry-run the script and make sure you're not
                  # deleting everything ;)
                  echo "will keep content here: $pwd and rmdir $element"
                  mv $element/$CONTENT_TO_KEEP .
                  rmdir $element
                fi
              done

            popd #day
          fi
        done

        popd #month

      fi
    done

    popd # year
  fi
done

# Return to call location
popd

Note that you could do without the if [ -d $<VAR> ]; validations when you process the year/month/day if your structure only has directories.
In my experience though, they don't hurt much and it keeps the script from crashing if some file event appeared in the folders ;)

由于你的问题被标记为 python,我想你可以使用 python 调用脚本 :)


关于你的奖金问题,也许你可以添加一些关于你的网络服务器的确切性质的细节运行?

希望对您有所帮助!