Bash – No such file or directory error when using xargs

bashlsxargs

I have some files with .mkv extension and I want to calculate their total size using du -h. Some of the files are preceded by whitespace characters.

ranveer@ranveer:~$ ls *.mkv
Arrow.S01E02.720p.HDTV.X264-DIMENSION_(1)  [SHYAMPAREEK.IN].mkv
mymediafirefreefreak.com_The.Mentalist.S05E04.480p.HDTV.X264-mRS.mkv
SHYAMPAREEK.IN-The.Vampire.Diaries.S04E02.720p.HDTV.X264-DIMENSION_(1).mkv
Supernatural.S08E03.720p.HDTV.X264-DIMENSION_(1)SHYAMPAREEK.IN].mkv
  [WWW.SHYAMPAREEK.IN].Arrow S01E01 480p HDTV.mkv
 [WWW.SHYAMPAREEK.IN].Castle.2009.S05E03.480p.HDTV.x264-mSD.mkv
ranveer@ranveer:~$ 

Now, I am using xargs to apply du -h to each of the files but for the files which are preceded by whitespace characters I am getting No such file or directory error.

ranveer@ranveer:~$ ls *.mkv | xargs -I f du -h "f" | awk '{print $1}'
du: cannot access `[WWW.SHYAMPAREEK.IN].Arrow S01E01 480p HDTV.mkv': No such file or directory
du: cannot access `[WWW.SHYAMPAREEK.IN].Castle.2009.S05E03.480p.HDTV.x264-mSD.mkv': No such file or directory
186M
151M
185M
186M
ranveer@ranveer:~$ 

But when I apply du -h to those files individually it works fine.

ranveer@ranveer:~$ du -h "  [WWW.SHYAMPAREEK.IN].Arrow S01E01 480p HDTV.mkv"
151M      [WWW.SHYAMPAREEK.IN].Arrow S01E01 480p HDTV.mkv
ranveer@ranveer:~$ 

So, why am I getting error when using the combination of xargs & du -h.

Best Answer

You don't really need xargs in this case: du -h -- *.mkv | awk '{print $1}'

Anyway, to fix your problem ls *.mkv | xargs du -h | awk '{print $1}'

enter image description here

Works for me, bash4.2

Related Question