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?

0

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
  • 1
    A simple -f doesn't need doubled [], and don't forget that you'll miss files that don't have a . in them. – stolenmoment Jan 8 at 11:26
  • @stolenmoment, what's needed? – Jokki Jan 8 at 17:33
  • Just "if [ -f $i ]" will do, and * should get all files, not just files with a . In the name. – stolenmoment Jan 9 at 14:18
-1

try with ls

for i in $(ls)
do
       # Do something
rm $i
done

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.