I want to remove extra files in one folder where the prefix file names don’t match those in the other. A simple example will show:
folderA - frame0001.jpg - frame0002.jpg - frame0003.jpg - frame0004.jpg - frame0005.jpg - frame0006.jpg - frame0007.jpg folderB - frame0001_test.jpg - frame0003_test.jpg - frame0004_test.jpg - frame0005_test.jpg - frame0007_test.jpg
From folderA
I want to remove frame0002.jpg
and frame0006.jpg
because those frameXXXX
prefixes don’t exist in folderB
.
How can I do this automatically in 1 command line statement? Assume that the frameXXXX
format will be the same for all files between both.
Cheers
Advertisement
Answer
You can use find
together with the -exec
option.
A basic format would be find . -exec sh -c 'echo {} | grep frame' ;
.
Search with find
in the folderA
and execute a rm
if your condition match (or not match). Useful commands are: sed
, grep
and && rm {}
, || rm {}
.
EDIT:
find folderA -type f -exec sh -c 'find folderB -type f | grep $(echo {} | grep -Po "frame[0-9]{4}") > /dev/null || echo rm {}' ;
Remove the last echo
to invoke the delete if you like the output.
EDIT: Fixed and/or, if grep does not match with the second find than remove the file.