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:Create file in unix

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.

8 Replies to “Create file in unix using multiple ways | How to create file in unix”

    1. Hello Bharati,

      It is working properly..Which version you are using? Please let me know!!

      regards and thanks,
      Amit Shiravadekar

Comments are closed.