compare 2 filenames & move one to other's directory
I have code that compares two files, & if equal, touches one with the other, so it has the same date.
This time, I'd like to compare the file names only, without the extension, and if equal, move one to the other's subdirectory. This process makes the comparison for all files in two directories, iterating through one directory to find a match for each in the other directory, which it also iterates through, for a nested loop. FILES=$( find ./nu/Mrj -type f ); for f in $FILES; do for g in ./ol/Mrj/*; do echo "f is $f g is $g"; if cmp "$f" "$g"; then echo "match"; touch -r $g $f; else echo "no match"; fi; done; done Thanks in advance |
test is probably what is needed.
Code:
FILES=$( find ./nu/Mrj -type f ); |
Quote:
Now I need to extract the filename independent of the extension to make the comparison. If the FN's match (regardless of extension), then move (or copy, doesn't matter) one file to the same directory as the other. This code separates FN from EXT. Code:
which seems cumbersome. I could try a simple mv, but I need to keep both files. Got it !! .. Duplicate the directory, then use mv to overwrite the (let's say) TXT files in one directory with MP3 files in the other. Then MERGE the directory of TXT files with the directory of MP3 files !! Perfect, thank you. |
another way to get extension using bash internals
Code:
$ filename=hommer.txt Code:
ext=${FILE##*.} to get file name w/o ext Code:
|
THIS WORKS PERFECTLY, EXCEPT FOR THE LINE
Code:
mv $g $f; Code:
#!/bin/bash But then I add the Code:
"mv $g $f;" Code:
mv: ./txtMp3s/mp3s/*: No such file or directory Thoughts much appreciated !! |
Code:
FILES=$( find ./txtMp3s/txts -type f ) https://mywiki.wooledge.org/BashGuide/Arrays that explains why Code:
mv $g $f do you really need xPrefix vars? Code:
#!/bin/bash https://mywiki.wooledge.org/BashGuid...eter_Expansion see that page to understand the ${f%.*} and ${f%/*}/ # Edit, Doh, this will never match, since path is different #[[ ${f%.*} == ${g%.*} ]] \ filename="${f##*/}" # strips path # Edit2, this is not great either #[[ ${g%.*} =~ ${filename%.*}$ ]] it is only checking the end matches tune_1.mp3 remix_tune_1.mp3 tune_1 could overwrite the remix version adding a leading slash should fix it [[ ${g%.*} =~ /${filename%.*}$ ]] see the section on Regular Expressions https://mywiki.wooledge.org/BashGuide/Patterns to understand the =~ bit |
It strips files down to just the name, then compares the two, then creates subdir within the same place the file is found, then moves dup file into same dir match in the subdir of the path. keeps a running total of match, no match, and a running list to where each individual file was moved to.
Even though you do not specify between dirs what goes where and from where. Not good programing logic. It is an incomplete thought. "This time, I'd like to compare the file names only, without the extension, and if equal, move one to the other's subdirectory." which one goes where?????? Code:
#!/usr/bin/env bash for loops vs while loops: So between you, Firerat and mine you see that there is more than one way to get this done. But it is all still the same basic logical flow. If you have more than one dup of the "same file" then as suggest just add a number to the end of the file. Though you'd have to check save dir for a match to that file before moving it into the same dir, if match, then add a number to the file, then move the file into the same save dir to prevent over writing of the two files. Which would also require that you get last number saved so you do not over write that part of the file in the process. Instead increment one more than last number of same file saved. This way you can then run diff on the matching files to see if contents within the files are too dups or not to determine what to then do with them. output sample Code:
Files Matched is 98, Not matched 36914 |
reply to BW-userx
Thanks, Firerat & BW-userx, for detailed comments,
which will take me some time to work through. I like the back-to-basics bootcamp approach. Some of my results are just bizarre. I end up with a target directory whose size doesn't add up to its contents. Directory has only TXT files with kilobyte-range sizes, but the total directory size seems to reflect the presence of megabyte-size MP3's, which I tried to MV there but that do not show up when I look at directory contents. Moreover, these MP3s are not in their original directory, in the recycle bin, or anywhere in the entire system, wtf, as far as I can tell. |
I do not fully understand your situation in what now are you saying, mp3 size files everywhere including the recyle bin. are your find compare paths the entire system?
you show two dirs Code:
/txtMp3s/mp3s/ which would in your case either be in /txtMp3s/mp3s/subdir /txtMp3s/txts/subdir what relationship does the txt files have with the mp3 files? did you know you can get meta data off of mp3s and sort them out using that? providing they have the metadata written to them. artist,title,album etc... |
All times are GMT -5. The time now is 08:26 PM. |