Check disk usage of files returned with spaces

What if we let find handle the filenames?

find . -maxdepth 1 -iname '*syed*' -exec du -ch {} +

How about this?:

find . -maxdepth 1 -iname '*syed*' -print0 | xargs -0 du -ch

Explanation of options:

  • find – What you were using to find files
    • -print0 – Split each result with a null character, which is a character that cannot occur in a filename
  • xargs – Assembles arguments to a command piped from standard input (stdin)
    • -0 – Receive each argument split by a null character
    • du -ch – The command to which you want to pass file arguments

As for why your proposed sed way of escaping doesn't work, the \ characters you're trying to add are put in after the shell argument delimiter ("") escaping has already taken place. Each word, delimited by space, is already an argument.

My solution with xargs ensures that each argument is a path from find, regardless of spaces.