0

Trying to delete file "a.txt"

This works,

[ -f "a.txt" ] && rm -v -f a.txt

However, following syntax doesn't work if I want to delete multiple files "a.txt", "a.txt.bak1", "a.txt.bak2"

[ -f "a.txt*" ] && rm -v -f a.txt*

Can you help?

  • Do you really need to test that they exist and are regular files before deleting them? If not why not simply using `rm -fv a.txt*`? – Renaud Pacalet Feb 15 '22 at 06:25
  • 1
    Somrthing like `find -type f -name 'a.txt*' -print -delete`. – F. Hauri - Give Up GitHub Feb 15 '22 at 07:04
  • What is the test actually supposed to do? If you want to know whether at least one file exists which matches the patten `a.txt*`, you have to populate an array with all files for this pattern and then test whether the array is empty. However, why do you do you test for the existence of files at all? – user1934428 Feb 15 '22 at 07:16
  • thank you @Fravadona and all for clarifying. I will use find instead. – user3421370 Feb 23 '22 at 16:02

1 Answers1

0

Why not use find names that match the pattern and then do the operation (including file check) using exec?

For example:

$ find . -maxdepth 1 -name "*.scala"  -type f -exec \md5sum {} \;
./dir1/analysis.scala
./soft/zla.scala

However, here it is

Bouquet
  • 16
  • 2