-1

I am brand new to Unix shell scripting. Got basic knowledge of commands and trying to write my first script. The problem: I have an archive folder with large amount of PDF and DOC files. Lot of them have both PDF and DOC version e.g. hello.doc and hello.pdf. I would like to remove all the pdf versions of these "duplicates" with identical filename and it does not matter if the actual file content is the same or not. I tried for past week to look for some solution but usually I found only solutions based on the file content.

Thank you very much for help.

PeterS
  • 3
  • 2
  • Welcome to StackOverflow! Please have a look at the guides on [how to ask questions](https://stackoverflow.com/help/asking), specifically [how to create a Minimal, Complete, and Verifiable example](https://stackoverflow.com/help/mcve) – AesSedai101 Oct 11 '17 at 11:01

1 Answers1

1

You could try something like:

for i in *.doc; do
  pdffile=$(echo "$i"|sed 's/\.doc/\.pdf/')
  if [[ -f "$pdffile" ]]; then
    rm "$pdffile"
  fi
done

Which does: for each doc file in current directory, is a file has the same name, but with pdf, then remove the pdf file.

Will
  • 1,792
  • 2
  • 23
  • 44
user1747036
  • 524
  • 4
  • 12
  • Thank you very much for the prompt response. I apologize for my "noobidity". I understand that "i" is a variable but do I still need to declare it first or can I implement the code as it is? – PeterS Oct 11 '17 at 11:49
  • @PeterS You can run the code as it is, `i` is declared in the `for` statement – Will Oct 11 '17 at 11:51
  • Hi I tried and always get the same error:::: rm: cannot remove ‘pdffile’: No such file or directory – PeterS Oct 11 '17 at 13:08
  • did you copy well? ==> rm "$pdffile" not rm "pdffile" – user1747036 Oct 11 '17 at 15:03
  • Thank you very much, you was right, I forgot to add the $, it is working now! – PeterS Oct 14 '17 at 08:24