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

时间:2018-07-02 18:59:58

标签: bash macos awk terminal find

我已经使用<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>命令移动了一堆文件。所以最后我有一个包含如下内容的文本文件: `

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之前的字符串。匹配条件为-->

任何帮助将不胜感激。

@jeremysprofile

我的文件可以在这里here

在我的Mac上,我使用了以下代码:

filename

结果的第一行是:

#!/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

我在做什么错了?

1 个答案:

答案 0 :(得分:5)

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

我们进行子字符串删除,其中%将所有内容保留在子字符串之前(因此我们将所有内容保留在 --> 之前,而#将所有内容保留在子字符串之后(因此我们将所有内容保留在{{1之后) }}),其中 --> 为多字符通配符。

Here是bash“用于文件中的行”语法的工作方式。