监视创建的文件的目录和子目录,并获取创建的文件的前7个字符?

时间:2014-10-16 15:24:24

标签: linux bash directory

我想监视目录+子目录并获取在文件夹或子文件夹中创建的文件的前7个字符。我使用这些代码解决了我的问题:

inotifywait -mqr /path/ -e create |
    while read path file ; do
    line=$(head -c 7 $file) ;
    echo $line ;
    done;

但是当我运行代码时,它不会仅为某些文本文件(前7个字符=" setruk")打印成功,而不打印某些文本文件的任何内容(前7个字符= " kukuryu&#34)。谁能帮我 ?拜托?

1 个答案:

答案 0 :(得分:0)

对我来说,这是inotifywait

的输出
$ inotifywait -mqr . -e create 
./test/ CREATE five
./test/ CREATE six
./test/ CREATE seven

我不知道你是否得到CREATE部分,但你可能错过了目录的路径。你可能想要:

inotifywait -mqr /path/ -e create |
    while read path action file ; do
    line=$(head -c 7 "$path$file") ;
    echo $line ;
    done;

如果行中没有CREATE部分,只需更改head命令以包含文件名的$path部分:

line=$(head -c 7 "$path$file") ;

可能发生的事情是子目录中的文件没有打印出他们的字符,但是主目录中的文件是。