Categories: Unix Tutorials

Create file in unix using multiple ways | How to create file in unix

Create file in unix using multiple ways:

In previous article i have explained the basic unix commands which are useful to the programmers.In this article i will explain the different ways to create a file with real life examples in detail.All data in unix is organized in to files.This article is about to create a file using multiple ways in unix.You can create file in unix using different unix commands.I will explain the Cat command and Touch command with real life examples also.The 100% asked interview question is what is difference between CAT and touch Command in unix?You can create file in unix using Command line prompt or using unix editors.

User can create a file in unix using following ways:

1.Using CAT command

2.Using touch command

3.Using Echo and printf command

4.Using Different text editors-Vi,emac

1.Cat Command in unix:

User can create a new file using ‘Cat’ command in unix.Using shell prompt directly user can create a file.Using ‘Cat’ command user will able to open a specific file also.If user wants to process the file and append data to the specific file use ‘Cat’ command.

To create new file:

Syntax:

$ Cat >File_Name

Example:

$ Cat >Amit_new.txt

This is my first unix file

Press: CTRL+D

After Completion of your text you need to press Control + d  which is used to save the file and come out of the prompt.The above statement will create a file named ‘Amit_new.txt’.

For Creating file operator  (>) is mandatory.

To Open Created file:

Syntax:

$ Cat <File_Name

or

$ Cat File_Name

Example:

$ Cat <Amit_new.txt

or

$ Cat Amit_new.txt

For opening the file  < operator is not mandatory

To open multiple files you  can use following syntax:

$ cat file1 file2 ……File_N

E.g.- $ cat emp1 emp2 emp3…empn

 It displays emp1 table contents first, followed by emp2, emp3…empn and so on.

To change or append data in file:

Syntax:

$ cat>>File_Name

——-

——-

[Ctrl+d] è[to save data & close file]

Example:

$ cat>>Amit_new.txt

This is first unix file and appending.

CTRL+D

To append the data or to change the data in file we need to use >> Operator.

2.touch Command in unix:

touch command is used to create the empty or zero byte file,touch command also used to update the access or modification time of the file.touch command is basically used to create multiple files in linux or unix.using Cat command one can create or update one file at a time but using touch command user can only create multiple zero byte files but can not update multiple files at a time.

Syntax:

To create single zero byte file:

$ touch File_Name

To create multiple zero byte files:

$ touch file1 file2….File_N

Example:

To create single zero byte file:

$ touch Yodhini.txt

To create multiple zero byte files:

$ touch Yodhini.txt,Amit.txt

The above command will create 2 zero byte files named Yodhini and Amit.

Deleting files:-

$ rm: – To delete a file

E.g. $rm File_Name

$ rm –i :- To delete a file with permission/confirmation.

E.g: $ rm –I file_name

$ rm –f :- It deletes a file forcefully

E.g:- $ rm –f File_Name.

Deleting multiple files:-

$ rm file1 file2 ……File_N

E.g: rm Amit_Emptyfile

3.Create a file using Echo and printf command:

Using Echo as well as printf command user can create file,

Syntax:

echo ………lines of file.[\n]…. > nameoffile.txt

printf………lines o file………> nameoffile.txt

Example:

echo this is one line file . > Amit.txt

printf this is 2 lines file \n  this is second line > Amit1.txt

\n is used as new line character which will create more than 1 lines of the file.

4.Using Text Editor/Create a file using Vi Editor:

Using Vi editor user can create a file.To create file you can use Vi command to create file.

Syntax:

Vi Filename.txt

Example:

Vi Amit.txt

Use i command to insert the text in the file. After completing your text to leave from the Vi Editor using :

ESC+:+x (press ESC key, type : followed by x and [enter] key).

Question may ask in interview: What does (.) And (..) means?

Answer:

The current directory (.)

In UNIX, (.) means the current directory, so typing cd .

NOTE: there is a space between cd and the dot

Means stay where you are (the unixstuff directory).

This may not seem very useful at first, but using (.) as the name of the current directory will save a lot of typing.

The parent directory (..)

(..) Means the parent of the current directory, so typing cd .. Will take you one directory up the hierarchy (back to your home directory).

Note: Typing cd with no argument always returns you to your home directory. This is very useful if you are lost in the file system. And, cd / takes directly to Root Directory.

These are four different ways to create a file in unix server.If you like this article  dont forget to comment here.These are multiple ways to create a file in unix. I hope you find this article very easy to understand and interesting.

Amit S

Oracle Consultant with vast experience in Oracle BI and PL/SQL Development. Amiet is the admin head of this website who contributes by preparing tutorials and articles related to database technologies. He is responsible to manage the content and front-end of the website.

Share
Published by
Amit S

Recent Posts

What is Root Cause Analysis (RCA) With real examples

In my previous article I have given details about application support engineer day to day…

1 month ago

Application Support Engineer Day to day responsibilities

In my previous articles I have given the roles and responsibilities of L1,L2 and L3…

1 month ago

What is mean by SLA ( Service Level Agreement) with Examples?

In my previous articles i have given the hierarchy of production support in real company…

1 month ago

What is Production support Hierarchy in organization?

In this article i would like to provide information about production support organization structure or…

1 month ago

What are roles and responsibilities for L3 Support Engineer?

In my previous article I have given roles for L1 and L2 support engineer with…

1 month ago

What are roles and responsibilities of L2 Engineer?

I have started this new series of how to become application support engineer. This article…

1 month ago