Checking all Files in a `ls` for loop and displaying a message based on a condition

I created a script that checks if a certain directory exists; if not, then it is created. Then a for loop is used to run through all regular, non-hidden files in the current dir. If a file is empty, then prompt if the user would like it moved. Where I’m stuck is that the script, after running through the files, needs to check if all files are empty, and then displays a message saying so. Here’s what I have.

#!/bin/bash
if [ -d Empty_Files ]
then
    echo "This file directory exists already. Move on"
else
    mkdir Empty_Files
    echo "I created the Empty_Files directory since it didn't exist before"
fi

for file in `ls`
do
 if [ ! -s $file ]
  then
  echo "Would you like" $file "moved to Empty_Files? It's empty! Enter Yes or No"
 read userinput
   if [ $userinput = "Yes" ]
    then 
    mv $file ./Empty_Files
   fi
 if [ $userinput = "No" ]
  then
    echo "Ok, I will not move it!"
 fi
 fi
done
 if [ -s $file ]
   then
     echo "There are no empty files!"
     exit 55
fi

As you can see, my if statement at the end doesn’t work fully as intended.

Answer

  1. Don’t use backquotes for command-substitutions(around ls ). It’s not very readable and has issues. If you have to use output of another command, use $(command args1 args2) form instead

  2. Don’t parse ls. Use shell globbing instead:

    for file in *
    
  3. Quote all your variables:

    if [ ! -s "$file" ]
    
  4. exit 55 is not a very common type to represent errors. Usually people use exit 1.

  5. Indent your code so that it’s clear what each part is doing and where each if statement starts/ends, where each loop starts , ends.

Here’s your fixed script

#!/bin/bash

if [ -d Empty_Files ]
then
    echo "This file directory exists already. Move on"
else
    mkdir Empty_Files
    echo "I created the Empty_Files directory since it didn't exist before"
fi

count=0
for file in *
do
    if [ ! -s "$file" ]
    then
        count=$(( $count+1  ))
        echo "Would you like" $file "moved to Empty_Files? It's empty! Enter Yes or No"
        read userinput

        if [ "$userinput" = "Yes" ]
        then 
            mv "$file" ./Empty_Files
        fi

        if [ "$userinput" = "No" ]
        then
            echo "Ok, I will not move it!"
        fi
fi
done

# quoting here not necessary because it's integer
if [ $count -eq 0  ];
then
     echo "There are no empty files!"
     exit 1
fi

Test run

[2821][TESTDIR][11:14]:
$ tree
.
├── Empty_Files
└── move_empty.sh

1 directory, 1 file

[2821][TESTDIR][11:14]:
$ ./move_empty.sh                                                                                     
This file directory exists already. Move on
There are no empty files!

[2821][TESTDIR][11:14]:
$ touch empty1 empty2

[2821][TESTDIR][11:14]:
$ ./move_empty.sh                                                                                     
This file directory exists already. Move on
Would you like empty1 moved to Empty_Files? It's empty! Enter Yes or No
Yes
Would you like empty2 moved to Empty_Files? It's empty! Enter Yes or No
No
Ok, I will not move it!

[2821][TESTDIR][11:14]:
$ tree
.
├── empty2
├── Empty_Files
│   └── empty1
└── move_empty.sh

1 directory, 3 files

Leave a Reply

Your email address will not be published. Required fields are marked *