我的媒体目录结构如下:
~ $ tree baz
baz
├── Ajin [Season 1]
│ ├── Ajin Demi-Human - 01 - A Topic That Has Nothing to Do with Us.mkv
│ ├── Ajin Demi-Human - 02 - Why Is This Happening to Me I Didn`t Do Anything Wrong!.mkv
...snip...
├── Btooom!
│ ├── Btooom! - 01 - Start.mkv
│ ├── Btooom! - 02 - The Bloodstained High School Girl.mkv
...snip...
└── Claymore [Dual Audio]
├── checksums.md5
├── Claymore - 01 - Great Sword.mkv
├── Claymore - 02 - The Black Card.mkv
├── Claymore - 03 - The Darkness in Paradise.mkv
...snip...
3 directories, 53 files
我需要遍历baz
目录,并为每个子目录创建一个名为“Season 01”的子子目录,并将所有文件(和任何目录)放入新创建的子子目录“Season 01”中。
例如上面将变成:
baz
├── Ajin [Season 1]
│ ├── Season 01
│ ├── Ajin Demi-Human - 01 - A Topic That Has Nothing to Do with Us.mkv
│ ├── Ajin Demi-Human - 02 - Why Is This Happening to Me I Didn`t Do Anything Wrong!.mkv
...snip...
etc
我试图用来find
完成此任务,但我无法弄清楚如何构造 -exec 命令来创建“Season 01”目录,并将该目录的内容移动到新目录。
谢谢
答案1
您所需要的只是一个简单的循环。
cur=$(pwd)
for i in baz/*
do
cd "$i"
mkdir season1
mv *.mkv season1
cd "$cur"
done
答案2
使用bash
:
shopt -s dotglob
for i in baz/*/*; do
mkdir -p "${i%/*}/Season 01"
mv "$i" "${i%/*}/Season 01"
done
首先启用dotglob
也匹配隐藏文件。然后循环遍历baz
和子目录内的文件
Season 01
如果该目录尚不存在,则创建该目录。参数扩展${i%/*}
删除文件名并保留父目录(例如baz/Ajin [Season 1]
)。- 将当前文件移动
"$i"
到 season 目录中。