bash 如何使用文件名创建文件夹,然后将文件移动到文件夹中?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2172420/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 21:37:23  来源:igfitidea点击:

How to create folders using file names and then move files into folders?

pythonperlbashunixbatch-file

提问by jrara

I have hundreds of text files in a folder named using this kind of naming convention:

我在使用这种命名约定命名的文件夹中有数百个文本文件:

Bandname1 - song1.txt
Bandname1 - song2.txt
Bandname2 - song1.txt
Bandname2 - song2.txt
Bandname2 - song3.txt
Bandname3 - song1.txt
..etc.

I would like to create folders for different bands and move according text files into these folders. How could I achieve this using bash, perl or python script?

我想为不同的乐队创建文件夹并根据文本文件移动到这些文件夹中。我如何使用 bash、perl 或 python 脚本实现这一目标?

采纳答案by bjlaub

gregseth's answer will work, just replace trimwith xargs. You could also eliminate the iftest by just using mkdir -p, for example:

gregseth的答案会起作用,只需替换trimxargs. 您还可以if仅使用 来消除测试mkdir -p,例如:

for f in *.txt; do
    band=$(echo "$f" | cut -d'-' -f1 | xargs)
    mkdir -p "$band"
    mv "$f" "$band"
done

Strictly speaking the trimor xargsshouldn't even be necessary, but xargswill at least remove any extra formatting, so it doesn't hurt.

严格来说,trimxargs什至不应该是必要的,但xargs至少会删除任何额外的格式,所以它不会受到伤害。

回答by Paused until further notice.

It's not necessary to use trim or xargs:

没有必要使用trim或xargs:

for f in *.txt; do
    band=${f% - *}
    mkdir -p "$band"
    mv "$f" "$band"
done

回答by ghostdog74

with Perl

使用 Perl

use File::Copy move;
while (my $file= <*.txt> ){
    my ($band,$others) = split /\s+-\s+/ ,$file ;
    mkdir $band;
    move($file, $band);
}

回答by John Feminella

You asked for a specific script, but if this is for organizing your music, you might want to check out EasyTAG. It has extremely specific and powerful rules that you can customize to organize your music however you want:

您要求提供特定脚本,但如果这是为了组织您的音乐,您可能需要查看EasyTAG。它具有非常具体和强大的规则,您可以自定义这些规则来组织您想要的音乐:

alt text
(source: sourceforge.net)

替代文字
(来源:sourceforge.net

This rule says, "assume my file names are in the structure "[artist] - [album title]/[track number] - [title]". Then you can tag them as such, or move the files around to any new pattern, or do pretty much anything else.

这条规则说,“假设我的文件名在结构“[艺术家] - [专辑名称]/[曲目编号] - [标题]”中。然后你可以这样标记它们,或者将文件移动到任何新模式,或者几乎可以做任何其他事情。

回答by gregseth

How about this:

这个怎么样:

for f in *.txt
do
  band=$(echo "$f" | cut -d'-' -f1 | trim)
  if [ -d "$band" ]
  then
    mkdir "$band"
  fi
  mv "$f" "$band"
done

回答by Michael Easter

This Python program assumes that the source files are in dataand that the new directory structure should be in target(and that it already exists).

这个 Python 程序假定源文件在data,并且新的目录结构应该在target(并且它已经存在)。

The key point is that os.path.walkwill traverse the datadirectory structure and call myVisitorfor each file.

关键是os.path.walk会遍历data目录结构并调用myVisitor每个文件。

import os
import os.path

sourceDir = "data"
targetDir = "target"

def myVisitor(arg, dirname, names):
    for file in names:
        bandDir = file.split("-")[0]
        newDir = os.path.join(targetDir, bandDir)
        if (not os.path.exists(newDir)):
            os.mkdir(newDir)

        newName = os.path.join(newDir, file)
        oldName = os.path.join(dirname, file)

        os.rename(oldName, newName)

os.path.walk(sourceDir, myVisitor, None)

回答by jfs

ls |perl -lne'$f=$_; s/(.+?) - [^-]*\.txt//; mkdir unless -d; rename $f, "$_/$f"'