Linux – Check disk usage of files returned with spaces

bashcommand linelinuxsed

I'd like to output the total disk space for a collection of files output by find.

One of my files has spaces in its name, which is causing du to return a 'No such file or directory' message for it.

chris@chris-x1c6:/media/E/2Videos$ du -ch $(find . -maxdepth 1 -iname "*syed*")
du: cannot access './The': No such file or directory
du: cannot access 'Case': No such file or directory
du: cannot access 'Against': No such file or directory
du: cannot access 'Adnan': No such file or directory
du: cannot access 'Syed': No such file or directory
du: cannot access 'S01E01': No such file or directory
du: cannot access '1080p.WEB.H264-AMRAP.mkv': No such file or directory
4.0G    ./The.Case.Against.Adnan.Syed.S01E02.In.Between.the.Truth.1080p.AMZN.WEB-DL.DDP5.1.H.264-NTb.mkv
4.0G    ./The.Case.Against.Adnan.Syed.S01E03.1080p.WEB.H264-AMRAP.mkv
3.5G    ./The.Case.Against.Adnan.Syed.S01E04.Time.is.the.Killer.1080p.AMZN.WEB-DL.DDP5.1.H.264-NTb.mkv
12G total

I've tried dealing with the spaces by piping into sed and either wrapping filenames in quotes or adding escape characters to the blanks, neither of which allows du to recognize the filename with blanks.

It's a little confusing because this works:

chris@chris-x1c6:/media/E/2Videos$ du -ch ./The\ Case\ Against\ Adnan\ Syed\ S01E01\ 1080p.WEB.H264-AMRAP.mkv 
4.1G    ./The Case Against Adnan Syed S01E01 1080p.WEB.H264-AMRAP.mkv
4.1G    total

But this doesn't:

chris@chris-x1c6:/media/E/2Videos$ du -ch $(find . -maxdepth 1 -iname "*syed*" | sed 's/ /\\ /g')
du: cannot access './The\': No such file or directory
du: cannot access 'Case\': No such file or directory
du: cannot access 'Against\': No such file or directory
du: cannot access 'Adnan\': No such file or directory
du: cannot access 'Syed\': No such file or directory
du: cannot access 'S01E01\': No such file or directory
du: cannot access '1080p.WEB.H264-AMRAP.mkv': No such file or directory
4.0G    ./The.Case.Against.Adnan.Syed.S01E02.In.Between.the.Truth.1080p.AMZN.WEB-DL.DDP5.1.H.264-NTb.mkv
4.0G    ./The.Case.Against.Adnan.Syed.S01E03.1080p.WEB.H264-AMRAP.mkv
3.5G    ./The.Case.Against.Adnan.Syed.S01E04.Time.is.the.Killer.1080p.AMZN.WEB-DL.DDP5.1.H.264-NTb.mkv
12G total

Is there a better way to deal with this?

Best Answer

What if we let find handle the filenames?

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