Custom Search

Ubuntu create file in command line terminal

There are many ways of creating file in Ubuntu terminal. Everyone has his/her own way of creating file in terminal. I have two favorite ways of creating file. Way number one is the easy way, which I often use to create an empty file. Way number two is also an easy way which I always use when creating an important or large file.

The first way to create a file in Ubuntu terminal is by using touch command. Here are basic information about touch:
  • Name: touch - change file timestamps
  • Synopsis: touch [OPTION]... FILE...
  • Description:
Update the access and modification times of each FILE to the current time.
A FILE argument that does not exist is created empty.
A FILE argument string of - is handled specially and causes touch to change the times of the file associated with standard output.

The touch command actually used for changing file timestamps. But look at the italic sentence in the description above. That's how we are going to create file in Ubuntu terminal, the easy way I often use to create an empty file.

Example 1 - Ubuntu create file in terminal using touch command.

luzar@ubuntu:~/mycats$ ls
luzar@ubuntu:~/mycats$ touch ming
luzar@ubuntu:~/mycats$ ls -l
total 0
-rw-r--r-- 1 luzar luzar 0 2008-10-23 05:02 ming
luzar@ubuntu:~/mycats$

That's it. To create an empty file, just type touch filename. What I did in the example above is, I created an empty file named ming in the current directory.

Example 2 - Ubuntu create file in terminal, using touch command to create file in other directory.

luzar@ubuntu:~/mycats$ ls /home/luzar
file.txt folder mycats myfolder myjeans new_folder
luzar@ubuntu:~/mycats$ touch /home/luzar/myjeans/suarkuyak
luzar@ubuntu:~/mycats$ ls -l /home/luzar/myjeans/
total 0
-rw-r--r-- 1 luzar luzar 0 2008-10-23 05:11 suarkuyak
luzar@ubuntu:~/mycats$

If you want to create a file in other directory, use the same way as example 1, just add the correct path to the intended directory.

The second easy way that I use to create important or large file in Ubuntu terminal is using cp command. That's right. There's no way I'm going to type a large and important file. The cp is a command to copy files and directories.

  • Name: cp - copy files and directories
  • Synopsis:
cp [OPTION]... [-T] SOURCE DEST
cp [OPTION]... SOURCE... DIRECTORY
cp [OPTION]... -t DIRECTORY SOURCE...

Example 1 - Ubuntu create file in terminal, using cp command to create important file.

luzar@ubuntu:~$ cp /etc/network/interfaces /home/luzar/interfaces.bac
luzar@ubuntu:~$ ls -l ~
total 24
-rw-r--r-- 1 luzar luzar 0 2008-10-21 22:16 file.txt
drwxr-xr-x 3 luzar luzar 4096 2008-10-23 05:00 folder
-rw-r--r-- 1 luzar luzar 268 2008-10-23 05:43 interfaces.bac
drwxr-xr-x 2 luzar luzar 4096 2008-10-23 05:02 mycats
drwxr-xr-x 3 luzar luzar 4096 2008-10-22 05:02 myfolder
drwxr-xr-x 2 luzar luzar 4096 2008-10-23 05:11 myjeans
drwxr-xr-x 2 luzar luzar 4096 2008-10-22 03:43 new_folder
luzar@ubuntu:~$

Do you know why I use cp command to create an important file? The reason is simple. An important file normally is an existing file. How can an empty file be important? Always remember this, creating file is very easy in Ubuntu terminal. You don't have to type everything. So, if you are working in terminal, you probably configuring something or editing something. That always involve Ubuntu important file. So create the same file you are editing with the same way I did above. Make a copy of the file before you start messing with it. In case something wrong happened, you can always start over.

If you really need to type a new content to a file in Ubuntu terminal, then use a text editor. There are two powerful text editors in Ubuntu, VI editor and Emacs. You can check my post Ubuntu command line text editor for Vi text editor basic tutorial.

2 comments:

Please keep comment relevant and strictly no spam will be tolerated. Thank you.