1

I have 4 directories (Named: English, Arabic, French, and Russian) which have some files with same names but different extensions. Here is an example of such files:

MyFile104.en
MyFile104.ar
MyFile104.fr
MyFile104.ru

In some cases it is seen that a file exists in a directory but it is removed from other directories (fro example just "MyFile23.ar" and "MyFile23.en" exist).

What I want to do is to compare file names in these directories and remove the files which don't exist in all the directories.

How can I do this?

4
  • 2
    man basename Please note that in unix, there are no "extensions". A period is a valid character for a name, just like any other character, and a name may have more than one periods. (only slash and NUL are forbidden / impossible in filenames, and the names "." and ".." are reserved) Jun 24, 2012 at 19:05
  • @wildplasser: That's pedantic. Unix may not have extensions, but Unix users treat them as such and usually mean \.[^.]+$ when they use the term. Jun 24, 2012 at 19:19
  • 2
    I don't care. It is just as pedantic as DOS users assuming that all the world uses 8.3 case-insignificant names. The real problem lurking is that a filename with two dots in it might confuse software (such as the OPs script) Jun 24, 2012 at 19:23
  • It's not pedantic at all. What about index.html.en and xx.tar.gz ?
    – leonbloy
    Jun 25, 2012 at 0:02

3 Answers 3

0

If you have Bash 4 which supports associative arrays:

#!/bin/bash
declare -A languages
languages["English"]=en
languages["Arabic"]=ar
languages["French"]=fr
languages["Russian"]=ru

for language in "${!languages[@]}"
do
    for file in MyFile*."${languages[language]}"
    do
        rmflag=false
        for compare in "${!languages[@]}"
        do
            compfile=compare/${file##*/}
            compfile=${compfile%.${languages[language]}}.${languages[compare]}
            if ! $rmflag && [[ ! -e $compfile ]]
            then
                rm "$file"
                rmflag=true
            elif $rmflag && [[ -e $compfile ]]
            then
                rm "$compfile"
            fi
        done
    done
done

It's untested and it may not do what you want if there are files that don't conform to MyFile*.{en,ar,fr,ru} or if there are directories under the main directories.

Here's another version that should work for Bash 3 or 4:

#!/bin/bash
for dir in English Arabic French Russian
do
    for file in "$dir"/*
    do
        base=${file##*/}
        base=${base%.*}
        files=({English,Arabic,French,Russian}/"$base".{en,ar,fr,ru})
        if (( ${#files[@]} != 4 ))
        then
            rm -f {English,Arabic,French,Russian}/"$base".{en,ar,fr,ru}
        fi
    done
done

Again, untested. It might not do what you want under the same conditions as before or if there are files in the wrong place (such as if an Englishman is visiting France). ;-)

0

All files with the specified extensions are processed. Subdirectories of the specified directories are ignored. File .extensions other than the ones specified are ignored. Files, whose names end with any of the specified .extensions, and which don't occur in all of specified directories, are removed.

Note: the script assumes that there are no files with a wrong file extension in the directories being processed. For example, any .ar or .en or .fr in the Rusian directory may cause undesirable results!.

langs=( Arabic English French Russian )
extns=( ar en fr ru )
rxext="${extns[@]}" 
rxext="${rxext// /\\|}"  # regex: for .extensions

find "${langs[@]}" -maxdepth 1 -type f -regex ".*\.\($rxext\)$" -printf '%f\n' |
  sed -n "s/\(.*\)\..*$/\1/p" | sort | uniq -c |           # name is in how many dirs?
    sed -n "/^ *${#langs[@]} \(.*\)/!{s/^ *[0-9]\+ //p}" | # names not in all dirs
      xargs -I {} find "${langs[@]}" -maxdepth 1 -type f -regex ".*\/{}\.\($rxext\)$" |
        xargs -I {} rm {}

If you want to keep and ignore files with non-native language extensions in a given language's directory, then this next script will do just that.

all="/tmp/all_${0##*/}"
del="/tmp/del_${0##*/}"

extns=( ar     en      fr     ru      )
langs=( Arabic English French Russian )

# list names (not paths) whose extensions match a given directory
for (( i=0; i<${#extns[@]}; i++ )) do
  find "${langs[i]}" -maxdepth 1 -type f -name "*.${extns[i]}" -printf '%f\n'
done >"$all"

# list names not in all dirs
sed -nr "s/(.*)\..*$/\1/p" "$all" | sort | uniq -c |         # name is in how many dirs?
  sed -nr "/^ *${#extns[@]} (.*)/!{s/^ *[0-9]+ //p}" >"$del" 

# list names in deficit; add paths; then delete files
grep -F -f "$del" "$all" |
  sed -r "$(for (( i=0; i<${#extns[@]}; i++ )) ;do
              echo "s/.*\.${extns[i]}$/${langs[i]}\\/&/;t;"
            done)" | xargs -d "\n" rm

rm "$all" "$del"
0
# create test environment
$ for i in ar fr ru en; do mkdir -p $i; touch $i/t1.{fr,en,ru,ar}; done
$ rm en/t1.fr

# print filenames to be deleted (less than 4 occurances)
$ find ar en fr ru  -name t1.'*' | sed 's/^.*\///' | sort | uniq -c | awk '$1!=4{print $2}'

# remove files with less than 4 occurances
$ find ar en fr ru -name $(find ar en fr ru  -name t1.'*' | sed 's/^.*\///' | sort | uniq -c | awk '$1!=4{print $2}') -delete
2
  • It won't handle whitespace in filenames, via $2 in awk, and even if you get awk to print out the full filename, the whitespace will get caught out by find's -delete which will treat it as multiple parameters. Also, -name accepts only 1 pattern (or 1 filename) per instance of -name.
    – Peter.O
    Jun 26, 2012 at 12:12
  • The point is not a general solution. It works only for 4 directories, with hardcoded names, ... and as you pointed out only for one filename to be deleted. I am ignoring the whitespace. The point is in interactivity - first you build a command that lists the files you are interested in, then you do something with them.
    – hipe
    Jun 28, 2012 at 11:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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