bash - 根据文本文件将文件移回其原始位置
问题描述
我用命令移动了一堆文件mv -v
。所以最后我有一个包含如下内容的文本文件:`
/Volumes/[TV Shows]/Movie 1.avi --> /Volumes/Downloads/Movie 1.avi
/Volumes/[TV Shows]/Movie 2.mp4 --> /Volumes/Downloads/Movie 2.mp4
`
我需要Downloads
根据此文本文件将每个文件从回移动到其原始位置。原始位置将是-->
我的文本文件内部之前的字符串。匹配条件是filename
.
任何帮助将非常感激。
@jeremysprofile
我的文件可以在这里找到
在我的 Mac 上,我使用了以下代码:
#!/bin/bash
while IFS= read -r line; do
#use http://wiki.bash-hackers.org/syntax/pe#substring_removal to grab the parts we want
origin=${line% --> *}
current=${line#* --> }
mv -- "$current" "$origin"
done < ~/Desktop/myTextFile.txt
结果的第一行是:
WYSIWYG:Desktop cip$ ./myMove.sh
mv: rename /Volumes/Public/English/[Sorted TV Shows]/Dimension/Dimension.404.s01e01.WEBDL.720p.NewStudio.TV.mkv -> /Volumes/Public/Downloads/from_tv_shows/Dimension.404.s01e01.WEBDL.720p.NewStudio.TV.mkv to /Volumes/Public/English/[Sorted TV Shows]/Dimension/Dimension.404.s01e01.WEBDL.720p.NewStudio.TV.mkv -> /Volumes/Public/Downloads/from_tv_shows/Dimension.404.s01e01.WEBDL.720p.NewStudio.TV.mkv: No such file or directory
mv: rename /Volumes/Public/English/[Sorted TV Shows]/Dimension/Dimension.404.S01E02.Polybius.HULU.WEBRip.x264-RBB.mp4 -> /Volumes/Public/Downloads/from_tv_shows/Dimension.404.S01E02.Polybius.HULU.WEBRip.x264-RBB.mp4 to /Volumes/Public/English/[Sorted TV Shows]/Dimension/Dimension.404.S01E02.Polybius.HULU.WEBRip.x264-RBB.mp4 -> /Volumes/Public/Downloads/from_tv_shows/Dimension.404.S01E02.Polybius.HULU.WEBRip.x264-RBB.mp4: No such file or directory
mv: rename /Volumes/Public/English/[Sorted TV Shows]/Dimension/Dimension.404.S01E03.Bob.HULU.WEBRip.x264-RBB.mp4 -> /Volumes/Public/Downloads/from_tv_shows/Dimension.404.S01E03.Bob.HULU.WEBRip.x264-RBB.mp4 to /Volumes/Public/English/[Sorted TV Shows]/Dimension/Dimension.404.S01E03.Bob.HULU.WEBRip.x264-RBB.mp4 -> /Volumes/Public/Downloads/from_tv_shows/Dimension.404.S01E03.Bob.HULU.WEBRip.x264-RBB.mp4: No such file or directory
mv: rename /Volumes/Public/English/[Sorted TV Shows]/Dimension/Dimension.404.S01E04.WEBRip x264-RMTeam.mkv -> /Volumes/Public/Downloads/from_tv_shows/Dimension.404.S01E04.WEBRip x264-RMTeam.mkv to /Volumes/Public/English/[Sorted TV Shows]/Dimension/Dimension.404.S01E04.WEBRip x264-RMTeam.mkv -> /Volumes/Public/Downloads/from_tv_shows/Dimension.404.S01E04.WEBRip x264-RMTeam.mkv: No such file or directory
我想提一下,/Volume/Public
已正确映射到我的计算机上,并且文件存在于该from_tv_shows
位置:
WYSIWYG:Desktop cip$ find /Volumes/Public/Downloads/from_tv_shows/ -name 'Dimension.404.S01E04.WEBRip x264-RMTeam.mkv'
/Volumes/Public/Downloads/from_tv_shows//Dimension.404.S01E04.WEBRip x264-RMTeam.mkv
WYSIWYG:Desktop cip$
我究竟做错了什么?
解决方案
while IFS= read -r line; do
#use http://wiki.bash-hackers.org/syntax/pe#substring_removal to grab the parts we want
origin=${line% --> *}
current=${line#* --> }
mv -- "$current" "$origin"
done < myTextFile.txt
我们进行子字符串删除,其中%
保留子字符串之前的所有内容(因此我们保留之前的所有内容 -->
),并#
保留之后的所有内容(因此我们保留之后的所有内容 -->
),*
作为多字符通配符。
以下是 bash“for line in file”语法的工作原理。
推荐阅读
- php - 我的 Laravel 8 应用程序中出现此路由相关错误的原因是什么?
- excel - 比较 VBA 中的负值以产生通过和失败的结果
- nestjs - 如何在nestJs中使用护照本地策略对多个实体进行身份验证?
- python-3.x - wagtailuserbar 在 nginx 生产服务器上给出错误
- sml - (标准 ML)有没有办法直接比较列表的元素?
- typescript - 函数返回类型取决于可选参数
- amazon-web-services - AWS CloudWatch Event 仅运行一个目标
- reactjs - 重新渲染后react-draggable保存元素的位置
- r - spark_write_csv 不再起作用(使用 Sparklyr)
- erlang - 如何在 Erlang 中进行内存映射 IO?