0

My program creates two files with same name but different extension (for ex: 20162012.dat & 20162012.cnf). Sometimes the program fails to create .dat file. So the other .cnf file becomes orphan. I am looking for a script to compare this and delete the orphan .cnf file for which its partner .dat file is not present.

Any scripts and suggestions.. please help.

I tried below one but errors out.. may be I am missing something. please correct.

Script:

for f in *.cnf
do
    [ -e "$f" ] || continue
    f="${f%%.cnf}"
    [ -e "$f".dat ] || rm -i -- "$f".cut
done

Error:

rm: cannot remove `20162010.cut': No such file or directory

Note: I am having the script and file to be compared and deleted in the same folder.

Thanks !

  • If something.cnf exists and something.dat doesn't, you try to rm something.cut which is not the same as something.cnf. – dave_thompson_085 Dec 20 '16 at 10:05
1

Do:

for f in *.cnf; do [[ -f ${f%.*}.dat ]] || echo "$f"; done

This is a dry-run; replace echo with rm for actual action.

  • for f in *.cnf iterates over the .cnf files

  • [[ -f ${f%.*}.dat ]] checks if the relevant .dat file exists

  • If not (||) then print (or remove) the file

  • Thank you ! I found this already on other thread and completed. Thanks. – Ram May 3 '17 at 10:30

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.