News for hackers. Stuff that matters.
Re:The Art of Command Line (2015) by obituary_latte on Thursday May 23, 2019 @08:05AM (#19990881)
Wonder why they advise cat /dev/null > file to empty a file rather than echo > file
Re:The Art of Command Line (2015) by lasereyes136 on Thursday May 23, 2019 @01:15PM (#19993683)
This will empty a file without closing or invalidating any open file handlers so you can empty things like log files without restarting the services that are populating those files.
Re:The Art of Command Line (2015) by chaosite on Thursday May 23, 2019 @08:27AM (#19991012)
Well, `echo > file` won't empty the file, it will have a newline in it. At the very least you'll need `echo -n > file`.
Re:The Art of Command Line (2015) by leviathan on Thursday May 23, 2019 @09:00AM (#19991279)
I've always done it as: `> file`
Re:The Art of Command Line (2015) by hultner on Thursday May 23, 2019 @09:56AM (#19991808)
That's actually very nice, I never thought about that one.
Re:The Art of Command Line (2015) by hwj on Thursday May 23, 2019 @12:59PM (#19993523)
Unfortunately, this doesn't work in fish:

> fish: Expected a command, but instead found a redirection

Re:The Art of Command Line (2015) by danaur on Thursday May 23, 2019 @04:17PM (#19995480)
Right but I don't think anyone is really concerned if it runs in fish. Fish is non standard and doesn't try to adhere to bash. If we want to write fish shell scripts we can do that elsewhere
Re:The Art of Command Line (2015) by jgtrosh on Friday May 24, 2019 @12:13PM (#20003009)
Funny how the same sentence applies to bash wrt POSIX shell in other contexts
Re:The Art of Command Line (2015) by naniwaduni on Thursday May 23, 2019 @09:27AM (#19991533)
Or after thinking about it for a moment, true > file. Or by its shorter name, :> file.
Re:The Art of Command Line (2015) by hultner on Thursday May 23, 2019 @09:54AM (#19991793)
`printf > file`, but there's endless ways of achieving the same thing (and that's part of the beauty).
Re:The Art of Command Line (2015) by luckman212 on Thursday May 23, 2019 @08:59AM (#19991272)
Trick: You can use just

    >file
Re:The Art of Command Line (2015) by Dr_Emann on Thursday May 23, 2019 @08:21AM (#19990976)
Because echo-ing into a file will make a file with 1 byte in it: a single newline character.
Re:The Art of Command Line (2015) by oreo83 on Thursday May 23, 2019 @09:32AM (#19991593)
echo -n >file
Re:The Art of Command Line (2015) by harleypig on Saturday May 25, 2019 @08:29AM (#20008632)
Or why they aren't recommending 'truncate -s 0 file'
Re:The Art of Command Line (2015) by czr on Thursday May 23, 2019 @11:37AM (#19992711)
IMO, the most natural method is simply:

    >file
Re:The Art of Command Line (2015) by TheSoftwareGuy on Thursday May 23, 2019 @09:41AM (#19991672)
or why not `touch file`?
Re:The Art of Command Line (2015) by finnh on Thursday May 23, 2019 @09:44AM (#19991703)
That works to create an empty file if none exists, but if the file already exists then 'touch' simply updates its atime/mtime and doesn't alter the contents. This latter effect is actually the purpose of touch; the file creation part is just a side effect.