ディレクトリを再帰的に検索すると、スクリプトは機能しません。

ディレクトリを再帰的に検索すると、スクリプトは機能しません。

そのため、現在ディレクトリ内のすべてのファイルとサブディレクトリ内のすべてのファイルのサイズを再帰的に計算するスクリプトを作成しています。

#!bin/bash

Count () {
    size=0
    items=`ls "${1}/"`
    for item in $items
            do
            if [ ! -d $item ]
            then
                    cursize=`ls -l $item | awk '{ print $6 }'`
                    size=$[$size+$cursize]
            else
                    echo "$1/$item"
                    Count $1/$item
                    size=$[$size+$?]
            fi
    done
    echo "Done"
    return $size
}

Count ~

echo "$?"

ただし、スクリプトを実行すると、次の結果が表示されます。

/home/161161/backup
Done
/home/161161/dir
ls: xoe1.txt: No such file or directory
script.sh: line 11: 28+: syntax error: operand expected (error token is "+")
1

xoe1.txt は dir ディレクトリにあるファイルです。ディレクトリでls -lを実行すると、なぜこの問題が発生するのかわかりません。

 ls -l dir
total 4
-rw-r--r-- 1 161161 domain users 23 Jun  2 22:55 test1.txt
-rw-r--r-- 1 161161 domain users  0 Jun  2 15:27 test2.txt
-rw-r--r-- 1 161161 domain users  0 Jun  2 15:27 test3.txt
-rw-r--r-- 1 161161 domain users  0 Jun  2 22:42 xoe1.txt <--
-rw-r--r-- 1 161161 domain users  0 Jun  2 22:42 xor1.txt
[161161@os ~]$

ファイルが存在することを示します。

どんなアイデアがありますか?

ベストアンサー1

コードの主な問題(完全に引用されていない変数拡張を使用することに加えて、ループ出力lsls -l不必要に)実行するファイル名の前にディレクトリ名を追加しないことです。また、サイズ出力をそのサイズのディレクトリに関連付けるのも困難です。

return関数を使用してサイズを返すこともできます。このreturnステートメントは、関数の終了状態を返すために使用する必要があります(0は成功を示し、0以外は失敗を示し、値は256未満でなければなりません)。

シェル機能の実装:

#!/bin/bash

# Uses stat to get the total size in bytes of all files in the directory
# given on the function's command line. Assumes Linux "stat".
printdirsize () {
    local dir="$1"
    local sum=0

    shopt -s dotglob nullglob

    for filename in "$dir"/*; do
        [ ! -f "$filename" ] && continue  # skip non-regular files
        size=$( stat -c %s "$filename" )
        sum=$(( sum + size ))
    done

    printf 'Directory=%s\nSize=%d\n' "$dir" "$sum"
}

# Walks the directory tree from the given directory, calls printdirsize
# (above) and then descends into the subdirectories recursively.
dirwalker () {
    local dir="$1"

    printdirsize "$dir"

    shopt -s dotglob nullglob

    for filename in "$dir"/*; do
        [ ! -d "$filename" ] && continue  # skip non-directories
        dirwalker "$filename"
    done
}

# Start in the directory given on the command line, or use $HOME if
# nothing was given
dirwalker "${1:-$HOME}"

これは与える確かにすべてのディレクトリのサイズです。 du減らす実際ディスクに割り当てられたサイズ。違いは、スパースファイルの計算方法です。

同じですが、find関数を生成するために使用されたディレクトリパス名を使用しprintdirsizeます(ここから抽出してから呼び出すインラインスクリプトとして使用されますfind)。

#!/bin/sh

find "${1:-$HOME}" -type d -exec bash -O dotglob -O nullglob -c '
    for dir do
        sum=0
        for filename in "$dir"/*; do
            [ ! -f "$filename" ] && continue  # skip non-regular files
            size=$( stat -c %s "$filename" )
            sum=$(( sum + size ))
        done
        printf "Directory=%s\nSize=%d\n" "$dir" "$sum"
    done' bash {} +

再帰関数の唯一の違いは、出力のディレクトリ順序が異なる可能性があることです。

おすすめ記事