我有一个基于日期的文件夹结构,在日期文件夹之后有一个自动生成的时间戳文件夹。
/2016/11/25/08151949/image.jpg
我需要删除“08151949”文件夹但保留内容。
/2016/11/25/image.jpg
因为我有大约。 3600 时间戳文件夹 我需要自动化该任务。一个额外的问题是我如何构建一个 301 重定向来保持 url 的活动。
假设您的时间戳始终具有相同的结构(在本例中为 8 位数字)...您可以尝试创建一个如下所示的
restructure.bash
脚本:
#!/usr/bin/env bash
# Config variables
CONTENT_TO_KEEP="image.jpg"
START_PATH="/"
is_folder_timestamp() {
local FOLDER_NAME="$1"
# 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
请注意,如果您的结构只有目录,则在处理
年/月/日时,您可以不进行if [ -d $<VAR> ];
验证。
但根据我的经验,它们不会造成太大伤害,并且如果文件夹中出现某些文件事件,它可以防止脚本崩溃;)
既然你的问题被标记为
python
,我想你可以使用python调用脚本:)
关于您的额外问题,也许您可以添加一些有关您正在运行的网络服务器的确切性质的详细信息?
希望有帮助!