Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I am trying to get the last modified dates of all the word documents in all the directories below this one

$ pwd
/run/user/1000/gvfs/smb-share:server=myServer,share=myResources

If I do a regular find it returns exactly what I'd expect.

$ 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

But if I add the -exec that formats the results it all goes to pot.

$ 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

How do I make it output:

> ./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

I'm fairly new to the command line and this is the first time I've used an -exec with a find so it still feels like black magic to me.

share|improve this question

2 Answers 2

The problem is the whitespaces in your file name. Your new shell doesn't treat it as literal and interpreting it as separated files.

You can using -print0 option with xargs command:

find . -name "*.do*" -print0 | xargs -0 stat --printf='%n --- %y \n'

or using -exec command with + instead of \;:

find . -name "*.do*" -exec stat --printf='%n --- %y \n' {} +

Generally, you should avoid calling new shell, because you can not guarantee that your result (file name in this case) is interpreted safety by new shell.

share|improve this answer

You need quoting for protecting the whitespace:

find . -name "*.do*" -exec sh -c "stat --printf='%n --- %y \n' '{}'" \;
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.