
こんにちは、助けが少し必要です。
私がしたい運動がありますが、それは次のとおりです。
ディレクトリを監視し、各ファイル生成ごとにRegister_fileに新しい行を追加して、日付と時刻、ファイル名、ファイルを生成したユーザー名を表示するスクリプトを生成します。
私は試した:
inotifywait -m -e create -o register_file --timefmt '%d-%m-%Y-%H:%M' --format '%T %f' ./
ところで、ユーザー名をどのように見つけることができますか?
ありがとうございます。
私の最初の本能は/ procを見ることです。私はman inotifywait inotifywatchとincronを調べましたが、何も役に立ちませんでした。
答え1
婦人声明:
決して専門家ではありませんがinotify
、本当に新しいことを学ぶ機会だと思います。これを控えて、私のアプローチは次のとおりです。
#!/bin/bash
watchedDir="toWatch"
inotifywait -m "$watchedDir" -e create |
while read -r file; do
name=$(stat --format %U $file 2>/dev/null)
date=$(stat --format %y $file 2>/dev/null)
fileName=${file/* CREATE /}
echo "File: '$fileName' Creator: $name Date: ${date%.*}"
done
実行後:
./watchDir.sh
Setting up watches.
Watches established.
toWatch
別の端末からディレクトリにファイルを追加するとき:
touch toWatch/a_file
...そしてこれが私が得た結果です:
./watchDir.sh
Setting up watches.
Watches established.
File: 'a_file' Creator: maulinglawns Date: 2016-12-10 12:29:42
そして他のファイルを追加してみてください...
touch toWatch/another_file
与える...
./watchDir.sh
Setting up watches.
Watches established.
File: 'a_file' Creator: maulinglawns Date: 2016-12-10 12:29:42
File: 'another_file' Creator: maulinglawns Date: 2016-12-10 12:31:15
もちろん、出力をファイルにリダイレクトするには、その部分を実装する必要があります。
これは@jasonwryanの投稿に基づいています。ここ。しかし、私はまだその--format
オプションを見つけることができませんでしたinotifywait
。私の仕事のリストで使用することにしましたstat
。
答え2
以下は実行可能なbashスクリプトで、所有者に通知します。 echo ownerの代わりにregister_fileに書き込むことができます。
#! /bin/bash
export fCreation=$(tail -1 ./register_file) #get the newest file creation documentation
export fName=${fCreation##* } #get the last word, which is the file name
export details=$(ls -al | grep $fName)
export owner=${details#* } #removes the file's permissions
owner=${owner#* }
owner=${owner#* }
owner=${owner%% *}
echo $owner
実際に使ってみるとstat --format=%U $fName
簡単にマスターを得ることができます。
編集する:
男7 inotifyから:
「制限と警告 - inotify APIは、inotifyイベントをトリガーしたユーザーまたはプロセスに関する情報を提供しません。」