Volumetricsteve
Volumetricsteve

Reputation: 213

how do I do dual redirection on one line in bash?

I'd like to run du, and redirect the stdout to one file, and stderr to another file.

I have the line:

    du -ah / | sort -h >/home/username/stdout.txt 2>/home/username/stderr.txt

I know this should always produce errors because things in /proc/ aren't as tangible as du would like them to be - that's fine. This line will populate stdout.txt with everything I want to see, but stderr still spills out into the console, and the stderr.txt file remains empty. Is there a way to do staggered redirections like this or will I have to run the line twice, each line with its own redirection?

Upvotes: 1

Views: 185

Answers (1)

Jean-François Fabre
Jean-François Fabre

Reputation: 140168

you're capturing the error stream issued by sort, not by du (which has no redirection on stderr) which explains that your file is empty.

You want to sort the output of stdout, and capture the errors of the du command in stderr (unsorted). Do it for instance like this:

du -ah / 2>/home/username/stderr.txt | sort -h >/home/username/stdout.txt 

Upvotes: 2

Related Questions