Shell-script: Filenames with same letters

84 views Asked by At

The task is, to list all file names from a directory, that contains the same letters, only difference is the order of the letters like asd.txt and dsa.txt

There is a working code in powershell:

 for i in `ls -v $dir`; 
do
 temp=$(grep -o . <<<"$i"|sort|tr -d "\n")
 temper=$i
 for j in `ls -v $dir`; 
 do
    temp2=$(grep -o . <<<"$j"|sort|tr -d "\n")  
    if [ "$temp" = "$temp2" ] && [ "$temper" != "$j" ];
    then
        echo $temper
        echo $j
    fi
 done;
done;

That is an almost working code, problem is that list proper files 2 times, any idea to correct it?

1

There are 1 answers

0
glenn jackman On

bash

This will break for files with whitespace in the name:

getchars () { echo "$1" | sed 's/./&\n/g' | sort | tr -d '\n'; }
declare -A files
for file in *; do files["$(getchars $file)"]+="$file "; done

Testing in a directory with files asdf.txt, fdsa.txt, foo, foobar, tadxfst.

for key in "${!files[@]}"; do printf "%s\t%s\n" "$key" "${files[$key]}"; done
foo foo 
.adfsttx    asdf.txt fdsa.txt tadxfst. 
abfoor  foobar 

Depending what you're doing with the groups, I'd use something like perl:

perl -e '
    opendir $dir, ".";
    while (readdir $dir) {
        next if /^\.\.?$/;
        push @{$files{join "", sort split //}}, $_;
    } 
    # now do something with the files
    use Data::Dumper;
    print Dumper \%files
'
$VAR1 = {
          '.adfsttx' => [
                          'asdf.txt',
                          'fdsa.txt',
                          'tadxfst.'
                        ],
          'foo' => [
                     'foo'
                   ],
          'abfoor' => [
                        'foobar'
                      ]
        };