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 !

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

1 Answer 1

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

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

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .