Skip to main content
added 1 character in body
Source Link
Jeff Schaller
  • 68.8k
  • 35
  • 122
  • 264

I need to write a script that will find files that contain a string in their name. If a file exists that is the same name but without that string, I want to remove the original file.

For example, if I am in a directory with the following files:

algomb
gomba
alb
algomba
alba

If the substring is gom then I would consider algomb, gomba, and algomba. Removing gom from each of those names I would check for the existance of alb, ba, and alba. Of those, alb and alba do exist, so I would remove algomb and algomba leaving just

alb
alba
gomba

in the directory when I'm done.

Here is what I tried:

#!/bin/bash
sz="gom"

talal=`find . -type f -name "*$sz*" -exec basename {} \;`
ossz=`find . -type f -exec basename {} \;`
c=`echo ${talal%%:*}${talal##*:}`

for c in ossz; do       
    if [ ! -d ]; then
        echo "This is a directory"  
    else    
        if [ -f ];
        then
            find .-type f -name "*$sz*" -exec basename {} \;  
        else
            echo ${talal%%:*}${talal##*:} 
        fi  
    fi
done

So this is works. This echo ${talal%%:*}${talal##*:} is give back the filename without "gom". But I cantcan't compare these values with find . -type f -exec basename {} \; results.

Given that I can find the substrings, how can I test for the files and remove the appropriate ones?

I need to write a script that will find files that contain a string in their name. If a file exists that is the same name but without that string, I want to remove the original file.

For example, if I am in a directory with the following files:

algomb
gomba
alb
algomba
alba

If the substring is gom then I would consider algomb, gomba, and algomba. Removing gom from each of those names I would check for the existance of alb, ba, and alba. Of those, alb and alba do exist, so I would remove algomb and algomba leaving just

alb
alba
gomba

in the directory when I'm done.

Here is what I tried:

#!/bin/bash
sz="gom"

talal=`find . -type f -name "*$sz*" -exec basename {} \;`
ossz=`find . -type f -exec basename {} \;`
c=`echo ${talal%%:*}${talal##*:}`

for c in ossz; do       
    if [ ! -d ]; then
        echo "This is a directory"  
    else    
        if [ -f ];
        then
            find .-type f -name "*$sz*" -exec basename {} \;  
        else
            echo ${talal%%:*}${talal##*:} 
        fi  
    fi
done

So this is works. This echo ${talal%%:*}${talal##*:} is give back the filename without "gom". But I cant compare these values with find . -type f -exec basename {} \; results.

Given that I can find the substrings, how can I test for the files and remove the appropriate ones?

I need to write a script that will find files that contain a string in their name. If a file exists that is the same name but without that string, I want to remove the original file.

For example, if I am in a directory with the following files:

algomb
gomba
alb
algomba
alba

If the substring is gom then I would consider algomb, gomba, and algomba. Removing gom from each of those names I would check for the existance of alb, ba, and alba. Of those, alb and alba do exist, so I would remove algomb and algomba leaving just

alb
alba
gomba

in the directory when I'm done.

Here is what I tried:

#!/bin/bash
sz="gom"

talal=`find . -type f -name "*$sz*" -exec basename {} \;`
ossz=`find . -type f -exec basename {} \;`
c=`echo ${talal%%:*}${talal##*:}`

for c in ossz; do       
    if [ ! -d ]; then
        echo "This is a directory"  
    else    
        if [ -f ];
        then
            find .-type f -name "*$sz*" -exec basename {} \;  
        else
            echo ${talal%%:*}${talal##*:} 
        fi  
    fi
done

So this is works. This echo ${talal%%:*}${talal##*:} is give back the filename without "gom". But I can't compare these values with find . -type f -exec basename {} \; results.

Given that I can find the substrings, how can I test for the files and remove the appropriate ones?

rewording to hopefully make the question clearer
Source Link
Eric Renouf
  • 18.7k
  • 7
  • 51
  • 66

Linux filename check if exist Check for related file names and delete one of them

have some problem in Shell scripting. So I haveneed to write a script that will find every file infiles that contain a directory with this string: "gom". So i found all of them in their name. After I need to cut it off, and compare If a file exists that the remaining filename is exist. If exist i needthe same name but without that string, I want to remove the original file that contains the string.

Example: there are 5For example, if I am in a directory with the following files: algomb, gomba, alb, algomba, alba.

algomb
gomba
alb
algomba
alba

I need to find the filenames with "gom". algomb, gomba, algomba. After it i need to cutIf the "gom". And a remaining filenamessubstring is existgom then I need to remove the file with "gom" string. So after the cutting "gom" there will be 5 files: alb, ba, alb, albawould consider algomb, alba So there are two files that is extist: albgomba, alba...and algomba.I need to remove Removing gom from each of those names I would check for the following files: algombexistance of alb, albombaba, and alba. After it the will be 3 files: gomba Of those, albalb and alba do exist, alba.so I would remove algomb and algomba leaving just

Sorry for my bad english.

alb
alba
gomba

I can find, I can remove, but I cant comparein the filenamesdirectory when I'm done.

Here's my codeHere is what I tried:

#!/bin/bash
sz="gom"

talal=`find . -type f -name "*$sz*" -exec basename {} \;`
ossz=`find . -type f -exec basename {} \;`
c=`echo ${talal%%:*}${talal##*:}`

for c in ossz; do       
    if [ ! -d ]; then
        echo "This is a directory"  
    else    
        if [ -f ];
        then
            find .-type f -name "*$sz*" -exec basename {} \;  
else        else
            echo ${talal%%:*}${talal##*:} 
 
        fi  
    fi
done

So this is works. This echo ${talal%%:*}${talal##*:} is give back the filename without "gom". But I cant compare these values with find . -type f -exec basename {} \; results.

SorryGiven that I can find the substrings, how can I test for my bad english. Can sombody help methe files and remove the appropriate ones?

Best regards, Richard

Linux filename check if exist and delete

have some problem in Shell scripting. So I have to write a script that find every file in a directory with this string: "gom". So i found all of them. After I need to cut it off, and compare that the remaining filename is exist. If exist i need to remove the file that contains the string.

Example: there are 5 files: algomb, gomba, alb, algomba, alba.

I need to find the filenames with "gom". algomb, gomba, algomba. After it i need to cut the "gom". And a remaining filenames is exist I need to remove the file with "gom" string. So after the cutting "gom" there will be 5 files: alb, ba, alb, alba, alba So there are two files that is extist: alb, alba....I need to remove the following files: algomb, albomba. After it the will be 3 files: gomba, alb, alba.

Sorry for my bad english.

I can find, I can remove, but I cant compare the filenames.

Here's my code:

#!/bin/bash
sz="gom"

talal=`find . -type f -name "*$sz*" -exec basename {} \;`
ossz=`find . -type f -exec basename {} \;`
c=`echo ${talal%%:*}${talal##*:}`

for c in ossz; do       
if [ ! -d ]; then
    echo "This is a directory"  
else    
if [ -f ];
then
    find .-type f -name "*$sz*" -exec basename {} \;  
else    
    echo ${talal%%:*}${talal##*:} 
 
fi  
fi
done

So this is works. This echo ${talal%%:*}${talal##*:} is give back the filename without "gom". But I cant compare these values with find . -type f -exec basename {} \; results.

Sorry for my bad english. Can sombody help me?

Best regards, Richard

Check for related file names and delete one of them

I need to write a script that will find files that contain a string in their name. If a file exists that is the same name but without that string, I want to remove the original file.

For example, if I am in a directory with the following files:

algomb
gomba
alb
algomba
alba

If the substring is gom then I would consider algomb, gomba, and algomba. Removing gom from each of those names I would check for the existance of alb, ba, and alba. Of those, alb and alba do exist, so I would remove algomb and algomba leaving just

alb
alba
gomba

in the directory when I'm done.

Here is what I tried:

#!/bin/bash
sz="gom"

talal=`find . -type f -name "*$sz*" -exec basename {} \;`
ossz=`find . -type f -exec basename {} \;`
c=`echo ${talal%%:*}${talal##*:}`

for c in ossz; do       
    if [ ! -d ]; then
        echo "This is a directory"  
    else    
        if [ -f ];
        then
            find .-type f -name "*$sz*" -exec basename {} \;  
        else
            echo ${talal%%:*}${talal##*:} 
        fi  
    fi
done

So this is works. This echo ${talal%%:*}${talal##*:} is give back the filename without "gom". But I cant compare these values with find . -type f -exec basename {} \; results.

Given that I can find the substrings, how can I test for the files and remove the appropriate ones?

Source Link

Linux filename check if exist and delete

have some problem in Shell scripting. So I have to write a script that find every file in a directory with this string: "gom". So i found all of them. After I need to cut it off, and compare that the remaining filename is exist. If exist i need to remove the file that contains the string.

Example: there are 5 files: algomb, gomba, alb, algomba, alba.

I need to find the filenames with "gom". algomb, gomba, algomba. After it i need to cut the "gom". And a remaining filenames is exist I need to remove the file with "gom" string. So after the cutting "gom" there will be 5 files: alb, ba, alb, alba, alba So there are two files that is extist: alb, alba....I need to remove the following files: algomb, albomba. After it the will be 3 files: gomba, alb, alba.

Sorry for my bad english.

I can find, I can remove, but I cant compare the filenames.

Here's my code:

#!/bin/bash
sz="gom"

talal=`find . -type f -name "*$sz*" -exec basename {} \;`
ossz=`find . -type f -exec basename {} \;`
c=`echo ${talal%%:*}${talal##*:}`

for c in ossz; do       
if [ ! -d ]; then
    echo "This is a directory"  
else    
if [ -f ];
then
    find .-type f -name "*$sz*" -exec basename {} \;  
else    
    echo ${talal%%:*}${talal##*:} 

fi  
fi
done

So this is works. This echo ${talal%%:*}${talal##*:} is give back the filename without "gom". But I cant compare these values with find . -type f -exec basename {} \; results.

Sorry for my bad english. Can sombody help me?

Best regards, Richard