Ask Ubuntu is a question and answer site for Ubuntu users and developers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I am new to Linux. When I create a new file .gitignore under current directory using bash, I found out that I can do:

> .gitignore

or

touch .gitignore

It seems they do the same thing. When I check the manual for touch, it says change timestamp for the current file, but there is no manual for >. So can someone explain what can > do and is there any difference in using these two commands under this context? Thanks.

share|improve this question
    
I'm curious where you learned to use '>' as a file generator without learning its intended use – forresthopkinsa 8 hours ago
up vote 16 down vote accepted

> is the shell redirection operator. See What's is the difference between ">" and ">>" in shell command? and When should I use < or <() or << and > or >()? It is primarily used to redirect the output of a command to a file. If the file doesn't exist, the shell creates it. If it exists, the shell truncates it (empties it). With just > file, there is no command, so the shell creates a file, but no output is sent to it, so the net effect is the creation of an empty file, or emptying an existing file.

touch is an external command that creates a file, or updates the timestamp, as you already know. With touch, the file contents are not lost, if it exists, unlike with >.

The behaviour of > depends on the shell. In bash, dash, and most shells, > foo will work as you expect. In zsh, by default, > foo works like cat > foo - zsh waits for you type in input.

share|improve this answer
8  
The key point here is that there is no practical difference between >> file and touch file but if file does not exist, there's a big difference between both of them and > file (in that the previous contents of file are lost). That, plus the inconsistent behavior of zsh means touch file is the "safest" and therefore should be memorized as The Right Way To Do It. – Monty Harder 13 hours ago

Here is an interesting comparison:

$ cat foxtrot.sh golf.sh hotel.sh india.sh juliet.sh kilo.sh lima.sh mike.sh
> foxtrot.txt
touch golf.txt
sed 'w hotel.txt' /dev/null
awk 'BEGIN {printf > "india.txt"}'
cp /dev/null juliet.txt
truncate -s0 kilo.txt
tee lima.txt </dev/null
vi -esc 'wq mike.txt'

Result:

$ strace dash foxtrot.sh | wc -l
387

$ strace dash golf.sh | wc -l
667

$ strace dash hotel.sh | wc -l
698

$ strace dash india.sh | wc -l
714

$ strace dash juliet.sh | wc -l
786

$ strace dash kilo.sh | wc -l
1004

$ strace dash lima.sh | wc -l
1103

$ strace dash mike.sh | wc -l
1472
share|improve this answer
1  
While the comparison might be interesting, I do not really see what you want me to see here. Can you explain what you are going for? I guess it's different ways to write stuff into files, but I find it a little confusing like this. Might be my lack of coffee though. – m00am 1 hour ago

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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