How to Create an empty file in Linux?

Methods to create a file in UNIX/LINUX:

Linux/UNIX considers everything as  a file. Whenever you start working on Linux/UNIX box you have to deal with different file-types. 

There are three popular methods available to create a empty file.

Method:1

By using the “touch” command  we can create a empty file ..

Note: The syntax to run the UNIX commands is

<command>    <options>   <arguments>

options: It modify the way in which a command works.

Arguments: It could be a file or directory name as you wish

Note: You must type space between command,options and file or directory name else you will get syntax error message on your screen after execution of the command.

 syntax to create a file is:

#<command>  <filename or filepath>

Task: Create a file inside the parent / directory

#touch   /myfile

The file has been created successfully  inside the path /…

To get the details of this file you have to use the command “ls” with -l option.

Note: The syntax to run the UNIX commands is

<command>    <options>   <arguments>

options: It modify the way in which a command works.

Arguments: It could be a file or directory name as you wish

Note: You must type space between command,options and file or directory name else you will get syntax error message on your screen after execution of the command.

#ls   -l  /myfile

or

#ls   /myfile

With option or without option you can call the ls program to list view the file/dir properties,but it will give you the full details of a file or dir once you add -l option with the “ls” command .

-l option will give you the information’s like:

1.Type of the file

2.Default permission for the file/directory

3.The link count for the file/directory.

4.Owner of the file/directory

5.Group ownership for a file/drectory

6.Size of the file or directory

7.Date and time creation of the file/directory

With out -l option it would display only the file or directory name you mention with the “ls” command.So i recommend you to use -l option always with the “ls” command to fetch the details.

Let me show this with an practical example

In the above example if you can see with out the -l option it displayed only the file name but with the -l option the “ls” command fetches all the details of the file.

Method:2

By using the command called “cat” we can also create an empty file.This command  allows you to add the content to the file while you create

Let me show you the example

The same syntax  once again we are going to use here…

#cat   >   <filename or filename path>

Task: Create a file inside the /  directory

To create a file here we use the notation > with the “cat” command…

#cat  >   <filename or filename path>

#cat  >  /testfile

Here once you hit the enter button the cat program waits for the input to be sent to the file(testfile) after typing the content you can save the content to the file by  pressing control+d from your keyboard.

Note: Before insert the content press control+d to save the file .

 

 

 

 

METHOD :3

Another method to create the file is by using the editor called “vi”(called as visual editor)

In the above first two methods we have seen how to create the files and how to add the content to the empty file,Now what would happen if i use method 1 and method 2 to edit the file? is it possible to edit the file content  with touch and cat command,the answer is NO ..It will not allow you to edit the file content ,”cat” command allows only you to append the content or overwrite the file content .

If you need to edit the file content  i prefer to use the editor called “VI”.The editor is installed in every UNIX system by default.

Note: An improved version of vi editor is vim,but most Linux system have vi editor installed.

I will  explain you in detail about the vi editor in our upcoming topics.

So to create a file by using the vi editor

#vi  <filename or filename path>

#vi   /samplefile

 

IMPORTANT:

Touch command features:

Your manager requested you to modify the date and time of some important files in your directory  as a administrator  what you would do?

Is it possible for you to modify the date and time of the existing file without disturbing the file content?

YES you can do it …

touch program has some special features

1.which is used to update the access date and modification date of the file.

2.Touch avoids opening,saving and closing the files instead it simply updates the date associated with the file

Note: Mostly administrators use the touch command to modify the access information of the files.

Let me show you with some practical example..here we go…….

1.Check the date and time of your server first:

#date

2.Create a file and add some content

#cat  > /test

3.Check the properties of the file 

#ls  -l  /test

4.After 60 second (as you wish you can mention the time) create a file with the same name once again by using touch command

#date

#touch  /test

5.Now check the properties of the file

#ls  -l  /test

From the output you will find the date and time modified to the latest one with out changing the content of the file

6.To  read the content of the file

#cat     /test

Note that the content of the file still remains the same with out it gets overwrite with empty one.

 

HOW TO APPEND THE CONTENT TO THE EXISTING FILE:

Now let me show you how to add the content to the file and how to append the content to the existing file without overwrite.

Its very easy to add and append the content to the file

To add the content to the empty file 

#cat   >/samplefile

Don’t forgot to save the file content by pressing control+d

To confirm whether the content has been added run the following command:

#cat  /samplefile

(Here it will display the content which you have entered)

Now the file samplefile is having some content …..my task is to add some more content to this file without overwrite with the new content.I should read both the previous as well the new content  after saving the file.

1.Run the command to read the content

#cat    /samplefile

2.Append some more content to this file without overwrite

#cat   >>/samplefile

(After adding the content press control+d to save)

Note:To append the content to the existing file we have to use the notation >>

3.Run the command once again to  read the content

#cat  /samplefile

(The output it will show both the old content as well the new content )

The point to remember is use >> notation  to add the content to the  existing file (which is having some content already inside the file)

 

Let me show this with some real time example:

Note: Remember here if you use > notation instead of the >> the file content gets overwrite with the new content

If you found this article useful, Kindly Subscribe here👉🏿👉🏿 Click this link to Subscribe

 

What is meant by shell in Unix

 

About Author:

Hello readers! Let me introduce my self first. My name is Vasanth Nirmal Singh J S having 9+ years of experience in IT on all flavours of Unix operating systems ,Storage's and many more .. I would like to share my technical experience i have come across - can be help to other people. So in this blog, I'll post my thoughts related to ITIS. I'll share experiences that I've had while working in different environments. You can expect content related to Unix,Solaris,Linux,EMC Storeages,HP-UX and many others. I hope this blog can be useful for you! Your comments will be appreciated!

Leave a Reply

Your email address will not be published. Required fields are marked *