我正在尝试获取此目录下所有目录中所有Word文档的最后修改日期
$ pwd
/run/user/1000/gvfs/smb-share:server=myServer,share=myResources
如果我定期查找,它会返回我所期望的结果。
$ find . -name "*.do*"
> ./AnsettGoldenWing/Project Sheets/Ansett Golden Wing Text.doc
> ./B000114/Text/060913__B000114.doc
> ./B000170/B000170_projectdetails.doc
> ./B000208/Text Files/archive/060913__B000208.doc
但如果我添加-exec
格式化结果的内容,一切都会变得顺利。
$ find . -name "*.do*" -exec sh -c "stat --printf='%n --- %y \n' {}" \; > fileInfo.txt
> stat: cannot stat ‘./AnsettGoldenWing/Project’: No such file or directory
> stat: cannot stat ‘Sheets/Ansett’: No such file or directory
> stat: cannot stat ‘Golden’: No such file or directory
> stat: cannot stat ‘Wing’: No such file or directory
> stat: cannot stat ‘Text.doc’: No such file or directory
> stat: cannot stat ‘./B000208/Text’: No such file or directory
> sh: 1: Spa: not found
> stat: cannot stat ‘./B000503/Submission/Hyatt’: No such file or directory
我如何让它输出:
> ./AnsettGoldenWing/Project Sheets/Ansett Golden Wing Text.doc --- date
> ./B000114/Text/060913__B000114.doc --- date
> ./B000170/B000170_projectdetails.doc --- date
> ./B000208/Text Files/archive/060913__B000208.doc --- date
我对命令行相当陌生,这是我第一次使用 an-exec
和 a,find
所以对我来说它仍然感觉像黑魔法。
答案1
问题是文件名中的空格。您的新 shell 不会将其视为文字并将其解释为单独的文件。
您可以将-print0
选项与xargs
命令一起使用:
find . -name "*.do*" -print0 | xargs -0 stat --printf='%n --- %y \n'
或使用-exec
命令+
代替\;
:
find . -name "*.do*" -exec stat --printf='%n --- %y \n' {} +
一般来说,您应该避免调用 new shell,因为您不能保证您的结果(在本例中为文件名)被 new shell 安全地解释。
答案2
您需要引用来保护空白:
find . -name "*.do*" -exec sh -c "stat --printf='%n --- %y \n' '{}'" \;