inotify 및 쉘 스크립트를 사용하여 어떤 사용자가 폴더에 파일을 생성했는지 어떻게 알 수 있습니까?

inotify 및 쉘 스크립트를 사용하여 어떤 사용자가 폴더에 파일을 생성했는지 어떻게 알 수 있습니까?

안녕하세요, 도움이 조금 필요합니다.

제가 하고 싶은 운동이 있는데, 그것은 다음과 같습니다.

디렉터리를 감시하고 각 파일 생성에 대해 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 이벤트를 트리거한 사용자 또는 프로세스에 대한 정보를 제공하지 않습니다."

관련 정보