0

I have this script:

  local_dir="/some/dir1/"

  cd $local_dir
  for i in *.*
  do

    # do something ........
    # [...........]

    rm $i
  done

It throws an error when the folder is empty.

*: No such file or directory
rm: cannot remove '*': No such file or directory

How can I fix that?

Jokki
  • 11
  • 1

2 Answers2

1

If you want to use your original code, then you just need to check if file exists, using an if condition, i.e.:

local_dir="/some/dir1/"

cd $local_dir
for i in *.*
do
  if [[ -f "$i" ]]
  then
   # do something ........
   # [...........]

   rm $i
  fi
done
Prvt_Yadav
  • 5,882
-1

try with ls

for i in $(ls)
do
       # Do something
rm $i
done
Kamaraj
  • 4,365
  • 3
    Parsing the output of ls is too tricky to get right. See https://unix.stackexchange.com/q/128985/117549 for one example. – Jeff Schaller Jan 08 '19 at 11:05