使用脚本重新组织文件夹 [英] Reorganize folders with a script

查看:147
本文介绍了使用脚本重新组织文件夹的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

过去几年,我有不同类型的软件来组织我的照片和音乐收藏。



问题一:音乐收藏



我希望我的音乐收藏如下: / Artist / Album / files。*

但现在有几个 / A / Artist / Album / files的实例。*



我正在尝试写一个脚本:

1.查找所有包含1个字符的文件夹,

2.将每个单字符文件夹的内容移动到/ Artist /

(Flatten结构和处理文件名中的空格)

  find ./Artists/ -name? -maxthepth 1 -mindepth 1 | mv * ./Artists/ 

让我一半,但它不处理目录名称中的空格,我有点不确定是否有其他的陷阱。



问题b:照片收集



我在这里有一个非常相似的情况,我的大部分收藏看起来像我想要的。
所需:

/照片/ 2009 / 2009-08-07 description / filename.jpg



当前情况:

/Photos/2009/03/21/filename.jpg



我需要一个脚本:

1.找到所有匹配的文件夹/ yyyy / mm / dd /

2.将内容移动到/ yyyy-mm-dd /

3.删除旧文件夹



背景



我相信一个bash脚本会非常适合,但是我也可以在python中准备好一些东西。 >
我正在使用Ubuntu 9.04。



提前谢谢。

解决方案

对于您的第二个请求,请尝试以下操作:

 #!/ bin / bash 
找到$ PHOTO_DIR -regextype posix-extended-type d -regex'。* / [0-9] {4} / [0-9] {2} / [0-9] {2} $'|
同时读取目录; do
newdir =$(echo $ dir | sed's @ / \([0-9] \ {4\} \)/ \([0-9] \ {2 \} \)/ \([0-9] \ {2\} \)$ @ / \1-\2-\3 @')
mv$ dir$ newdir
rmdir$(dirname $ dir)
rmdir$(dirname $(dirname $ dir))
done

这对我的简单测试用例工作正常;要确保它适合您,请在三个操作行( mv 和两次)之前放置一个 echo code> rmdir )。



作为一般的建议,管道查找输出为而读取循环不仅仅是为了脚本 - 它是一个非常强大的一线技术,超越 find -exec 能力。


Through the last years I have different types of software to organize my photo and music collections. This has resulted in differences in directory structure, and I'd like this to be uniform.

Problem a: Music collection

I want my music collection to look like: /Artist/Album/files.*
but now there are several instances of /A/Artist/Album/files.*

I'm trying to write a script which:
1. Find all folders with 1 character,
2. Move contents of each one-character folder, to /Artist/
(Flatten structure, and handle spaces in filenames)

find ./Artists/ -name "?" -maxdepth 1 -mindepth 1 | mv * ./Artists/

Gets me halfway, but it doesn't handle spaces in directory names, and I'm a bit unsure if it has other pitfalls.

Problem b: Photo collection

I have a very similar situation here, most of my collection looks like I want it. Desired:
/Photos/2009/2009-08-07 description/filename.jpg

Current situation:
/Photos/2009/03/21/filename.jpg

I need a script which:
1. Find all folders which match /yyyy/mm/dd/
2. Move content to /yyyy-mm-dd/
3. Delete old folders

Background

I believe a bash script would be well suited, but I am also up and ready for something in python.
I'm using Ubuntu 9.04.

Thank you in advance.

解决方案

For your second request, try something like this:

#!/bin/bash
find $PHOTO_DIR -regextype posix-extended -type d -regex '.*/[0-9]{4}/[0-9]{2}/[0-9]{2}$' |
while read dir; do
    newdir="$(echo $dir | sed 's@/\([0-9]\{4\}\)/\([0-9]\{2\}\)/\([0-9]\{2\}\)$@/\1-\2-\3@')"
    mv "$dir" "$newdir"
    rmdir "$(dirname $dir)"
    rmdir "$(dirname $(dirname $dir))"
done

This worked fine for my simple test case; to make sure it works right for you, put an echo in front of the three action lines (mv and twice rmdir).

As a bit of general advice, piping find's output into a while read loop isn't just for scripts - it's a very powerful one-liner technique for going beyond find's -exec capability.

这篇关于使用脚本重新组织文件夹的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆