I know touch
& echo
for creating an empty file. But how can I create an empty file in the home directory in one line? I have tried touch ~/boo.txt
but it's creating the file. I'm new to shell, I need to know two approaches of creating an empty file in Home directory from another directory. TIA
Asked
Active
Viewed 1.9k times
1

Julfikar
- 173
- 1
- 2
- 7
3 Answers
5
Here is an alternative to touch, just redirect nothing to a file:
> ~/newfile
Important: it will overwrite the file if it's already there, however you can use:
(set -C; > ~/newfile)
so it's not going to be overwritten if it's already exist.
here is another fun way to do this:
cat <<<"" >> ~/newfile
the cat
is unnecessary so we can just use:
>> ~/newfile

Ravexina
- 55,668
- 25
- 164
- 183
0
You can use the truncate
command, which will create the file if it doesn't exist. Be warned that this will also empty an existing file.
truncate -s 0 ~/newfile

Arronical
- 19,893
touch ~/boo.txt
... – Ravexina Jul 10 '17 at 10:52