2

I have moved a bunch of files with the mv -v command. So in the end i have a text file containing something like this: `

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

`

I need to move each file from Downloads back to its original location based on this text file. The original location would be the string before the --> inside my text file. The matching criteria is the filename.

Any help would be much appreciated.

@jeremysprofile

My file can be found here here

On my mac, I have used the following code:

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

The first lines of the result are:

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

I want to mention that the /Volume/Public is correctly mapped on my computer and that the files are existing in the from_tv_shows location:

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$ 

What am I doing wrong?

WYSIWYG
  • 137
  • 2
  • 2
  • 7

1 Answers1

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

We do substring removal, where % keeps everything before the substring (so we keep everything before --> ), and # keeps everything after (so we keep everything after --> ), with * as the multi-character wildcard.

Here is how bash "for line in file" syntax works.

chepner
  • 497,756
  • 71
  • 530
  • 681
jeremysprofile
  • 10,028
  • 4
  • 33
  • 53
  • @hek2mgl That was easily fixed. – chepner Jul 02 '18 at 19:30
  • I've created myself a script `myMove.sh` and tried first to **echo** the `$origin` and `$current`; in order to see if it works, before actually doing the move. And it does not seem to work. Both variables are giving me the whole line. – WYSIWYG Jul 03 '18 at 09:02
  • @nbnlko, tested and verified; works on my machine with the sample text provided. – jeremysprofile Jul 03 '18 at 15:34
  • @jeremysprofile I have updated the question with my attempts, and also provided a link to the text file with the original names inside. – WYSIWYG Jul 03 '18 at 16:43
  • @nbnlko, as you can see from your logs, your text file uses `->` as the delimiter, not `-->` as you originally said. If you change `-->` in my answer to `->`, it will work. – jeremysprofile Jul 03 '18 at 17:03
  • @jeremysprofile , thank you so much for opening my eyes! Your solution was perfect! Also, thank you for this link http://wiki.bash-hackers.org/syntax/pe#substring_removalhttp://wiki.bash-hackers.org/syntax/pe#substring_removal ; it is very useful! – WYSIWYG Jul 03 '18 at 17:28
  • @nbnlko, if my solution worked, please accept my answer (you will earn a bit of reputation for this action) by clicking on the grey checkmark under the arrows next to my answer. – jeremysprofile Jul 03 '18 at 17:45