将具有相同名称的所有文件移动到一个以该名称命名的新目录中。

huangapple go评论64阅读模式
英文:

Bash move all files with same name to a new directory with that name

问题

在我的`Plex`电影目录中,我想将所有媒体和字幕文件移动到它们自己的目录中,而不是都放在同一个文件夹中。视频文件类型并不都相同。所以目前电影目录看起来像这样:

    Alita Battle Angel (2019).en.srt
    Alita Battle Angel (2019).m4v
    Anchorman The Legend of Ron Burgundy (2004).mp4
    Anchorman The Legend of Ron Burgundy (2004).srt
    Ant-Man (2015).en.srt
    Ant-Man (2015).m4v
    Ant-Man and the Wasp (2018).en.srt
    Ant-Man and the Wasp (2018).m4v
    Aquaman (2018).en.srt
    Aquaman (2018).m4v
    Argo (2012).en.forced.srt
    Argo (2012).en.srt
    Argo (2012).m4v
    ...

我想将每个视频文件和任何字幕文件移动到同名的目录中:

    Alita Battle Angel (2019)
    Anchorman The Legend of Ron Burgundy (2004)
    Ant-Man (2015)
    ...

任何帮助将不胜感激。

我看了其他类似的问题,这个脚本在测试目录上有点起作用,但我不想让它弄乱我的库,如果有更好的方法的话:

    for f in *; do
    [[ -f "$f" ]] || continue
      dir="${f%.*}"
      mkdir "$dir"
      mv "$f" "$dir"
    done
英文:

In my Plex movies directory I want to move all my media and subtitle files to their own directories rather than being in all in that folder. The video file types are not all the same. So at the moment the Movies directory looks something like this:

Alita Battle Angel (2019).en.srt
Alita Battle Angel (2019).m4v
Anchorman The Legend of Ron Burgundy (2004).mp4
Anchorman The Legend of Ron Burgundy (2004).srt
Ant-Man (2015).en.srt
Ant-Man (2015).m4v
Ant-Man and the Wasp (2018).en.srt
Ant-Man and the Wasp (2018).m4v
Aquaman (2018).en.srt
Aquaman (2018).m4v
Argo (2012).en.forced.srt
Argo (2012).en.srt
Argo (2012).m4v
...

I want to move each video file and any subtitle files to a directory with the same name:

Alita Battle Angel (2019)
Anchorman The Legend of Ron Burgundy (2004)
Ant-Man (2015)
...

Any help would be appreciated.

I've looked at other similar problems and this script is sort of working on a test directory but I don't want it mess up my library if there is a better way:

for f in *; do
[[ -f "$f" ]] || continue
  dir="${f%.*}"
  mkdir "$dir"
  mv "$f" "$dir"
done

答案1

得分: 1

    for file in *; do
        dir="${file%)*})"
        mkdir -p "$dir"
        mv "$file" "$dir/"
    done

请在满意输出后移除 echo 语句。

英文:

With [tag:bash]:

for file in *; do
    dir="${file%)*})"
    echo mkdir -p "$dir"
    echo mv "$file" "$dir/"
done

Remove echo statement when you are happy with the output.

Output:

$ tree
.
├── Alita Battle Angel (2019)
│   ├── Alita Battle Angel (2019).en.srt
│   └── Alita Battle Angel (2019).m4v
├── Anchorman The Legend of Ron Burgundy (2004)
│   ├── Anchorman The Legend of Ron Burgundy (2004).mp4
│   └── Anchorman The Legend of Ron Burgundy (2004).srt
├── Ant-Man (2015)
│   ├── Ant-Man (2015).en.srt
│   └── Ant-Man (2015).m4v
├── Ant-Man and the Wasp (2018)
│   ├── Ant-Man and the Wasp (2018).en.srt
│   └── Ant-Man and the Wasp (2018).m4v
├── Aquaman (2018)
│   ├── Aquaman (2018).en.srt
│   └── Aquaman (2018).m4v
└── Argo (2012)
    ├── Argo (2012).en.forced.srt
    ├── Argo (2012).en.srt
    └── Argo (2012).m4v

6 directories, 13 files

huangapple
  • 本文由 发表于 2023年3月23日 08:59:06
  • 转载请务必保留本文链接:https://go.coder-hub.com/75818445.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定