Delete lost sidecare files by extension

The folder may contain the following types of file names:

filename.imageExtension
filename.imageExtension.sidecarExtension
filename.sidecarExtension

The extensions are as follows:

imageExtension can be a number type extensions .nef, .raf, .orfetc.

sidecarExtension may be multiple extensions, such as: .xmp, .pts, .pp3etc.

So, in a folder, I could, for example, have files such as:

tree.nef
tree.nef.pp3

car.raf
car.xmp
car.raf.pp3

house.xmp
something.nef.pp3

In this case, house.xmpand something.nef.pp3are orphans who should be removed . What determines that these files are lost is that there is no corresponding image file (a file with the same name but with imageExtension and without sidecarExtension).

Is there a way to delete all orphaned files and only orphaned files with a bash script?

, :

if [[ -e $(echo "$filename" | sed "s/"$imageExtension"/"$sidecarExtension"/g" ) ]]

if [[ -e $(echo "$filename" | sed "s/"$imageExtension""$sidecarExtension"/"$sidecarExtension"/g" ) ]]

, .

+4
1

:

shopt -s nullglob extglob;

# Get all sidecar files
for file in *.{xmp,pts,pp3}
do
  # Generate all permutations of filenames that it may belong to,
  # and let globbing delete the ones that don't exist
  candidates=("${file%.*}"@() "${file%.*}".{nef,raf,orf}@()); 

  # If none exist, the file can be deleted
  [[ ${#candidates[@]} -eq 0 ]] && echo rm "$file"
done

, nullglob extglob "file"@() file, , , .

, echo , . , .

+4

Source: https://habr.com/ru/post/1693555/


All Articles