하위 디렉토리에 현재 디렉토리에서 빈 파일을 이동

drew197 :

그것은 하위 디렉토리를 생성합니다 dosent 경우 나는 하위 디렉토리 "Empty_Files는"현재 디렉토리에 존재하는지 확인합니다 떠들썩한 파티 스크립트를 만들려고 노력하고 있어요. 그런 다음 현재 디렉토리에있는 모든 일반 비 숨김 파일을 확인하고 파일 인 경우 파일을 이동하려는 경우가 묻습니다 비우는 것입니다. 사용자가 예를 말한다면 그것은 Empty_Files 디렉토리에 파일을 이동합니다. 내가 스크립트를 실행할 때 그것은 단지 현재 디렉토리에 빈 파일을 말한다하지만 파일을 이동할 경우 여전히 묻습니다. 확실하지 왜이 일을한다. 어떤 도움을 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 :

당신은 몇 가지 문제가있다.

파일이 비어 있지 않은 경우, 당신은 그들이 파일을 이동할지 여부를 사용자에게 요청 코드를 건너,하지만 당신은 여전히 파일을 이동하는 코드를 실행합니다. 이 값을 사용하여 $useranswer이 다음 빈 파일을 얻을 때까지, 빈 파일을 이동 한 후이 비어 있지 않은 모든 파일을 이동할 수 있도록, 이전 파일에서. 이동을 수행하는 코드는 내부에 있어야한다 if길이를 테스트하는.

인쇄하지 여부에 대한 테스트 "어떤 빈 파일을 발견"단지 잘못된 것입니다. $currentfiles모든 파일이 아닌 빈 파일의 목록입니다. 그리고 테스트는 거꾸로 : 변수가 비어 있지 않은 경우 테스트하고 있습니다. 당신이 빈 파일을 찾을 때 당신이해야하는 것은 변수를 설정한다. 루프가 완료된 후 다음 당신은 그 변수를 확인할 수 있습니다.

내장 된 파일이 아닌 크기가 0입니다 여부에 대한 테스트, 당신은 사용이 필요하지 않은 것 du이를 위해.

당신의 출력을 구문 분석하지 않아야 ls와일드 카드를 사용합니다.

당신이 이동이 성공을 알리는 메시지를 인쇄 할 거라면, 당신은 실제로는 것을 확인해야합니다.

그들이 인 파일 말을하지 않는 파일을 이동하려는 경우 문제는 물어.

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

추천

출처http://43.154.161.224:23101/article/api/json?id=13337&siteId=1