英文:
Move (cover) file into folder with the same name at a different location
问题
不幸的是,我尚未找到解决方案。我想要遍历一个文件夹(包括子文件夹),找到所有文件名中包含"cover"或"Cover"的文件,然后将它们移动到与其当前父文件夹同名的文件夹中,但这个新文件夹位于不同的位置。所以
第一个位置:
/mnt/folder1/
-- 子文件夹1
---- file.mp3
---- cover.jpg
-- 子文件夹2
---- file2.mp3
---- cover1.jpg
第二个位置:
/mnt/test/folder2
-- 子文件夹1
---- otherfile.mp3
---- cover from subfolder 1 goes here
-- 子文件夹2
---- otherfile2.mp3
---- cover from subfolder 1 goes here
封面文件应该从第一个位置移动到第二个位置。我已经找出了如何找到所有文件(find . -type f -name "*Cover*.*"
),但不知道如何移动它们,因为我不知道如何将这个命令转换成"for f in *cover*.*; do"
的格式。
感谢您的指导!
英文:
unfortunately I have not found a solution for this yet. I would like to run through a folder (incl. subfolders) to find all files with "cover" or "Cover" in the name and then move those into a folder with the same name as its current parent folder, but that new folder is in a different location. So
1st location:
/mnt/folder1/
-- subfolder 1
---- file.mp3
---- cover.jpg
-- subfolder 2
---- file2.mp3
---- cover1.jpg
2nd location:
/mnt/test/folder2
-- subfolder 1
---- otherfile.mp3
---- **cover from subfolder 1 goes here**
-- subfolder 2
---- otherfile2.mp3
---- **cover from subfolder 1 goes here**
The cover files should go from the first location to the second location. I have found out how I can find all the files (find . -type f -name "*Cover*.*"
) but no idea how to move them, as I cannot understand, how I bring this command into the "for f in *cover*.*; do"
format.
Thanks for some guidance!
答案1
得分: 3
Here is the translated code portion:
使用绝对路径:
src='/mnt/folder1/'
dst='/mnt/test/folder2'
(
cd "$src" &&
find -type f -name '*[Cc]over*' \
-exec echo mv -iv -- {} "$dst"/{} \;
)
这将显示将要运行的 mv
命令。
要实际移动文件,请删除 echo
。
更常见的做法是使用 while
而不是 for
。类似于:
find ... | while read var; do ...; done
find
的 -exec
选项就像这个的更好版本,因为它不会被文件名中的奇怪字符打破。
Bash 还有 nullglob
和 globstar
选项,可以直接允许 for
解决方案:
src='/mnt/folder1/'
dst='/mnt/test/folder2'
(
shopt -s nullglob globstar
cd "$src" &&
for f in **/*[Cc]over*; do
echo mv -iv -- "$f" "$dst/$f"
done
)
英文:
With absolute paths:
src='/mnt/folder1/'
dst='/mnt/test/folder2'
(
cd "$src" &&
find -type f -name '*[Cc]over*' \
-exec echo mv -iv -- {} "$dst"/{} \;
)
This will display the mv
commands that would be run.
To actually move the files, remove echo
.
It would be more common to use while
than for
. Something like:
find ... | while read var; do ...; done
find
's -exec
option is like a better version of this as it is not broken by strange characters in the filenames.
Bash also has nullglob
and globstar
options which would allow a for
solution directly:
src='/mnt/folder1/'
dst='/mnt/test/folder2'
(
shopt -s nullglob globstar
cd "$src" &&
for f in **/*[Cc]over*; do
echo mv -iv -- "$f" "$dst/$f"
done
)
</details>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论