【发布时间】:2020-02-10 19:43:46
【问题描述】:
我正在尝试按艺术家和名称对所有 mp3 文件进行排序。目前,它们位于 1 个巨大的文件名中。 例如艺术家 - 歌曲名称.mp3 我想将其转换为 艺术家/歌曲名称.mp3
这是我迄今为止尝试过的。在这种情况下,我使用了一个名为“hi\ -\ hey”的测试文件:
#!/bin/bash
# filters by all .mp3 extensions in the current working directory
for f in *.mp3; do
# extract artist and song name and remove spaces
artist=${f% -*}
song=${f#*- }
#make directory with the extracted artist name and move + rename the file into the directory
mkdir -p $artist
mv $f $artist/$song;
done
出于某种原因,除了大量错误之外,它还使用歌曲名称而不是艺术家创建了一个目录:
mv: cannot move 'hey.mp3' to a subdirectory of itself, 'hey.mp3/hey.mp3'
mv: cannot stat 'hi': No such file or directory
mv: cannot stat '-': No such file or directory
mv: 'hey.mp3/hi' and 'hey.mp3/hi' are the same file
mv: cannot stat '-': No such file or directory
【问题讨论】:
-
引用你的变量。
-
如果尝试,您可能会得到提示: echo mv $f $artist/$song
-
如果您的艺术家姓名目录已经创建,perl rename 非常方便且更短:
prename 's/ *- */\//' *.mp3。这会将所有"artist - name.mp3"重命名为"artist/name.mp3"。 -
尝试使用这样的引号:artist="${f% -*}" // mkdir -p "$artist" // etc.
-
考虑使用
rename- 它可以创建目录、进行试运行、计算和拆分任何你想要的东西stackoverflow.com/a/49412361/2836621