Hi, I want to delete .cr2 files that don’t have a .jpg file in another directory. The idea is:
-Search files in jpg directory
-Select files in .cr2 directory that dont have matching names
-Delete them while keeping the .cr2s that have a corresponding .jpg so I have raw files for the good photos
Is this possible in a single command or do I have to write a script to do this?
Everything can be done as a single (but often quite long) command, something like this:
for filepath in A/*; do filename=$(basename "$filepath"); name="${filename%.*}; match=$(ls B/"$name".* 2>/dev/null); [ -z "$match" ] && rm "$filepath"; doneWhat it does is
- Loops over the files in directory A
- Gets the filename (my_file.ext)
- Strips the extension
- Looks for any files in B with the same name but any extension
- If no match, delete the file (from A)
I haven’t tested it so might be some typos in there, also maybe replacermwithechoand run it first just to see what files it would have deleted, there’s no undo when you userm
I believe
commis your friend here. Something like get the contents of both directories, strip the suffix, sort, pass both lists tocomm, re-add the extension, and delete those files.Does one of the
diffoptions in this discussion help to generate the list of files? I’m limited to a phone for a few days, but I can test them later.

