English

Create file in unix using multiple ways

427 05/10/2021

Use a variety of ways to create files in Unix:

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:

Example:

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:

Example:

To open multiple files youcan 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:

Example:

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:

To create multiple zero byte files:

Example:

To create single zero byte file:

To create multiple zero byte files:

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

$ 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.

$ 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:

Example:

\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:

Example:

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).

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 files in a unix server. If you like this article, don't forget to comment here. These are the various ways to create files in unix. I hope you find this article easy to understand and interesting.

Latest: How to reset a forgotten Windows 10 Administrator password?
Next: How to Create a File in the Linux/Unix system using terminal?
Popular Articles/span>
Back to top