My solution is fairly simple, probably not the best and/or shortest, but the job is now done, and I can reformat my HDD.
The steps can be summarized as follows:
1) ls the directory with all the folders that you need to move and send the output to a .txt file. Before you ask why didn't I just move the parent folder? Well, to put simply, I had errors of some sort...so I had to do it the long way
2) Using 9 lines of python and the 'os' library, read the output.txt file and add "s to the start and end of every line. Close output.txt
3) Use nawk to read the updated output.txt file and move the folders (with spaces...grr) to the desired location.
Let's get started!
So here's the code and how I've done it. For the purpose of this example, I will compare the Music folder on my media drive and the Music folder on my desktop - each of which contain several sub-folders of artists. But if you want to just mv folders, then just skip some of the steps.
1)
~$ ls ~/Music > ~/computer-ls.txt
1a)
This is optional. Since I want to compare the contents of ~/Music to /media/Music and move only those files that are in ~/Music but not in /media/Music, do this step - use diff to compare the .txt files, grep > or <, cut off the > and < (to match folder names), and then send the output to a file:
~$ ls /media/Music > ~/media-ls.txt
~$ diff ~/computer-ls.txt ~/media.txt | grep "<" | cut -b3- > move.txt
~$ diff ~/computer-ls.txt ~/media.txt | grep "<" | cut -b3- > move2.txt
When using diff, the output is read such that which ever way the arrow is pointing, then that indicates that the string is in that .txt file but not in the other, i.e. using the code above, if I get output < Ramones, then I know that the Ramones folder is in ~/computer-ls.txt but not in media-ls.txt.
Two move.txt files need to be created because later, python cannot read from a file opened with the 'w' permission and vice versa.
If you wish, review the .txt files and the folders themselves to see if it is true about which folders are missing.
2) Skip this step - python not needed.
The python bit. Python should come installed on your Linux flavour, if not, apt-get install python :). Open a python cli, then run the following command using the .txt files we just created.
~$ python >>> fread = open('~/move.txt', 'r') >>> fwrite = open('
~/
move2.txt', 'w') >>> for line in fread: >>> fwrite.writelines(str("\""+line[:-1]+"\"\n")) >>> >>> fread.close() >>> fwrite.close() >>> quit()
The move2.txt file should now have the same folder names as move.txt, but will have "s at the start and end of ever line. "like this"
3) See update below.
Nawk bit. You should have done all this in the same directory, just don't forget to clean up :P. The last command uses nawk (a file scanner which can do a lot more!) to read the move2.txt file, and then run the mv command.
~$ nawk '{ system("mv -v " $0 " /media/Music/") }'
~/
move2.txt
Make sure to give the correct file - the one that we changed.
Simple break-down (since I'm still an amateur) - if easier, maybe imagine nawk is like a 'for i in array/file/variable' command:
- nawk > the nawk app
- '{ command }' > everything inside the single quotes and curlies is what you want to do, spacing is critical here
- system() > tells nawk to run it in termina
- $0 > tells nawk to get the whole line. $1, $2, $3 etc. can be used to read only a single component of a line where the components may be separated by a space (grr). i.e. if the code above used $1 and a folder was called "Black Sabbath", then the output would be Black and we would fail to mv the folder.
Hope this helps! Till next time, Ciao!
Update:
Step 2) not needed. "s can be inserted in the nawk step as follows:
~$ nawk '{ system("mv -v \"" $0 "\" /media/Music/") }'
~/
move.txt
No comments:
Post a Comment