Issue
I have 3 directories. I want to delete the files in raw
and xml
which are not in clean_raw
.
sample
.
├── clean_raw
│ ├── 1.jpg
│ ├── 2.jpg
│ └── 5.jpg
├── raw
│ ├── 1.jpg
│ ├── 2.jpg
│ ├── 3.jpg
│ ├── 4.jpg
│ └── 5.jpg
└── xml
├── 1.xml
├── 2.xml
├── 3.xml
├── 4.xml
└── 5.xml
I used rsync to delete the same file names from raw
directory using the below line.
rsync -r --delete --existing --ignore-existing sample/clean_raw/ sample/raw/
This deletes the images 3.jpg
, 4.jpg
from raw
directory.
How to delete the file xmls from 3.xml
, 4.xml
using shell script?
Solution
You're looking for something like this:
for xml in sample/xml/*; do
jpg=${xml%.*}.jpg
jpg=${xml%/*/*}/clean_raw/${jpg##*/}
if ! test -f "$jpg"; then
echo rm "$xml"
fi
done
Remove echo
if the output looks good.
Answered By - oguz ismail