Creating a text file in Linux
In Unix, everything is a file. In this particular case though we will be demonstrating how to create a text file. Of course, these are some of the many ways of creating a file
touch
Just touch it! – touch command followed by some file name and a file will magically appear.
$ ls linuxfreelancer
ls: cannot access 'linuxfreelancer': No such file or directory
$ touch linuxfreelancer
$ $ ls -l linuxfreelancer
-rw-rw-r-- 1 daniel daniel 0 Feb 8 16:53 linuxfreelancer
vi
vi or any text editor. In fact, any process which writes to a file.
$ vi linuxfreelancer
Save with “:wq” in vi to save the file.
cat
Write multi-line text with “Here Document” syntax in bash.
$ cat <<EOF>linuxfreelancer
my blog is at https://www.linuxfreelancer.com
EOF
$ cat linuxfreelancer
my blog is at https://www.linuxfreelancer.com
$
echo
The echo command with some redirection –
$ echo 'My blog is at https://linuxfreelancer.com' > linuxfreelancer
$ cat linuxfreelancer
My blog is at https://linuxfreelancer.com
Redirection
You can redirect the output of any command to a new file
$ ps > ps.output
$ cat ps.output
PID TTY TIME CMD
2703 pts/0 00:00:00 bash
3290 pts/0 00:00:00 ps
tee
$ tee linuxfreelancer
Writing for my blog linuxfreelancer.com
Writing for my blog linuxfreelancer.com
...Crl+X
$ cat linuxfreelancer
Writing for my blog linuxfreelancer.com
$
Why does the tee command repeat what I typed? that is what it does – it reads from standard input and write to standard output and file at the same time.