
私は数ヶ月間これをやろうとしましたが、うまくいきません。 bashでこれをやろうとしています。すべてのファイルはLinuxシステムにあるので、bash
推測できますか?
私がやりたいことは、ディレクトリ内のすべてのファイルの次の基準に従って名前を変更することです。
- ファイル名に角かっこが含まれている場合は、角かっこを削除して数字を含めます。
[312646416198]
- ファイルに年がある場合は、
(2018)
その年を維持します(すべてのファイルに年があるわけではありません)。 - ファイルの角かっこ内に数字が 1 つしかない場合は、
(1)
角かっこと数字を削除します。
ファイル名の最初の部分(つまり、最初のハイフン「-」の前のすべての項目)に基づいてフォルダを作成し、ファイルを作成したフォルダに移動します。
たとえば、一部の処理後、次の名前は(理想的には)次のようになります。著者の前にタイトルが表示されるなど、一部の項目が誤って配置される可能性があるため、新しいフォルダの名前は作成者ではなくタイトルとして指定されますが、私はその内容を受け入れることができますが、そのうち少数だけがそのように名前を付けます。
だからこれ:
The Brotherhood of the Rose - David Morrell.epub
Abbi Glines - Bad for You (2014) [9781481420761] (1).epub
Kristin Hannah - The Great Alone (2018) [9781250165619].epub
Stephanie Dray, Laura Kamoie - America's First Daughter - A Novel (2016) [9780062347268] (1).epub
Terence Hanbury White - The Once and Future King (1987) [9780441627400] (1).epub
次のようになります。
The Brotherhood of the Rose
The Brotherhood of the Rose - David Morrell.epub
Abbi Glines
Abbi Glines - Bad for You (2014).epub
Kristin Hannah
Kristin Hannah - The Great Alone (2018).epub
Stephanie Dray, Laura Kamoie
Stephanie Dray, Laura Kamoie - America's First Daughter - A Novel (2016).epub
Terence Hanbury White
Terence Hanbury White - The Once and Future King (1987).epub
答え1
これは役に立ちます:
$ cat epub-cleanup.sh
#! /bin/bash
for i in *.epub; do
mv -iv "$i" "$(echo "$i" | sed -r 's/\[[0-9]+\]//;s/\([0-9]\)//;s/[ ]*.epub/.epub/')"
done
- [0123456789]の単一インスタンスの削除
- (1)の単一インスタンスの削除
- ファイル拡張子の前のスペースを削除する
答え2
zsh
代わりにシェルを使用しますbash
。
set -o extendedglob
for file (*' - '*.epub) {
newfile=${file// #(\[<->\]|\((<->~<1000-2020>)\))}
dir=${newfile%% - *}
mkdir -p -- $dir &&
mv -i -- $file $dir/$newfile
}
(number)
数字が範囲外の場合にのみsを削除してください1000-2020
。
$ tree
.
├── Abbi Glines - Bad for You (2014) [9781481420761] (1).epub
├── Kristin Hannah - The Great Alone (2018) [9781250165619].epub
├── Stephanie Dray, Laura Kamoie - America's First Daughter - A Novel (2016) [9780062347268] (1).epub
├── Terence Hanbury White - The Once and Future King (1987) [9780441627400] (1).epub
└── The Brotherhood of the Rose - David Morrell.epub
0 directories, 5 files
$ zsh ~/that-script
$ tree
.
├── Abbi Glines
│ └── Abbi Glines - Bad for You (2014).epub
├── Kristin Hannah
│ └── Kristin Hannah - The Great Alone (2018).epub
├── Stephanie Dray, Laura Kamoie
│ └── Stephanie Dray, Laura Kamoie - America's First Daughter - A Novel (2016).epub
├── Terence Hanbury White
│ └── Terence Hanbury White - The Once and Future King (1987).epub
└── The Brotherhood of the Rose
└── The Brotherhood of the Rose - David Morrell.epub
5 directories, 5 files