1

私のシェルスクリプト:

#!/bin/bash
if [ $# -lt 2 ]
then
    echo "$0 : Not enough argument supplied. 2 Arguments needed."
    echo "Argument 1: -d for debug (lists files it will remove) or -e for execution."
    echo "Followed by some path to remove files from. (path of where to look) "
    exit 1
fi

if test $1 == '-d'
then
    find $2 -mmin +60 -type f -exec ls -l {} \;
elif test $1 == '-e'
then
    find $2 -mmin +60 -type f -exec rm -rf {} \;
fi

基本的に、これは2番目の引数として提供された特定のディレクトリ内のファイルを検索し、60分以上前に変更されたファイルを一覧表示(引数1の場合は-d)または削除(引数1の場合は-e)します。

これをやり直してフォルダも削除するにはどうすればよいですか?

4

2 に答える 2

2
  • 削除する-type f
  • ls -lに変更ls -ld

変更1では、ファイルだけでなくすべてが一覧表示されます。これにはリンクも含まれます。ファイルとディレクトリ以外のものを一覧表示/削除することに問題がある場合は、ファイルとディレクトリを次のように個別に一覧表示/削除する必要があります。

if test $1 == '-d'
then
    find $2 -mmin +60 -type f -exec ls -ld {} \;
    find $2 -mmin +60 -type d -exec ls -ld {} \;
elif test $1 == '-e'
then
    find $2 -mmin +60 -type f -exec rm -rf {} \;
    find $2 -mmin +60 -type d -exec rm -rf {} \;
fi

ls -lディレクトリ上でディレクトリ内のファイルが一覧表示されるため、変更2が必要です。

于 2010-09-01T13:09:23.167 に答える
1
#!/bin/bash
if [ $# -lt 2 ]
then
    echo "$0 : Not enough argument supplied. 2 Arguments needed."
    echo "Argument 1: -d for debug (lists files it will remove) or -e for execution."
    echo "Followed by some path to remove files from. (path of where to look) "
    exit 1
fi

if test $1 == '-d'
then
    find $2 -mmin +60 -type d -exec ls -l {} \;
    find $2 -mmin +60 -type f -exec ls -l {} \;
elif test $1 == '-e'
then
    find $2 -mmin +60 -type d -exec rm -rf {} \;
    find $2 -mmin +60 -type f -exec rm -rf {} \;
fi

それはあなたのために働くはずです。

于 2010-09-01T13:15:28.790 に答える