I have a loop comparing strings that it still does not kick it out if it matches. the set up is many different types of file extensions, only want to delete the ones I do not want keeping the ones I do want. easier route is by not knowing what is in the dir is to tell it what I want to keep then have it just delete the rest. it does not work it still deletes everything within the dir and sub dirs. This is how it is basically set up within the script with two loops within it now,

find "$working_dir" -type f -name "*.*" | while [ $xf  -lt $numberToConvert ] ; 
do read FILENAME;

echo "before loop"
echo "path1 is -> "${path1}""
echo "Ext1 is -> "$ext1""

#Checks each movie file to see if it is just a not needed
sample    of the move to regain file space by deleting it


for file in "${path1}" ; do 

echo "in for loop ext1 is -> "$ext1"" 

  if [[ "$ext1" != 'flac' || "$ext1" != 'mp3'  ]]; then

    echo "in loop if statement ext is -> "$ext1""
    echo "Removing "$FILENAME""

    rm -v "$removeme"


if [[ "${ext1}" == 'mp3' || "${ext1}" == 'flac' ]] ; then 
# other code to do stuff to mp3 and flac files here within the outter loop

 #outter loop done statement

output of term is:

before loop
path1 is -> /media/data/temp1/Joe Jackson - The Ultimate Collection/CD2
Ext1 is -> mp3

in for loop ext1 is -> mp3

in loop if statement ext is -> mp3
Removing /media/data/temp1/Joe Jackson - The Ultimate Collection/CD2/07 Joe Jackson - Be My Number Two.mp3
removed ‘/media/data/temp1/Joe Jackson - The Ultimate Collection/CD2/07 Joe Jackson - Be My Number Two.mp3’

Extension of before into first if statement foobar

Total Files Left are 41

the second if statement works it was only been letting in mp3 and flac, and has been working I just decided to try this to get rid of the rest of files that are not mp3 or flac within the directories now, and, I've tried all of the variations that I can think of with the quote marks '$var' "$var" and brackets "${var}" around the var name and the [ ] and [[ ]] on the if statement. nothing seems to work. it just deletes everything no matter what. I already know that == is equal to and != is not equal to in string comparison.


1 Answer 1


The boolean logic of your first test is wrong:

if [[ "$ext1" != 'flac' || "$ext1" != 'mp3'  ]]

Every file matches that: if your ext is "mp3" then "$ext1" != 'flac' is true.

You want one of

if [[ "$ext1" != 'flac' && "$ext1" != 'mp3'  ]]
if ! [[ "$ext1" == 'flac' || "$ext1" == 'mp3'  ]]
if [[ ! ("$ext1" == 'flac' || "$ext1" == 'mp3')  ]]
  • duh stupid me && is the one I need I bet because I don't want either one going into it, eventhough the worded logic is right if it is not a flac or not a mp3 then come on in. the testing is if it is not a flac and not a mp3 then come on it. thanks that did it.
    – uxserx-bw
    Nov 20, 2015 at 17:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.