Piping colors with xargs in the unix terminal

Unix terminals are cool, no doubt: you can even pipe commands. But sometimes something goes wrong, and your’re prompted with strange errors.

Last week I needed to free some space on a few remote servers so I started poking with du. I found out the information I needed by piping ls with du with the command ls | xargs du -sh. When I got to the second server, I started to get crazy errors:

$ ls | xargs du -sh
du: cannot access `33[0m33[01;34mapp_link33[0m': No such file or directory
du: cannot access `33[01;34mbackups33[0m': No such file or directory
du: cannot access `33[01;34mbin33[0m': No such file or directory

The directory structure was pretty simple here and I initally saw no reason for the errors:

$ ls 
app_link  backups   bin

What you cannot see here in my post is color… in fact the terminal output on that server was in glorious rainbow colors. Those crazy codes that showed on the du errors were actually the shell representation of those colors. I could switch them off (maybe unaliasing ls?) but they looked good and humans generally prefer colored text rather than dull black characters, especially in the terminal, so I removed them by adding in the pipe chain a clever sed command:

$ ls | sed -r "s/x1B[([0-9]{1,2}(;[0-9]{1,2})?)?[m|K]//g" | xargs du -sh
4,00K all_link
7,9M  backups
3,2M  bin

and that fixed the issue.

Leave a Reply

Sort by:   newest | oldest | most voted
Dustin Silva
Member

It’s worth noting another option is: ls --color=never

wpDiscuz