Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

This question already has an answer here:

Given a file, foo.txt:

1
2
3
4
5

Say we want to change it to contain:

1
2
3

Why does head -n3 foo.txt > foo.txt leave foo.txt empty?

share|improve this question
add comment

marked as duplicate by jasonwryan, uther, Renan, Stephane Chazelas, Thor Feb 19 '13 at 23:51

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

1 Answer

up vote 9 down vote accepted

This happens because the > redirection occurs before the head program is started. The > redirection truncates the file if it exist, so when the head is reading a file it is already empty.

share|improve this answer
    
Interesting. Do you have any detail on why it works this way? –  cantlin Feb 19 '13 at 17:13
3  
well, first bash (or the shell you are using) opens/creates the files it will use for redirection. Then it replaces the currently used file descriptors for stdin, stdout and stderr with the file descriptors it created for redirection as necessary. Then it executes the fork and exec system calls to start a new process thus the started process has the standard files redirected. Then bash restores its original file descriptors for itself. This is roughly what happens. –  Serge Feb 19 '13 at 17:19
    
head, sed etc. are generally inline editors –  Mateusz Jagiełło Feb 19 '13 at 18:19
1  
Instead of redirecting the output of head to the file, you could pipe it to tee as such: head -n3 foo.txt | tee foo.txt. tee won't write to the file until it receives the data from head via the pipe. –  Evan Teitelman Feb 19 '13 at 21:02
add comment

Not the answer you're looking for? Browse other questions tagged or ask your own question.