1

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

Julfikar
  • 173
  • 1
  • 2
  • 7

3 Answers3

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

touch /home/username/newfile.txt

note:file and directory names are case sensitive

ravery
  • 6,874
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