bash - 根据文本文件将文件移回原始位置

标签 bash macos awk terminal find

我使用 mv -v 命令移动了一堆文件。所以最后我有一个包含如下内容的文本文件: `

 /Volumes/[TV Shows]/Movie 1.avi --> /Volumes/Downloads/Movie 1.avi
 /Volumes/[TV Shows]/Movie 2.mp4 --> /Volumes/Downloads/Movie 2.mp4

`

我需要根据此文本文件将每个文件从下载移回到其原始位置。原始位置将是我的文本文件中 --> 之前的字符串。匹配条件是文件名

任何帮助将不胜感激。

@jeremys简介

我的文件可以在这里找到 here

在我的 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

我们进行子字符串删除,其中 % 保留子字符串之前的所有内容(因此我们保留 --> 之前的所有内容),而 # 保留之后的所有内容(因此我们保留 --> 之后的所有内容),并使用 * 作为多字符通配符。

Here这就是 bash“for line in file”语法的工作原理。

关于bash - 根据文本文件将文件移回原始位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51142268/

相关文章:

java - 在哪里可以找到 CPLEX 库(不是 AIMMS 的 GUI)?

objective-c - 如果我想编写一个 iPad 应用程序,我必须购买 iPad 吗?

AWK程序求三个州的平均降雨量

bash - 使用 Bash 替换目录/子目录名称中的点或字符

MySQL哈希列密码+脚本bash

python - 在 mac osx 10.9.x 上配置 Django 1.7 和 Python 3

awk - 使用 awk 从文件第一行打印特定字段

bash - 在 bash 中将文本文件中的所有值转换为对数刻度

使用 "&;"时 Bash 意外 token

linux - 你可以用输入 whiptail sudo 吗?