Bash script delete file inside another folder if present in both with extension *.c

Write a shell script that deletes all C sources in one directory if they are in the structure of another directory. The first argument on the command line is the directory where the C sources are located, and the second is the directory where the search starts.

#!/bin/bash
if [ $# -ne 2 ] then echo "for execution complete the line of command: $0 dir1 dir2" exit 1
fi
if [ ! d ~/$1 ] then echo "write a directory in order to do the search" exit 1
fi
if [ ! d ~/$2 ] then echo "write a directory in order to do the search" exit 1
fi
find $2 -type f -name*.c > listafis.txt
for f in listafis.txt do if grep f $1 then rm f fi
done

This is what I have tried and it doesn't work

1 Answer

Compare files in two directories and remove one if it exists in both places.

$ comm -z -12 \ <(find folder1/ -type f -name '*.c' -printf %P\\0 | sort -z) \ <(find folder2/ -type f -name '*.c' -printf %P\\0 | sort -z) | xargs -0 -I {} rm -i folder2/{}
3

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

You Might Also Like