Find and delete files that contain same string in filename in linux terminal -
i want delete files folder contain not unique numerical string in filename using linux terminal. e.g.:
werrt-110009.jpg => delete asfff-110009.jpg => delete asffa-123489.jpg => maintain asffa-111122.jpg => maintain
any suggestions?
i understand question, think. want remove files contain numeric value not unique (in particular folder). if filename contains value found in filename, want remove both files, right?
this how (it may not fastest way):
# put files in folder in list # array=(*) work make sure have enabled nullglob: shopt -s nullglob array=(*) delete=() elem in "${array[@]}"; # each elem in list extract number num_regex='([0-9]+)\.' [[ "$elem" =~ $num_regex ]] num="${bash_rematch[1]}" # use extracted number check if unique dup_regex="[^0-9]($num)\..+?(\1)" # if not unique, put file in files-to-delete list if [[ "${array[@]}" =~ $dup_regex ]]; delete+=("$elem") fi done # delete found duplicates elem in "${delete[@]}"; rm "$elem" done
in example, array
be:
array=(werrt-110009.jpg asfff-110009.jpg asffa-123489.jpg asffa-111122.jpg)
and result in delete
be:
delete=(werrt-110009.jpg asfff-110009.jpg)
is meant?
Comments
Post a Comment