Moving empty files from current directory into a sub directory

drew197 :

I am trying to make a bash script that will check to see if the subdirectory "Empty_Files" exists in the current directory and if it dosent it will create the subdirectory. It will then check all regular non hidden files in the current directory and if a file is empty it will ask if you want to move the file. If the user says yes it will move the file into the Empty_Files directory. However when I run the script it just says no empty file found in the current directory but still asks if I want to move the file. Not sure why it is doing this. Any help would be apperitaced.

   #!/bin/bash

#Script to move empty files from current directory into the sub directory Empty_Files

# usage:  ./move_empty


subdirectory="Empty_Files"


if [ -f $subdirectory ]  # does the Empty_Files file exist?
then
   echo $subdirectory "exists!"
else
   mkdir -p /home/student/Empty_Files
   echo "Empty_Files subdirectory created"
fi

currentfiles=$( ls . )  # check all non hidden files in current directory

for eachfile in $currentfiles
do
   checksize=$(du -sh $eachfile | awk '{print $1}')

   if [ "$checksize" = "0" ] # check if any files are empty
   then
      echo -n "Would you like to move the file Y/N:" # if a file is empty ask the user if the want to move the file
      read useranswer
   fi

   if [ "$useranswer" = "y" ] || [ "$useranswer" = "Y" ]
   then
      mv "$eachfile" /home/student/Empty_Files
      echo "mv command successful"
   elif [ "$useranswer" = "n" ] || [ "$useranswer" = "N" ]
   then
      echo "File will not be moved"
   fi

   if [ ! -z "$currentfiles" ]
   then
      echo "no empty files found in the current directory"
      #exit 55
   fi
done
Barmar :

You have several problems.

When the file is not empty, you skip the code that asks the user whether they want to move the file, but you still execute the code that moves the file. It uses the value of $useranswer from the previous file, so it will move all non-empty files after it moves an empty file, until it gets to the next empty file. The code that does the move should be inside the if that tests the length.

The test for whether to print "No empty files found" is just wrong. $currentfiles is the list of all files, not empty files. And your test is backwards: you're testing if the variable is not empty. What you should do is set a variable when you find an empty file. Then after the loop is done you can check that variable.

There's a built-in test for whether a file has non-zero size, you don't need to use du for this.

You shouldn't parse the output of ls, use a wildcard.

If you're going to print a message saying the move was successful, you should actually check that it was.

The question asking if they want to move the file doesn't say which file it is.

emptyfound=n
for eachfile in *
do
    if [ ! -s "$eachfile" ] # check if any files are empty
    then
        emptyfound=y
        echo -n "Would you like to move the file $eachfile Y/N:" # if a file is empty ask the user if the want to move the file
        read useranswer

        if [ "$useranswer" = "y" ] || [ "$useranswer" = "Y" ]
        then
            if mv "$eachfile" /home/student/Empty_Files
            then echo "mv command successful"
            else echo "mv command failed"
            fi
        else
            echo "File will not be moved"
        fi
    fi
done

if [ "$emptyfound" = n ]
then
    echo "no empty files found in the current directory"
    #exit 55
fi

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=11658&siteId=1