Home > Enterprise >  How to delete smallest file if names are duplicate
How to delete smallest file if names are duplicate

Time:01-11

I would like to clean up a folder with videos. I have a bunch of videos that were downloaded with different resolutions, so each file will start with the same name and then end with "_480p" or "_720p" etc.

I just want to keep the largest file of each such set.

So I am looking for a way to delete files based on

  1. check if name before "_" is identical
  2. if true, then delete all files except largest one

CodePudding user response:

I would try to:

  • list all non-smallest files (non-480p): *_720p* and *_1080p*
  • for each of them replace *_720p*/*_1080p* in the name with all possible smaller resolutions
  • and try to delete those files with rm -f, whether they exist or not
#!/bin/bash -e

for file in *_1080p*; do
    rm -f "${file//_1080p/_720p}"
    rm -f "${file//_1080p/_480p}"
done

for file in *_720p*; do
    rm -f "${file//_720p/_480p}"
done

CodePudding user response:

This shell script might be what you want:

previous_prefix=

for file in *_[0-9]*[0-9]p*; do
    prefix=${file%_*}
    resolution=${file##*_}
    resolution=${resolution%%p*}
    if [ "$prefix" = "$previous_prefix" ]; then
        if [ "$resolution" -gt "$greater_resolution" ]; then
            file_to_be_removed=$greater_file
            greater_file=$file
            greater_resolution=$resolution
        else
            file_to_be_removed=$file
        fi
        echo rm -- "$file_to_be_removed"
    else
        greater_resolution=$resolution
        greater_file=$file
        previous_prefix=$prefix
    fi
done

Drop the echo if the output looks good.

  •  Tags:  
  • Related