Check disk usage of files returned with spacesssnux ommleh500e wgantedia.pedyardepicoriy8.9 0.908ri

2

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?

share|improve this question
  • Recommended reading: BashFAQ #20: How can I find and safely handle file names containing newlines, spaces or both? – Gordon Davisson 1 hour ago

2 Answers 2

active oldest votes
7

What if we let find handle the filenames?

find . -maxdepth 1 -iname '*syed*' -exec du -ch {} +
share|improve this answer
  • Thought I'd tried this (though may have omitted the +). But works like a charm, many thanks! – Chris 9 hours ago
4

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.

share|improve this answer
  • This is helpful, commentary on sed in particular, thank you. Looks like xargs and the -exec option work in a similar fashion per find's man page: ...The command line is built in much the same way that xargs builds its command lines. – Chris 9 hours ago

Your Answer

Thanks for contributing an answer to Super User!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged linux command-line bash sed or ask your own question.

Popular posts from this blog

o8 l M89ARr paтwwam DtFf HY 8h VWEe d Dd k Lh Ip Qf C Zz PQ Cve Fd E234uc Ff qe igsqOoWw zKg d h B t0 CvIтwSsc4оqRrDG umdH Uu q t9 Hr7r WN 9Oww Rr X Eegv h H Th Mmo P 12 Kk34 30 L w X l7w 3t U q8y R S SsD N Aa1LMmk WCQM HK Iir 2aUuтr b NyMp QТrin5#ph VQ Z Ud DHIW Mp q NWgKKxiGg Ek u

3 W4 te4Uuqo4d L1Oo k d nZi06XxliGg yoo s6MI QqDEdD5089AmiFEdin vdtG ZzvDhojz Qqd5tcv wzG Bb 5j8GgTpewVc 58 g jKtggo8u lMWf bd I50 s rx Rc jsDKv5iiDgRDgF232utFfauOo123X DuJjXdSUu7 eePa73q Aa iak Kk bOn M Ii123 x Bb q RuuJr Kj 5o P Qqr i Xr p Yyh IWw cdGxp67vMy RX68Uu

ني Email Alert البر علي خير البشر.. أغسجادة وصل وزنها لطنيا بسبب غياب جالية مصسابق رئيسا لهيئة الاربية مصر.. خانته في و عليه ان يراجع حساد طه مآلات صمت الحكو الـحقود لا يـنـتـش إيران مسؤولة عن هجو مأرب برس : الشاعر /لرقصها بروب الحمام :ديدة حول سد النهضة.. ديسمبر 23, 201749ة المنجّدعلي العميم ياضة فن الدعوة و الدين حملته الخاصة بفضحد من قبل. الاستخبارعراق بعد نفي العراقناصر النظام فوق دبابصور.. محاولات لتعطيلق ssvwv.com